Home | History | Annotate | Download | only in service
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "gpu/command_buffer/service/gles2_cmd_decoder.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/gl_surface_mock.h"
     18 #include "gpu/command_buffer/service/gles2_cmd_decoder_unittest_base.h"
     19 #include "gpu/command_buffer/service/gpu_switches.h"
     20 #include "gpu/command_buffer/service/image_manager.h"
     21 #include "gpu/command_buffer/service/mailbox_manager.h"
     22 #include "gpu/command_buffer/service/mocks.h"
     23 #include "gpu/command_buffer/service/program_manager.h"
     24 #include "gpu/command_buffer/service/stream_texture_manager_mock.h"
     25 #include "gpu/command_buffer/service/stream_texture_mock.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 
     33 #if !defined(GL_DEPTH24_STENCIL8)
     34 #define GL_DEPTH24_STENCIL8 0x88F0
     35 #endif
     36 
     37 using ::gfx::MockGLInterface;
     38 using ::testing::_;
     39 using ::testing::DoAll;
     40 using ::testing::InSequence;
     41 using ::testing::Invoke;
     42 using ::testing::MatcherCast;
     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 GLES2DecoderTest : public GLES2DecoderTestBase {
     58  public:
     59   GLES2DecoderTest() { }
     60 
     61  protected:
     62   void CheckReadPixelsOutOfRange(
     63       GLint in_read_x, GLint in_read_y,
     64       GLsizei in_read_width, GLsizei in_read_height,
     65       bool init);
     66 };
     67 
     68 class GLES2DecoderTestWithExtensions
     69     : public GLES2DecoderTest,
     70       public ::testing::WithParamInterface<const char*> {
     71  public:
     72   GLES2DecoderTestWithExtensions() {}
     73 
     74   virtual void SetUp() {
     75     InitDecoder(GetParam(),  // extensions
     76                 true,        // has alpha
     77                 true,        // has depth
     78                 false,       // has stencil
     79                 true,        // request alpha
     80                 true,        // request depth
     81                 false,       // request stencil
     82                 false);      // bind generates resource
     83   }
     84 };
     85 
     86 class GLES2DecoderWithShaderTest : public GLES2DecoderWithShaderTestBase {
     87  public:
     88   GLES2DecoderWithShaderTest()
     89       : GLES2DecoderWithShaderTestBase() {
     90   }
     91 
     92   void CheckTextureChangesMarkFBOAsNotComplete(bool bound_fbo);
     93   void CheckRenderbufferChangesMarkFBOAsNotComplete(bool bound_fbo);
     94 };
     95 
     96 class GLES2DecoderGeometryInstancingTest : public GLES2DecoderWithShaderTest {
     97  public:
     98   GLES2DecoderGeometryInstancingTest()
     99       : GLES2DecoderWithShaderTest() {
    100   }
    101 
    102   virtual void SetUp() {
    103     InitDecoder(
    104         "GL_ANGLE_instanced_arrays", // extensions
    105         true,                        // has alpha
    106         true,                        // has depth
    107         false,                       // has stencil
    108         true,                        // request alpha
    109         true,                        // request depth
    110         false,                       // request stencil
    111         true);                       // bind generates resource
    112     SetupDefaultProgram();
    113   }
    114 };
    115 
    116 class GLES2DecoderRGBBackbufferTest : public GLES2DecoderWithShaderTest {
    117  public:
    118   GLES2DecoderRGBBackbufferTest() { }
    119 
    120   virtual void SetUp() {
    121     InitDecoder(
    122         "",     // extensions
    123         false,  // has alpha
    124         false,  // has depth
    125         false,  // has stencil
    126         false,  // request alpha
    127         false,  // request depth
    128         false,  // request stencil
    129         true);   // bind generates resource
    130     SetupDefaultProgram();
    131   }
    132 };
    133 
    134 class GLES2DecoderManualInitTest : public GLES2DecoderWithShaderTest {
    135  public:
    136   GLES2DecoderManualInitTest() { }
    137 
    138   // Override default setup so nothing gets setup.
    139   virtual void SetUp() {
    140   }
    141 };
    142 
    143 TEST_F(GLES2DecoderWithShaderTest, DrawArraysNoAttributesSucceeds) {
    144   SetupTexture();
    145   AddExpectationsForSimulatedAttrib0(kNumVertices, 0);
    146   SetupExpectationsForApplyingDefaultDirtyState();
    147 
    148   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
    149       .Times(1)
    150       .RetiresOnSaturation();
    151   DrawArrays cmd;
    152   cmd.Init(GL_TRIANGLES, 0, kNumVertices);
    153   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    154   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    155 }
    156 
    157 // Tests when the math overflows (0x40000000 * sizeof GLfloat)
    158 TEST_F(GLES2DecoderWithShaderTest, DrawArraysSimulatedAttrib0OverflowFails) {
    159   const GLsizei kLargeCount = 0x40000000;
    160   SetupTexture();
    161   EXPECT_CALL(*gl_, DrawArrays(_, _, _))
    162       .Times(0)
    163       .RetiresOnSaturation();
    164   DrawArrays cmd;
    165   cmd.Init(GL_TRIANGLES, 0, kLargeCount);
    166   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    167   EXPECT_EQ(GL_OUT_OF_MEMORY, GetGLError());
    168 }
    169 
    170 // Tests when the math overflows (0x7FFFFFFF + 1 = 0x8000000 verts)
    171 TEST_F(GLES2DecoderWithShaderTest, DrawArraysSimulatedAttrib0PosToNegFails) {
    172   const GLsizei kLargeCount = 0x7FFFFFFF;
    173   SetupTexture();
    174   EXPECT_CALL(*gl_, DrawArrays(_, _, _))
    175       .Times(0)
    176       .RetiresOnSaturation();
    177   DrawArrays cmd;
    178   cmd.Init(GL_TRIANGLES, 0, kLargeCount);
    179   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    180   EXPECT_EQ(GL_OUT_OF_MEMORY, GetGLError());
    181 }
    182 
    183 // Tests when the driver returns an error
    184 TEST_F(GLES2DecoderWithShaderTest, DrawArraysSimulatedAttrib0OOMFails) {
    185   const GLsizei kFakeLargeCount = 0x1234;
    186   SetupTexture();
    187   AddExpectationsForSimulatedAttrib0WithError(
    188       kFakeLargeCount, 0, GL_OUT_OF_MEMORY);
    189   EXPECT_CALL(*gl_, DrawArrays(_, _, _))
    190       .Times(0)
    191       .RetiresOnSaturation();
    192   DrawArrays cmd;
    193   cmd.Init(GL_TRIANGLES, 0, kFakeLargeCount);
    194   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    195   EXPECT_EQ(GL_OUT_OF_MEMORY, GetGLError());
    196 }
    197 
    198 TEST_F(GLES2DecoderWithShaderTest, DrawArraysBadTextureUsesBlack) {
    199   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
    200   // This is an NPOT texture. As the default filtering requires mips
    201   // this should trigger replacing with black textures before rendering.
    202   DoTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 3, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
    203                kSharedMemoryId, kSharedMemoryOffset);
    204   AddExpectationsForSimulatedAttrib0(kNumVertices, 0);
    205   {
    206     InSequence sequence;
    207     EXPECT_CALL(*gl_, ActiveTexture(GL_TEXTURE0))
    208         .Times(1)
    209         .RetiresOnSaturation();
    210     EXPECT_CALL(*gl_, BindTexture(
    211         GL_TEXTURE_2D, TestHelper::kServiceBlackTexture2dId))
    212         .Times(1)
    213         .RetiresOnSaturation();
    214     EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
    215         .Times(1)
    216         .RetiresOnSaturation();
    217     EXPECT_CALL(*gl_, ActiveTexture(GL_TEXTURE0))
    218         .Times(1)
    219         .RetiresOnSaturation();
    220     EXPECT_CALL(*gl_, BindTexture(GL_TEXTURE_2D, kServiceTextureId))
    221         .Times(1)
    222         .RetiresOnSaturation();
    223     EXPECT_CALL(*gl_, ActiveTexture(GL_TEXTURE0))
    224         .Times(1)
    225         .RetiresOnSaturation();
    226   }
    227   SetupExpectationsForApplyingDefaultDirtyState();
    228   DrawArrays cmd;
    229   cmd.Init(GL_TRIANGLES, 0, kNumVertices);
    230   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    231   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    232 }
    233 
    234 TEST_F(GLES2DecoderWithShaderTest, DrawArraysMissingAttributesFails) {
    235   DoEnableVertexAttribArray(1);
    236 
    237   EXPECT_CALL(*gl_, DrawArrays(_, _, _))
    238       .Times(0);
    239   DrawArrays cmd;
    240   cmd.Init(GL_TRIANGLES, 0, kNumVertices);
    241   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    242   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
    243 }
    244 
    245 TEST_F(GLES2DecoderWithShaderTest,
    246        DrawArraysMissingAttributesZeroCountSucceeds) {
    247   DoEnableVertexAttribArray(1);
    248 
    249   EXPECT_CALL(*gl_, DrawArrays(_, _, _))
    250       .Times(0);
    251   DrawArrays cmd;
    252   cmd.Init(GL_TRIANGLES, 0, 0);
    253   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    254   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    255 }
    256 
    257 TEST_F(GLES2DecoderWithShaderTest, DrawArraysValidAttributesSucceeds) {
    258   SetupTexture();
    259   SetupVertexBuffer();
    260   DoEnableVertexAttribArray(1);
    261   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
    262   AddExpectationsForSimulatedAttrib0(kNumVertices, kServiceBufferId);
    263   SetupExpectationsForApplyingDefaultDirtyState();
    264 
    265   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
    266       .Times(1)
    267       .RetiresOnSaturation();
    268   DrawArrays cmd;
    269   cmd.Init(GL_TRIANGLES, 0, kNumVertices);
    270   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    271   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    272 }
    273 
    274 TEST_F(GLES2DecoderWithShaderTest, DrawArraysDeletedBufferFails) {
    275   SetupVertexBuffer();
    276   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
    277   DeleteVertexBuffer();
    278 
    279   EXPECT_CALL(*gl_, DrawArrays(_, _, _))
    280       .Times(0);
    281   DrawArrays cmd;
    282   cmd.Init(GL_TRIANGLES, 0, kNumVertices);
    283   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    284   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
    285 }
    286 
    287 TEST_F(GLES2DecoderWithShaderTest, DrawArraysDeletedProgramSucceeds) {
    288   SetupTexture();
    289   AddExpectationsForSimulatedAttrib0(kNumVertices, 0);
    290   SetupExpectationsForApplyingDefaultDirtyState();
    291   DoDeleteProgram(client_program_id_, kServiceProgramId);
    292 
    293   EXPECT_CALL(*gl_, DrawArrays(_, _, _))
    294       .Times(1)
    295       .RetiresOnSaturation();
    296   EXPECT_CALL(*gl_, DeleteProgram(kServiceProgramId))
    297       .Times(1);
    298   DrawArrays cmd;
    299   cmd.Init(GL_TRIANGLES, 0, kNumVertices);
    300   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    301   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    302 }
    303 
    304 TEST_F(GLES2DecoderWithShaderTest, DrawArraysWithInvalidModeFails) {
    305   SetupVertexBuffer();
    306   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
    307 
    308   EXPECT_CALL(*gl_, DrawArrays(_, _, _))
    309       .Times(0);
    310   DrawArrays cmd;
    311   cmd.Init(GL_QUADS, 0, 1);
    312   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    313   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
    314   cmd.Init(GL_POLYGON, 0, 1);
    315   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    316   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
    317 }
    318 
    319 TEST_F(GLES2DecoderWithShaderTest, DrawArraysInvalidCountFails) {
    320   SetupVertexBuffer();
    321   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
    322 
    323   // Try start > 0
    324   EXPECT_CALL(*gl_, DrawArrays(_, _, _)).Times(0);
    325   DrawArrays cmd;
    326   cmd.Init(GL_TRIANGLES, 1, kNumVertices);
    327   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    328   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
    329   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    330 
    331   // Try with count > size
    332   cmd.Init(GL_TRIANGLES, 0, kNumVertices + 1);
    333   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    334   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
    335   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    336 
    337   // Try with attrib offset > 0
    338   cmd.Init(GL_TRIANGLES, 0, kNumVertices);
    339   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 4);
    340   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    341   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
    342   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    343 
    344   // Try with size > 2 (ie, vec3 instead of vec2)
    345   DoVertexAttribPointer(1, 3, GL_FLOAT, 0, 0);
    346   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    347   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
    348   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    349 
    350   // Try with stride > 8 (vec2 + vec2 byte)
    351   DoVertexAttribPointer(1, 2, GL_FLOAT, sizeof(GLfloat) * 3, 0);
    352   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    353   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
    354   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    355 }
    356 
    357 TEST_F(GLES2DecoderWithShaderTest, DrawArraysInstancedANGLEFails) {
    358   SetupTexture();
    359   SetupVertexBuffer();
    360   DoEnableVertexAttribArray(1);
    361   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
    362 
    363   EXPECT_CALL(*gl_, DrawArraysInstancedANGLE(_, _, _, _))
    364       .Times(0)
    365       .RetiresOnSaturation();
    366   DrawArraysInstancedANGLE cmd;
    367   cmd.Init(GL_TRIANGLES, 0, kNumVertices, 1);
    368   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    369   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
    370 }
    371 
    372 TEST_F(GLES2DecoderGeometryInstancingTest,
    373        DrawArraysInstancedANGLENoAttributesFails) {
    374   SetupTexture();
    375 
    376   EXPECT_CALL(*gl_, DrawArraysInstancedANGLE(_, _, _, _))
    377       .Times(0)
    378       .RetiresOnSaturation();
    379   DrawArraysInstancedANGLE cmd;
    380   cmd.Init(GL_TRIANGLES, 0, kNumVertices, 1);
    381   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    382   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
    383 }
    384 
    385 TEST_F(GLES2DecoderGeometryInstancingTest,
    386        DrawArraysInstancedANGLESimulatedAttrib0) {
    387   SetupTexture();
    388   SetupVertexBuffer();
    389   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
    390 
    391   AddExpectationsForSimulatedAttrib0(kNumVertices, kServiceBufferId);
    392   SetupExpectationsForApplyingDefaultDirtyState();
    393 
    394   DoVertexAttribDivisorANGLE(0, 1);
    395   EXPECT_CALL(*gl_, DrawArraysInstancedANGLE(GL_TRIANGLES, 0, kNumVertices, 3))
    396       .Times(1)
    397       .RetiresOnSaturation();
    398   EXPECT_CALL(*gl_, VertexAttribDivisorANGLE(0, 0))
    399       .Times(1)
    400       .RetiresOnSaturation();
    401   EXPECT_CALL(*gl_, VertexAttribDivisorANGLE(0, 1))
    402       .Times(1)
    403       .RetiresOnSaturation();
    404   DrawArraysInstancedANGLE cmd;
    405   cmd.Init(GL_TRIANGLES, 0, kNumVertices, 3);
    406   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    407   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    408 }
    409 
    410 TEST_F(GLES2DecoderGeometryInstancingTest,
    411        DrawArraysInstancedANGLEMissingAttributesFails) {
    412   DoEnableVertexAttribArray(1);
    413 
    414   EXPECT_CALL(*gl_, DrawArraysInstancedANGLE(_, _, _, _))
    415       .Times(0);
    416   DrawArraysInstancedANGLE cmd;
    417   cmd.Init(GL_TRIANGLES, 0, kNumVertices, 1);
    418   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    419   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
    420 }
    421 
    422 TEST_F(GLES2DecoderGeometryInstancingTest,
    423        DrawArraysInstancedANGLEMissingAttributesZeroCountSucceeds) {
    424   DoEnableVertexAttribArray(1);
    425 
    426   EXPECT_CALL(*gl_, DrawArraysInstancedANGLE(_, _, _, _))
    427       .Times(0);
    428   DrawArraysInstancedANGLE cmd;
    429   cmd.Init(GL_TRIANGLES, 0, 0, 1);
    430   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    431   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    432 }
    433 
    434 TEST_F(GLES2DecoderGeometryInstancingTest,
    435        DrawArraysInstancedANGLEValidAttributesSucceeds) {
    436   SetupTexture();
    437   SetupVertexBuffer();
    438   DoEnableVertexAttribArray(1);
    439   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
    440   AddExpectationsForSimulatedAttrib0(kNumVertices, kServiceBufferId);
    441   SetupExpectationsForApplyingDefaultDirtyState();
    442 
    443   EXPECT_CALL(*gl_, DrawArraysInstancedANGLE(GL_TRIANGLES, 0, kNumVertices, 1))
    444       .Times(1)
    445       .RetiresOnSaturation();
    446   DrawArraysInstancedANGLE cmd;
    447   cmd.Init(GL_TRIANGLES, 0, kNumVertices, 1);
    448   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    449   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    450 }
    451 
    452 TEST_F(GLES2DecoderGeometryInstancingTest,
    453        DrawArraysInstancedANGLEWithInvalidModeFails) {
    454   SetupVertexBuffer();
    455   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
    456 
    457   EXPECT_CALL(*gl_, DrawArraysInstancedANGLE(_, _, _, _))
    458       .Times(0);
    459   DrawArraysInstancedANGLE cmd;
    460   cmd.Init(GL_QUADS, 0, 1, 1);
    461   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    462   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
    463   cmd.Init(GL_POLYGON, 0, 1, 1);
    464   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    465   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
    466 }
    467 
    468 TEST_F(GLES2DecoderGeometryInstancingTest,
    469        DrawArraysInstancedANGLEInvalidPrimcountFails) {
    470   SetupVertexBuffer();
    471   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
    472 
    473   EXPECT_CALL(*gl_, DrawArraysInstancedANGLE(_, _, _, _))
    474       .Times(0);
    475   DrawArraysInstancedANGLE cmd;
    476   cmd.Init(GL_TRIANGLES, 0, 1, -1);
    477   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    478   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
    479 }
    480 
    481 // Per-instance data is twice as large, but number of instances is half
    482 TEST_F(GLES2DecoderGeometryInstancingTest,
    483        DrawArraysInstancedANGLELargeInstanceSucceeds) {
    484   SetupTexture();
    485   SetupVertexBuffer();
    486   SetupExpectationsForApplyingDefaultDirtyState();
    487   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
    488 
    489   DoEnableVertexAttribArray(0);
    490   DoVertexAttribPointer(0, 4, GL_FLOAT, 0, 0);
    491   DoVertexAttribDivisorANGLE(0, 1);
    492   EXPECT_CALL(*gl_, DrawArraysInstancedANGLE(GL_TRIANGLES, 0, kNumVertices,
    493                                              kNumVertices / 2))
    494       .Times(1)
    495       .RetiresOnSaturation();
    496   DrawArraysInstancedANGLE cmd;
    497   cmd.Init(GL_TRIANGLES, 0, kNumVertices, kNumVertices / 2);
    498   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    499   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    500 }
    501 
    502 // Per-instance data is twice as large, but divisor is twice
    503 TEST_F(GLES2DecoderGeometryInstancingTest,
    504        DrawArraysInstancedANGLELargeDivisorSucceeds) {
    505   SetupTexture();
    506   SetupVertexBuffer();
    507   SetupExpectationsForApplyingDefaultDirtyState();
    508   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
    509 
    510   DoEnableVertexAttribArray(0);
    511   DoVertexAttribPointer(0, 4, GL_FLOAT, 0, 0);
    512   DoVertexAttribDivisorANGLE(0, 2);
    513   EXPECT_CALL(*gl_, DrawArraysInstancedANGLE(GL_TRIANGLES, 0, kNumVertices,
    514                                              kNumVertices))
    515       .Times(1)
    516       .RetiresOnSaturation();
    517   DrawArraysInstancedANGLE cmd;
    518   cmd.Init(GL_TRIANGLES, 0, kNumVertices, kNumVertices);
    519   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    520   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    521 }
    522 
    523 TEST_F(GLES2DecoderGeometryInstancingTest, DrawArraysInstancedANGLELargeFails) {
    524   SetupTexture();
    525   SetupVertexBuffer();
    526   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
    527 
    528   DoEnableVertexAttribArray(0);
    529   DoVertexAttribPointer(0, 2, GL_FLOAT, 0, 0);
    530   DoVertexAttribDivisorANGLE(0, 1);
    531   EXPECT_CALL(*gl_, DrawArraysInstancedANGLE(_, _, _, _))
    532       .Times(0)
    533       .RetiresOnSaturation();
    534   DrawArraysInstancedANGLE cmd;
    535   cmd.Init(GL_TRIANGLES, 0, kNumVertices, kNumVertices + 1);
    536   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    537   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
    538   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    539 
    540   EXPECT_CALL(*gl_, DrawArraysInstancedANGLE(_, _, _, _))
    541       .Times(0)
    542       .RetiresOnSaturation();
    543   cmd.Init(GL_TRIANGLES, 0, kNumVertices + 1, kNumVertices);
    544   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    545   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
    546   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    547 }
    548 
    549 // Per-index data is twice as large, but number of indices is half
    550 TEST_F(GLES2DecoderGeometryInstancingTest,
    551        DrawArraysInstancedANGLELargeIndexSucceeds) {
    552   SetupTexture();
    553   SetupVertexBuffer();
    554   SetupExpectationsForApplyingDefaultDirtyState();
    555   DoVertexAttribPointer(1, 4, GL_FLOAT, 0, 0);
    556 
    557   DoEnableVertexAttribArray(0);
    558   DoVertexAttribPointer(0, 2, GL_FLOAT, 0, 0);
    559   DoVertexAttribDivisorANGLE(0, 1);
    560   EXPECT_CALL(*gl_, DrawArraysInstancedANGLE(GL_TRIANGLES, 0, kNumVertices / 2,
    561                                              kNumVertices))
    562       .Times(1)
    563       .RetiresOnSaturation();
    564   DrawArraysInstancedANGLE cmd;
    565   cmd.Init(GL_TRIANGLES, 0, kNumVertices / 2, kNumVertices);
    566   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    567   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    568 }
    569 
    570 TEST_F(GLES2DecoderGeometryInstancingTest,
    571        DrawArraysInstancedANGLENoDivisor0Fails) {
    572   SetupTexture();
    573   SetupVertexBuffer();
    574   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
    575 
    576   DoEnableVertexAttribArray(0);
    577   DoVertexAttribPointer(0, 2, GL_FLOAT, 0, 0);
    578   DoVertexAttribDivisorANGLE(0, 1);
    579   DoVertexAttribDivisorANGLE(1, 1);
    580   EXPECT_CALL(*gl_, DrawArraysInstancedANGLE(_, _, _, _))
    581       .Times(0)
    582       .RetiresOnSaturation();
    583   DrawArraysInstancedANGLE cmd;
    584   cmd.Init(GL_TRIANGLES, 0, kNumVertices, 1);
    585   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    586   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
    587   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    588 }
    589 
    590 TEST_F(GLES2DecoderWithShaderTest, DrawElementsNoAttributesSucceeds) {
    591   SetupTexture();
    592   SetupIndexBuffer();
    593   AddExpectationsForSimulatedAttrib0(kMaxValidIndex + 1, 0);
    594   SetupExpectationsForApplyingDefaultDirtyState();
    595   EXPECT_CALL(*gl_, DrawElements(GL_TRIANGLES, kValidIndexRangeCount,
    596                                  GL_UNSIGNED_SHORT,
    597                                  BufferOffset(kValidIndexRangeStart * 2)))
    598       .Times(1)
    599       .RetiresOnSaturation();
    600   DrawElements cmd;
    601   cmd.Init(GL_TRIANGLES, kValidIndexRangeCount, GL_UNSIGNED_SHORT,
    602            kValidIndexRangeStart * 2);
    603   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    604   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    605 }
    606 
    607 TEST_F(GLES2DecoderWithShaderTest, DrawElementsMissingAttributesFails) {
    608   SetupIndexBuffer();
    609   DoEnableVertexAttribArray(1);
    610 
    611   EXPECT_CALL(*gl_, DrawElements(_, _, _, _))
    612       .Times(0);
    613   DrawElements cmd;
    614   cmd.Init(GL_TRIANGLES, kValidIndexRangeCount, GL_UNSIGNED_SHORT,
    615            kValidIndexRangeStart * 2);
    616   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    617   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
    618 }
    619 
    620 TEST_F(GLES2DecoderWithShaderTest,
    621        DrawElementsMissingAttributesZeroCountSucceeds) {
    622   SetupIndexBuffer();
    623   DoEnableVertexAttribArray(1);
    624 
    625   EXPECT_CALL(*gl_, DrawElements(_, _, _, _))
    626       .Times(0);
    627   DrawElements cmd;
    628   cmd.Init(GL_TRIANGLES, 0, GL_UNSIGNED_SHORT,
    629            kValidIndexRangeStart * 2);
    630   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    631   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    632 }
    633 
    634 TEST_F(GLES2DecoderWithShaderTest, DrawElementsExtraAttributesFails) {
    635   SetupIndexBuffer();
    636   DoEnableVertexAttribArray(6);
    637 
    638   EXPECT_CALL(*gl_, DrawElements(_, _, _, _))
    639       .Times(0);
    640   DrawElements cmd;
    641   cmd.Init(GL_TRIANGLES, kValidIndexRangeCount, GL_UNSIGNED_SHORT,
    642            kValidIndexRangeStart * 2);
    643   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    644   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
    645 }
    646 
    647 TEST_F(GLES2DecoderWithShaderTest, DrawElementsValidAttributesSucceeds) {
    648   SetupTexture();
    649   SetupVertexBuffer();
    650   SetupIndexBuffer();
    651   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
    652   AddExpectationsForSimulatedAttrib0(kMaxValidIndex + 1, kServiceBufferId);
    653   SetupExpectationsForApplyingDefaultDirtyState();
    654 
    655   EXPECT_CALL(*gl_, DrawElements(GL_TRIANGLES, kValidIndexRangeCount,
    656                                  GL_UNSIGNED_SHORT,
    657                                  BufferOffset(kValidIndexRangeStart * 2)))
    658       .Times(1)
    659       .RetiresOnSaturation();
    660   DrawElements cmd;
    661   cmd.Init(GL_TRIANGLES, kValidIndexRangeCount, GL_UNSIGNED_SHORT,
    662            kValidIndexRangeStart * 2);
    663   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    664   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    665 }
    666 
    667 TEST_F(GLES2DecoderWithShaderTest, DrawElementsDeletedBufferFails) {
    668   SetupVertexBuffer();
    669   SetupIndexBuffer();
    670   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
    671   DeleteIndexBuffer();
    672 
    673   EXPECT_CALL(*gl_, DrawElements(_, _, _, _))
    674       .Times(0);
    675   DrawElements cmd;
    676   cmd.Init(GL_TRIANGLES, kValidIndexRangeCount, GL_UNSIGNED_SHORT,
    677            kValidIndexRangeStart * 2);
    678   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    679   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
    680 }
    681 
    682 TEST_F(GLES2DecoderWithShaderTest, DrawElementsDeletedProgramSucceeds) {
    683   SetupTexture();
    684   SetupIndexBuffer();
    685   AddExpectationsForSimulatedAttrib0(kMaxValidIndex + 1, 0);
    686   SetupExpectationsForApplyingDefaultDirtyState();
    687   DoDeleteProgram(client_program_id_, kServiceProgramId);
    688 
    689   EXPECT_CALL(*gl_, DrawElements(_, _, _, _))
    690       .Times(1);
    691   EXPECT_CALL(*gl_, DeleteProgram(kServiceProgramId))
    692       .Times(1);
    693   DrawElements cmd;
    694   cmd.Init(GL_TRIANGLES, kValidIndexRangeCount, GL_UNSIGNED_SHORT,
    695            kValidIndexRangeStart * 2);
    696   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    697   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    698 }
    699 
    700 TEST_F(GLES2DecoderWithShaderTest, DrawElementsWithInvalidModeFails) {
    701   SetupVertexBuffer();
    702   SetupIndexBuffer();
    703   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
    704 
    705   EXPECT_CALL(*gl_, DrawElements(_, _, _, _))
    706       .Times(0);
    707   DrawElements cmd;
    708   cmd.Init(GL_QUADS, kValidIndexRangeCount, GL_UNSIGNED_SHORT,
    709            kValidIndexRangeStart * 2);
    710   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    711   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
    712   cmd.Init(GL_POLYGON, kValidIndexRangeCount, GL_UNSIGNED_SHORT,
    713            kValidIndexRangeStart);
    714   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    715   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
    716 }
    717 
    718 TEST_F(GLES2DecoderWithShaderTest, DrawElementsInvalidCountFails) {
    719   SetupVertexBuffer();
    720   SetupIndexBuffer();
    721   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
    722 
    723   // Try start > 0
    724   EXPECT_CALL(*gl_, DrawElements(_, _, _, _)).Times(0);
    725   DrawElements cmd;
    726   cmd.Init(GL_TRIANGLES, kNumIndices, GL_UNSIGNED_SHORT, 2);
    727   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    728   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
    729   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    730 
    731   // Try with count > size
    732   cmd.Init(GL_TRIANGLES, kNumIndices + 1, GL_UNSIGNED_SHORT, 0);
    733   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    734   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
    735   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    736 }
    737 
    738 TEST_F(GLES2DecoderWithShaderTest, DrawElementsOutOfRangeIndicesFails) {
    739   SetupVertexBuffer();
    740   SetupIndexBuffer();
    741   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
    742 
    743   EXPECT_CALL(*gl_, DrawElements(_, _, _, _)).Times(0);
    744   DrawElements cmd;
    745   cmd.Init(GL_TRIANGLES, kInvalidIndexRangeCount, GL_UNSIGNED_SHORT,
    746            kInvalidIndexRangeStart * 2);
    747   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    748   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
    749   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    750 }
    751 
    752 TEST_F(GLES2DecoderWithShaderTest, DrawElementsOddOffsetForUint16Fails) {
    753   SetupVertexBuffer();
    754   SetupIndexBuffer();
    755   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
    756 
    757   EXPECT_CALL(*gl_, DrawElements(_, _, _, _)).Times(0);
    758   DrawElements cmd;
    759   cmd.Init(GL_TRIANGLES, kInvalidIndexRangeCount, GL_UNSIGNED_SHORT, 1);
    760   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    761   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
    762   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    763 }
    764 
    765 TEST_F(GLES2DecoderWithShaderTest, DrawElementsInstancedANGLEFails) {
    766   SetupTexture();
    767   SetupVertexBuffer();
    768   SetupIndexBuffer();
    769   DoEnableVertexAttribArray(1);
    770   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
    771 
    772   EXPECT_CALL(*gl_, DrawElementsInstancedANGLE(_, _, _, _, _))
    773       .Times(0)
    774       .RetiresOnSaturation();
    775   DrawElementsInstancedANGLE cmd;
    776   cmd.Init(GL_TRIANGLES, kValidIndexRangeCount, GL_UNSIGNED_SHORT,
    777            kValidIndexRangeStart * 2, 1);
    778   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    779   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
    780 }
    781 
    782 TEST_F(GLES2DecoderGeometryInstancingTest,
    783        DrawElementsInstancedANGLENoAttributesFails) {
    784   SetupTexture();
    785   SetupIndexBuffer();
    786 
    787   EXPECT_CALL(*gl_, DrawElementsInstancedANGLE(_, _, _, _, _))
    788       .Times(0)
    789       .RetiresOnSaturation();
    790   DrawElementsInstancedANGLE cmd;
    791   cmd.Init(GL_TRIANGLES, kValidIndexRangeCount, GL_UNSIGNED_SHORT,
    792            kValidIndexRangeStart * 2, 1);
    793   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    794   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
    795 }
    796 
    797 TEST_F(GLES2DecoderGeometryInstancingTest,
    798        DrawElementsInstancedANGLESimulatedAttrib0) {
    799   SetupTexture();
    800   SetupVertexBuffer();
    801   SetupIndexBuffer();
    802   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
    803 
    804   AddExpectationsForSimulatedAttrib0(kMaxValidIndex + 1, kServiceBufferId);
    805   SetupExpectationsForApplyingDefaultDirtyState();
    806 
    807   DoVertexAttribDivisorANGLE(0, 1);
    808   EXPECT_CALL(*gl_, DrawElementsInstancedANGLE(
    809                         GL_TRIANGLES,
    810                         kValidIndexRangeCount,
    811                         GL_UNSIGNED_SHORT,
    812                         BufferOffset(kValidIndexRangeStart * 2),
    813                         3))
    814       .Times(1)
    815       .RetiresOnSaturation();
    816   EXPECT_CALL(*gl_, VertexAttribDivisorANGLE(0, 0))
    817       .Times(1)
    818       .RetiresOnSaturation();
    819   EXPECT_CALL(*gl_, VertexAttribDivisorANGLE(0, 1))
    820       .Times(1)
    821       .RetiresOnSaturation();
    822   DrawElementsInstancedANGLE cmd;
    823   cmd.Init(GL_TRIANGLES, kValidIndexRangeCount, GL_UNSIGNED_SHORT,
    824            kValidIndexRangeStart * 2, 3);
    825   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    826   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    827 }
    828 
    829 TEST_F(GLES2DecoderGeometryInstancingTest,
    830        DrawElementsInstancedANGLEMissingAttributesFails) {
    831   SetupIndexBuffer();
    832   DoEnableVertexAttribArray(1);
    833 
    834   EXPECT_CALL(*gl_, DrawElementsInstancedANGLE(_, _, _, _, _))
    835       .Times(0);
    836   DrawElementsInstancedANGLE cmd;
    837   cmd.Init(GL_TRIANGLES, kValidIndexRangeCount, GL_UNSIGNED_SHORT,
    838            kValidIndexRangeStart * 2, 1);
    839   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    840   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
    841 }
    842 
    843 TEST_F(GLES2DecoderGeometryInstancingTest,
    844        DrawElementsInstancedANGLEMissingAttributesZeroCountSucceeds) {
    845   SetupIndexBuffer();
    846   DoEnableVertexAttribArray(1);
    847 
    848   EXPECT_CALL(*gl_, DrawElementsInstancedANGLE(_, _, _, _, _))
    849       .Times(0);
    850   DrawElementsInstancedANGLE cmd;
    851   cmd.Init(GL_TRIANGLES, 0, GL_UNSIGNED_SHORT,
    852            kValidIndexRangeStart * 2, 1);
    853   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    854   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    855 }
    856 
    857 TEST_F(GLES2DecoderGeometryInstancingTest,
    858        DrawElementsInstancedANGLEValidAttributesSucceeds) {
    859   SetupIndexBuffer();
    860   SetupTexture();
    861   SetupVertexBuffer();
    862   DoEnableVertexAttribArray(1);
    863   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
    864   AddExpectationsForSimulatedAttrib0(kMaxValidIndex + 1, kServiceBufferId);
    865   SetupExpectationsForApplyingDefaultDirtyState();
    866 
    867   EXPECT_CALL(*gl_, DrawElementsInstancedANGLE(
    868                         GL_TRIANGLES,
    869                         kValidIndexRangeCount,
    870                         GL_UNSIGNED_SHORT,
    871                         BufferOffset(kValidIndexRangeStart * 2),
    872                         1))
    873       .Times(1)
    874       .RetiresOnSaturation();
    875   DrawElementsInstancedANGLE cmd;
    876   cmd.Init(GL_TRIANGLES, kValidIndexRangeCount, GL_UNSIGNED_SHORT,
    877            kValidIndexRangeStart * 2, 1);
    878   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    879   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    880 }
    881 
    882 TEST_F(GLES2DecoderGeometryInstancingTest,
    883        DrawElementsInstancedANGLEWithInvalidModeFails) {
    884   SetupIndexBuffer();
    885   SetupVertexBuffer();
    886   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
    887 
    888   EXPECT_CALL(*gl_, DrawElementsInstancedANGLE(_, _, _, _, _))
    889       .Times(0);
    890   DrawElementsInstancedANGLE cmd;
    891   cmd.Init(GL_QUADS, kValidIndexRangeCount, GL_UNSIGNED_SHORT,
    892            kValidIndexRangeStart * 2, 1);
    893   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    894   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
    895   cmd.Init(GL_INVALID_ENUM, kValidIndexRangeCount, GL_UNSIGNED_SHORT,
    896            kValidIndexRangeStart * 2, 1);
    897   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    898   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
    899 }
    900 
    901 // Per-instance data is twice as large, but number of instances is half
    902 TEST_F(GLES2DecoderGeometryInstancingTest,
    903        DrawElementsInstancedANGLELargeInstanceSucceeds) {
    904   SetupTexture();
    905   SetupIndexBuffer();
    906   SetupVertexBuffer();
    907   SetupExpectationsForApplyingDefaultDirtyState();
    908   //Add offset so we're sure we're accessing data near the end of the buffer.
    909   DoVertexAttribPointer(1, 2, GL_FLOAT, 0,
    910                         (kNumVertices - kMaxValidIndex - 1) * 2 *
    911                             sizeof(GLfloat));
    912 
    913   DoEnableVertexAttribArray(0);
    914   DoVertexAttribPointer(0, 4, GL_FLOAT, 0, 0);
    915   DoVertexAttribDivisorANGLE(0, 1);
    916   EXPECT_CALL(*gl_, DrawElementsInstancedANGLE(
    917                         GL_TRIANGLES,
    918                         kValidIndexRangeCount,
    919                         GL_UNSIGNED_SHORT,
    920                         BufferOffset(kValidIndexRangeStart * 2),
    921                         kNumVertices / 2))
    922       .Times(1)
    923       .RetiresOnSaturation();
    924   DrawElementsInstancedANGLE cmd;
    925   cmd.Init(GL_TRIANGLES, kValidIndexRangeCount, GL_UNSIGNED_SHORT,
    926            kValidIndexRangeStart * 2, kNumVertices / 2);
    927   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    928   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    929 }
    930 
    931 // Per-instance data is twice as large, but divisor is twice
    932 TEST_F(GLES2DecoderGeometryInstancingTest,
    933        DrawElementsInstancedANGLELargeDivisorSucceeds) {
    934   SetupTexture();
    935   SetupIndexBuffer();
    936   SetupVertexBuffer();
    937   SetupExpectationsForApplyingDefaultDirtyState();
    938   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
    939 
    940   DoEnableVertexAttribArray(0);
    941   DoVertexAttribPointer(0, 4, GL_FLOAT, 0, 0);
    942   DoVertexAttribDivisorANGLE(0, 2);
    943   EXPECT_CALL(*gl_, DrawElementsInstancedANGLE(
    944                         GL_TRIANGLES,
    945                         kValidIndexRangeCount,
    946                         GL_UNSIGNED_SHORT,
    947                         BufferOffset(kValidIndexRangeStart * 2),
    948                         kNumVertices))
    949       .Times(1)
    950       .RetiresOnSaturation();
    951   DrawElementsInstancedANGLE cmd;
    952   cmd.Init(GL_TRIANGLES, kValidIndexRangeCount, GL_UNSIGNED_SHORT,
    953            kValidIndexRangeStart * 2, kNumVertices);
    954   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    955   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    956 }
    957 
    958 TEST_F(GLES2DecoderGeometryInstancingTest,
    959        DrawElementsInstancedANGLELargeFails) {
    960   SetupTexture();
    961   SetupIndexBuffer();
    962   SetupVertexBuffer();
    963   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
    964 
    965   DoEnableVertexAttribArray(0);
    966   DoVertexAttribPointer(0, 2, GL_FLOAT, 0, 0);
    967   DoVertexAttribDivisorANGLE(0, 1);
    968   EXPECT_CALL(*gl_, DrawElementsInstancedANGLE(_, _, _, _, _))
    969       .Times(0)
    970       .RetiresOnSaturation();
    971   DrawElementsInstancedANGLE cmd;
    972   cmd.Init(GL_TRIANGLES, kValidIndexRangeCount, GL_UNSIGNED_SHORT,
    973            kValidIndexRangeStart * 2, kNumVertices + 1);
    974   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    975   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
    976   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    977 
    978   EXPECT_CALL(*gl_, DrawElementsInstancedANGLE(_, _, _, _, _))
    979       .Times(0)
    980       .RetiresOnSaturation();
    981   cmd.Init(GL_TRIANGLES, kInvalidIndexRangeCount, GL_UNSIGNED_SHORT,
    982            kInvalidIndexRangeStart * 2, kNumVertices);
    983   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    984   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
    985   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    986 }
    987 
    988 TEST_F(GLES2DecoderGeometryInstancingTest,
    989        DrawElementsInstancedANGLEInvalidPrimcountFails) {
    990   SetupTexture();
    991   SetupIndexBuffer();
    992   SetupVertexBuffer();
    993   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
    994 
    995   DoEnableVertexAttribArray(0);
    996   DoVertexAttribPointer(0, 2, GL_FLOAT, 0, 0);
    997   DoVertexAttribDivisorANGLE(0, 1);
    998   EXPECT_CALL(*gl_, DrawElementsInstancedANGLE(_, _, _, _, _))
    999       .Times(0)
   1000       .RetiresOnSaturation();
   1001   DrawElementsInstancedANGLE cmd;
   1002   cmd.Init(GL_TRIANGLES, kValidIndexRangeCount, GL_UNSIGNED_SHORT,
   1003            kValidIndexRangeStart * 2, -1);
   1004   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1005   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   1006   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1007 }
   1008 
   1009 // Per-index data is twice as large, but values of indices are smaller
   1010 TEST_F(GLES2DecoderGeometryInstancingTest,
   1011        DrawElementsInstancedANGLELargeIndexSucceeds) {
   1012   SetupTexture();
   1013   SetupIndexBuffer();
   1014   SetupVertexBuffer();
   1015   SetupExpectationsForApplyingDefaultDirtyState();
   1016   DoVertexAttribPointer(1, 4, GL_FLOAT, 0, 0);
   1017 
   1018   DoEnableVertexAttribArray(0);
   1019   DoVertexAttribPointer(0, 2, GL_FLOAT, 0, 0);
   1020   DoVertexAttribDivisorANGLE(0, 1);
   1021   EXPECT_CALL(*gl_, DrawElementsInstancedANGLE(
   1022                         GL_TRIANGLES,
   1023                         kValidIndexRangeCount,
   1024                         GL_UNSIGNED_SHORT,
   1025                         BufferOffset(kValidIndexRangeStart * 2),
   1026                         kNumVertices))
   1027       .Times(1)
   1028       .RetiresOnSaturation();
   1029   DrawElementsInstancedANGLE cmd;
   1030   cmd.Init(GL_TRIANGLES, kValidIndexRangeCount, GL_UNSIGNED_SHORT,
   1031            kValidIndexRangeStart * 2, kNumVertices);
   1032   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1033   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1034 }
   1035 
   1036 TEST_F(GLES2DecoderGeometryInstancingTest,
   1037        DrawElementsInstancedANGLENoDivisor0Fails) {
   1038   SetupTexture();
   1039   SetupIndexBuffer();
   1040   SetupVertexBuffer();
   1041   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
   1042 
   1043   DoEnableVertexAttribArray(0);
   1044   DoVertexAttribPointer(0, 2, GL_FLOAT, 0, 0);
   1045   DoVertexAttribDivisorANGLE(0, 1);
   1046   DoVertexAttribDivisorANGLE(1, 1);
   1047   EXPECT_CALL(*gl_, DrawElementsInstancedANGLE(_, _, _, _, _))
   1048       .Times(0)
   1049       .RetiresOnSaturation();
   1050   DrawElementsInstancedANGLE cmd;
   1051   cmd.Init(GL_TRIANGLES, kValidIndexRangeCount, GL_UNSIGNED_SHORT,
   1052            kValidIndexRangeStart * 2, kNumVertices);
   1053   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1054   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   1055   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1056 }
   1057 
   1058 TEST_F(GLES2DecoderWithShaderTest, GetVertexAttribPointervSucceeds) {
   1059   const float dummy = 0;
   1060   const GLuint kOffsetToTestFor = sizeof(dummy) * 4;
   1061   const GLuint kIndexToTest = 1;
   1062   GetVertexAttribPointerv::Result* result =
   1063       static_cast<GetVertexAttribPointerv::Result*>(shared_memory_address_);
   1064   result->size = 0;
   1065   const GLuint* result_value = result->GetData();
   1066   // Test that initial value is 0.
   1067   GetVertexAttribPointerv cmd;
   1068   cmd.Init(kIndexToTest, GL_VERTEX_ATTRIB_ARRAY_POINTER,
   1069            shared_memory_id_, shared_memory_offset_);
   1070   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1071   EXPECT_EQ(sizeof(*result_value), result->size);
   1072   EXPECT_EQ(0u, *result_value);
   1073   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1074 
   1075   // Set the value and see that we get it.
   1076   SetupVertexBuffer();
   1077   DoVertexAttribPointer(kIndexToTest, 2, GL_FLOAT, 0, kOffsetToTestFor);
   1078   result->size = 0;
   1079   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1080   EXPECT_EQ(sizeof(*result_value), result->size);
   1081   EXPECT_EQ(kOffsetToTestFor, *result_value);
   1082   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1083 }
   1084 
   1085 TEST_F(GLES2DecoderWithShaderTest, GetVertexAttribPointervBadArgsFails) {
   1086   const GLuint kIndexToTest = 1;
   1087   GetVertexAttribPointerv::Result* result =
   1088       static_cast<GetVertexAttribPointerv::Result*>(shared_memory_address_);
   1089   result->size = 0;
   1090   const GLuint* result_value = result->GetData();
   1091   // Test pname invalid fails.
   1092   GetVertexAttribPointerv cmd;
   1093   cmd.Init(kIndexToTest, GL_VERTEX_ATTRIB_ARRAY_POINTER + 1,
   1094            shared_memory_id_, shared_memory_offset_);
   1095   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1096   EXPECT_EQ(0u, result->size);
   1097   EXPECT_EQ(kInitialResult, *result_value);
   1098   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
   1099 
   1100   // Test index out of range fails.
   1101   result->size = 0;
   1102   cmd.Init(kNumVertexAttribs, GL_VERTEX_ATTRIB_ARRAY_POINTER,
   1103            shared_memory_id_, shared_memory_offset_);
   1104   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1105   EXPECT_EQ(0u, result->size);
   1106   EXPECT_EQ(kInitialResult, *result_value);
   1107   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   1108 
   1109   // Test memory id bad fails.
   1110   cmd.Init(kIndexToTest, GL_VERTEX_ATTRIB_ARRAY_POINTER,
   1111            kInvalidSharedMemoryId, shared_memory_offset_);
   1112   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   1113 
   1114   // Test memory offset bad fails.
   1115   cmd.Init(kIndexToTest, GL_VERTEX_ATTRIB_ARRAY_POINTER,
   1116            shared_memory_id_, kInvalidSharedMemoryOffset);
   1117   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   1118 }
   1119 
   1120 TEST_F(GLES2DecoderWithShaderTest, GetUniformivSucceeds) {
   1121   GetUniformiv::Result* result =
   1122       static_cast<GetUniformiv::Result*>(shared_memory_address_);
   1123   result->size = 0;
   1124   GetUniformiv cmd;
   1125   cmd.Init(client_program_id_,
   1126            kUniform2FakeLocation,
   1127            kSharedMemoryId, kSharedMemoryOffset);
   1128   EXPECT_CALL(*gl_, GetUniformiv(kServiceProgramId, kUniform2RealLocation, _))
   1129       .Times(1);
   1130   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1131   EXPECT_EQ(GLES2Util::GetGLDataTypeSizeForUniforms(kUniform2Type),
   1132             result->size);
   1133 }
   1134 
   1135 TEST_F(GLES2DecoderWithShaderTest, GetUniformivArrayElementSucceeds) {
   1136   GetUniformiv::Result* result =
   1137       static_cast<GetUniformiv::Result*>(shared_memory_address_);
   1138   result->size = 0;
   1139   GetUniformiv cmd;
   1140   cmd.Init(client_program_id_,
   1141            kUniform2ElementFakeLocation,
   1142            kSharedMemoryId, kSharedMemoryOffset);
   1143   EXPECT_CALL(*gl_,
   1144               GetUniformiv(kServiceProgramId, kUniform2ElementRealLocation, _))
   1145       .Times(1);
   1146   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1147   EXPECT_EQ(GLES2Util::GetGLDataTypeSizeForUniforms(kUniform2Type),
   1148             result->size);
   1149 }
   1150 
   1151 TEST_F(GLES2DecoderWithShaderTest, GetUniformivBadProgramFails) {
   1152   GetUniformiv::Result* result =
   1153       static_cast<GetUniformiv::Result*>(shared_memory_address_);
   1154   result->size = 0;
   1155   GetUniformiv cmd;
   1156   // non-existant program
   1157   cmd.Init(kInvalidClientId,
   1158            kUniform2FakeLocation,
   1159            kSharedMemoryId, kSharedMemoryOffset);
   1160   EXPECT_CALL(*gl_, GetUniformiv(_, _, _))
   1161       .Times(0);
   1162   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1163   EXPECT_EQ(0U, result->size);
   1164   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   1165   // Valid id that is not a program. The GL spec requires a different error for
   1166   // this case.
   1167 #if GLES2_TEST_SHADER_VS_PROGRAM_IDS
   1168   result->size = kInitialResult;
   1169   cmd.Init(client_shader_id_,
   1170            kUniform2FakeLocation,
   1171            kSharedMemoryId, kSharedMemoryOffset);
   1172   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1173   EXPECT_EQ(0U, result->size);
   1174   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   1175 #endif  // GLES2_TEST_SHADER_VS_PROGRAM_IDS
   1176   // Unlinked program
   1177   EXPECT_CALL(*gl_, CreateProgram())
   1178       .Times(1)
   1179       .WillOnce(Return(kNewServiceId))
   1180       .RetiresOnSaturation();
   1181   CreateProgram cmd2;
   1182   cmd2.Init(kNewClientId);
   1183   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd2));
   1184   result->size = kInitialResult;
   1185   cmd.Init(kNewClientId,
   1186            kUniform2FakeLocation,
   1187            kSharedMemoryId, kSharedMemoryOffset);
   1188   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1189   EXPECT_EQ(0U, result->size);
   1190   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   1191 }
   1192 
   1193 TEST_F(GLES2DecoderWithShaderTest, GetUniformivBadLocationFails) {
   1194   GetUniformiv::Result* result =
   1195       static_cast<GetUniformiv::Result*>(shared_memory_address_);
   1196   result->size = 0;
   1197   GetUniformiv cmd;
   1198   // invalid location
   1199   cmd.Init(client_program_id_, kInvalidUniformLocation,
   1200            kSharedMemoryId, kSharedMemoryOffset);
   1201   EXPECT_CALL(*gl_, GetUniformiv(_, _, _))
   1202       .Times(0);
   1203   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1204   EXPECT_EQ(0U, result->size);
   1205   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   1206 }
   1207 
   1208 TEST_F(GLES2DecoderWithShaderTest, GetUniformivBadSharedMemoryFails) {
   1209   GetUniformiv cmd;
   1210   cmd.Init(client_program_id_,
   1211            kUniform2FakeLocation,
   1212            kInvalidSharedMemoryId, kSharedMemoryOffset);
   1213   EXPECT_CALL(*gl_, GetUniformiv(_, _, _))
   1214       .Times(0);
   1215   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   1216   cmd.Init(client_program_id_, kUniform2FakeLocation,
   1217            kSharedMemoryId, kInvalidSharedMemoryOffset);
   1218   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   1219 };
   1220 
   1221 TEST_F(GLES2DecoderWithShaderTest, GetUniformfvSucceeds) {
   1222   GetUniformfv::Result* result =
   1223       static_cast<GetUniformfv::Result*>(shared_memory_address_);
   1224   result->size = 0;
   1225   GetUniformfv cmd;
   1226   cmd.Init(client_program_id_,
   1227            kUniform2FakeLocation,
   1228            kSharedMemoryId, kSharedMemoryOffset);
   1229   EXPECT_CALL(*gl_, GetUniformfv(kServiceProgramId, kUniform2RealLocation, _))
   1230       .Times(1);
   1231   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1232   EXPECT_EQ(GLES2Util::GetGLDataTypeSizeForUniforms(kUniform2Type),
   1233             result->size);
   1234 }
   1235 
   1236 TEST_F(GLES2DecoderWithShaderTest, GetUniformfvArrayElementSucceeds) {
   1237   GetUniformfv::Result* result =
   1238       static_cast<GetUniformfv::Result*>(shared_memory_address_);
   1239   result->size = 0;
   1240   GetUniformfv cmd;
   1241   cmd.Init(client_program_id_,
   1242            kUniform2ElementFakeLocation,
   1243            kSharedMemoryId, kSharedMemoryOffset);
   1244   EXPECT_CALL(*gl_,
   1245               GetUniformfv(kServiceProgramId, kUniform2ElementRealLocation, _))
   1246       .Times(1);
   1247   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1248   EXPECT_EQ(GLES2Util::GetGLDataTypeSizeForUniforms(kUniform2Type),
   1249             result->size);
   1250 }
   1251 
   1252 TEST_F(GLES2DecoderWithShaderTest, GetUniformfvBadProgramFails) {
   1253   GetUniformfv::Result* result =
   1254       static_cast<GetUniformfv::Result*>(shared_memory_address_);
   1255   result->size = 0;
   1256   GetUniformfv cmd;
   1257   // non-existant program
   1258   cmd.Init(kInvalidClientId,
   1259            kUniform2FakeLocation,
   1260            kSharedMemoryId, kSharedMemoryOffset);
   1261   EXPECT_CALL(*gl_, GetUniformfv(_, _, _))
   1262       .Times(0);
   1263   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1264   EXPECT_EQ(0U, result->size);
   1265   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   1266   // Valid id that is not a program. The GL spec requires a different error for
   1267   // this case.
   1268 #if GLES2_TEST_SHADER_VS_PROGRAM_IDS
   1269   result->size = kInitialResult;
   1270   cmd.Init(client_shader_id_,
   1271            kUniform2FakeLocation,
   1272            kSharedMemoryId, kSharedMemoryOffset);
   1273   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1274   EXPECT_EQ(0U, result->size);
   1275   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   1276 #endif  // GLES2_TEST_SHADER_VS_PROGRAM_IDS
   1277   // Unlinked program
   1278   EXPECT_CALL(*gl_, CreateProgram())
   1279       .Times(1)
   1280       .WillOnce(Return(kNewServiceId))
   1281       .RetiresOnSaturation();
   1282   CreateProgram cmd2;
   1283   cmd2.Init(kNewClientId);
   1284   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd2));
   1285   result->size = kInitialResult;
   1286   cmd.Init(kNewClientId,
   1287            kUniform2FakeLocation,
   1288            kSharedMemoryId, kSharedMemoryOffset);
   1289   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1290   EXPECT_EQ(0U, result->size);
   1291   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   1292 }
   1293 
   1294 TEST_F(GLES2DecoderWithShaderTest, GetUniformfvBadLocationFails) {
   1295   GetUniformfv::Result* result =
   1296       static_cast<GetUniformfv::Result*>(shared_memory_address_);
   1297   result->size = 0;
   1298   GetUniformfv cmd;
   1299   // invalid location
   1300   cmd.Init(client_program_id_, kInvalidUniformLocation,
   1301            kSharedMemoryId, kSharedMemoryOffset);
   1302   EXPECT_CALL(*gl_, GetUniformfv(_, _, _))
   1303       .Times(0);
   1304   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1305   EXPECT_EQ(0U, result->size);
   1306   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   1307 }
   1308 
   1309 TEST_F(GLES2DecoderWithShaderTest, GetUniformfvBadSharedMemoryFails) {
   1310   GetUniformfv cmd;
   1311   cmd.Init(client_program_id_,
   1312            kUniform2FakeLocation,
   1313            kInvalidSharedMemoryId, kSharedMemoryOffset);
   1314   EXPECT_CALL(*gl_, GetUniformfv(_, _, _))
   1315       .Times(0);
   1316   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   1317   cmd.Init(client_program_id_, kUniform2FakeLocation,
   1318            kSharedMemoryId, kInvalidSharedMemoryOffset);
   1319   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   1320 };
   1321 
   1322 TEST_F(GLES2DecoderWithShaderTest, GetAttachedShadersSucceeds) {
   1323   GetAttachedShaders cmd;
   1324   typedef GetAttachedShaders::Result Result;
   1325   Result* result = static_cast<Result*>(shared_memory_address_);
   1326   result->size = 0;
   1327   EXPECT_CALL(*gl_, GetAttachedShaders(kServiceProgramId, 1, _, _))
   1328       .WillOnce(DoAll(SetArgumentPointee<2>(1),
   1329                       SetArgumentPointee<3>(kServiceShaderId)));
   1330   cmd.Init(client_program_id_, shared_memory_id_, shared_memory_offset_,
   1331            Result::ComputeSize(1));
   1332   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1333   EXPECT_EQ(1, result->GetNumResults());
   1334   EXPECT_EQ(client_shader_id_, result->GetData()[0]);
   1335   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1336 }
   1337 
   1338 TEST_F(GLES2DecoderWithShaderTest, GetAttachedShadersResultNotInitFail) {
   1339   GetAttachedShaders cmd;
   1340   typedef GetAttachedShaders::Result Result;
   1341   Result* result = static_cast<Result*>(shared_memory_address_);
   1342   result->size = 1;
   1343   EXPECT_CALL(*gl_, GetAttachedShaders(_, _, _, _))
   1344       .Times(0);
   1345   cmd.Init(client_program_id_, shared_memory_id_, shared_memory_offset_,
   1346            Result::ComputeSize(1));
   1347   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   1348 }
   1349 
   1350 TEST_F(GLES2DecoderWithShaderTest, GetAttachedShadersBadProgramFails) {
   1351   GetAttachedShaders cmd;
   1352   typedef GetAttachedShaders::Result Result;
   1353   Result* result = static_cast<Result*>(shared_memory_address_);
   1354   result->size = 0;
   1355   EXPECT_CALL(*gl_, GetAttachedShaders(_, _, _, _))
   1356       .Times(0);
   1357   cmd.Init(kInvalidClientId, shared_memory_id_, shared_memory_offset_,
   1358            Result::ComputeSize(1));
   1359   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1360   EXPECT_EQ(0U, result->size);
   1361   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   1362 }
   1363 
   1364 TEST_F(GLES2DecoderWithShaderTest, GetAttachedShadersBadSharedMemoryFails) {
   1365   GetAttachedShaders cmd;
   1366   typedef GetAttachedShaders::Result Result;
   1367   cmd.Init(client_program_id_, kInvalidSharedMemoryId, shared_memory_offset_,
   1368            Result::ComputeSize(1));
   1369   EXPECT_CALL(*gl_, GetAttachedShaders(_, _, _, _))
   1370       .Times(0);
   1371   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   1372   cmd.Init(client_program_id_, shared_memory_id_, kInvalidSharedMemoryOffset,
   1373            Result::ComputeSize(1));
   1374   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   1375 }
   1376 
   1377 TEST_F(GLES2DecoderWithShaderTest, GetShaderPrecisionFormatSucceeds) {
   1378   ScopedGLImplementationSetter gl_impl(::gfx::kGLImplementationEGLGLES2);
   1379   GetShaderPrecisionFormat cmd;
   1380   typedef GetShaderPrecisionFormat::Result Result;
   1381   Result* result = static_cast<Result*>(shared_memory_address_);
   1382   result->success = 0;
   1383   const GLint range[2] = { 62, 62 };
   1384   const GLint precision = 16;
   1385   EXPECT_CALL(*gl_,GetShaderPrecisionFormat(_, _, _, _))
   1386       .WillOnce(DoAll(SetArrayArgument<2>(range,range+2),
   1387                       SetArgumentPointee<3>(precision)))
   1388       .RetiresOnSaturation();
   1389   cmd.Init(GL_VERTEX_SHADER, GL_HIGH_FLOAT,
   1390            shared_memory_id_, shared_memory_offset_);
   1391   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1392   EXPECT_NE(0, result->success);
   1393   EXPECT_EQ(range[0], result->min_range);
   1394   EXPECT_EQ(range[1], result->max_range);
   1395   EXPECT_EQ(precision, result->precision);
   1396   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1397 }
   1398 
   1399 TEST_F(GLES2DecoderWithShaderTest, GetShaderPrecisionFormatResultNotInitFails) {
   1400   GetShaderPrecisionFormat cmd;
   1401   typedef GetShaderPrecisionFormat::Result Result;
   1402   Result* result = static_cast<Result*>(shared_memory_address_);
   1403   result->success = 1;
   1404   // NOTE: GL might not be called. There is no Desktop OpenGL equivalent
   1405   cmd.Init(GL_VERTEX_SHADER, GL_HIGH_FLOAT,
   1406            shared_memory_id_, shared_memory_offset_);
   1407   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   1408 }
   1409 
   1410 TEST_F(GLES2DecoderWithShaderTest, GetShaderPrecisionFormatBadArgsFails) {
   1411   typedef GetShaderPrecisionFormat::Result Result;
   1412   Result* result = static_cast<Result*>(shared_memory_address_);
   1413   result->success = 0;
   1414   GetShaderPrecisionFormat cmd;
   1415   cmd.Init(GL_TEXTURE_2D, GL_HIGH_FLOAT,
   1416            shared_memory_id_, shared_memory_offset_);
   1417   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1418   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
   1419   result->success = 0;
   1420   cmd.Init(GL_VERTEX_SHADER, GL_TEXTURE_2D,
   1421            shared_memory_id_, shared_memory_offset_);
   1422   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1423   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
   1424 }
   1425 
   1426 TEST_F(GLES2DecoderWithShaderTest,
   1427        GetShaderPrecisionFormatBadSharedMemoryFails) {
   1428   GetShaderPrecisionFormat cmd;
   1429   cmd.Init(GL_VERTEX_SHADER, GL_HIGH_FLOAT,
   1430            kInvalidSharedMemoryId, shared_memory_offset_);
   1431   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   1432   cmd.Init(GL_VERTEX_SHADER, GL_TEXTURE_2D,
   1433            shared_memory_id_, kInvalidSharedMemoryOffset);
   1434   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   1435 }
   1436 
   1437 TEST_F(GLES2DecoderWithShaderTest, GetActiveUniformSucceeds) {
   1438   const GLuint kUniformIndex = 1;
   1439   const uint32 kBucketId = 123;
   1440   GetActiveUniform cmd;
   1441   typedef GetActiveUniform::Result Result;
   1442   Result* result = static_cast<Result*>(shared_memory_address_);
   1443   result->success = 0;
   1444   cmd.Init(client_program_id_, kUniformIndex, kBucketId,
   1445            shared_memory_id_, shared_memory_offset_);
   1446   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1447   EXPECT_NE(0, result->success);
   1448   EXPECT_EQ(kUniform2Size, result->size);
   1449   EXPECT_EQ(kUniform2Type, result->type);
   1450   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1451   CommonDecoder::Bucket* bucket = decoder_->GetBucket(kBucketId);
   1452   ASSERT_TRUE(bucket != NULL);
   1453   EXPECT_EQ(0, memcmp(bucket->GetData(0, bucket->size()), kUniform2Name,
   1454                       bucket->size()));
   1455 }
   1456 
   1457 TEST_F(GLES2DecoderWithShaderTest, GetActiveUniformResultNotInitFails) {
   1458   const GLuint kUniformIndex = 1;
   1459   const uint32 kBucketId = 123;
   1460   GetActiveUniform cmd;
   1461   typedef GetActiveUniform::Result Result;
   1462   Result* result = static_cast<Result*>(shared_memory_address_);
   1463   result->success = 1;
   1464   cmd.Init(client_program_id_, kUniformIndex, kBucketId,
   1465            shared_memory_id_, shared_memory_offset_);
   1466   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   1467 }
   1468 
   1469 TEST_F(GLES2DecoderWithShaderTest, GetActiveUniformBadProgramFails) {
   1470   const GLuint kUniformIndex = 1;
   1471   const uint32 kBucketId = 123;
   1472   GetActiveUniform cmd;
   1473   typedef GetActiveUniform::Result Result;
   1474   Result* result = static_cast<Result*>(shared_memory_address_);
   1475   result->success = 0;
   1476   cmd.Init(kInvalidClientId, kUniformIndex, kBucketId,
   1477            shared_memory_id_, shared_memory_offset_);
   1478   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1479   EXPECT_EQ(0, result->success);
   1480   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   1481 #if GLES2_TEST_SHADER_VS_PROGRAM_IDS
   1482   result->success = 0;
   1483   cmd.Init(client_shader_id_, kUniformIndex, kBucketId,
   1484            shared_memory_id_, shared_memory_offset_);
   1485   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1486   EXPECT_EQ(0, result->success);
   1487   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   1488 #endif  // GLES2_TEST_SHADER_VS_PROGRAM_IDS
   1489 }
   1490 
   1491 TEST_F(GLES2DecoderWithShaderTest, GetActiveUniformBadIndexFails) {
   1492   const uint32 kBucketId = 123;
   1493   GetActiveUniform cmd;
   1494   typedef GetActiveUniform::Result Result;
   1495   Result* result = static_cast<Result*>(shared_memory_address_);
   1496   result->success = 0;
   1497   cmd.Init(client_program_id_, kBadUniformIndex, kBucketId,
   1498            shared_memory_id_, shared_memory_offset_);
   1499   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1500   EXPECT_EQ(0, result->success);
   1501   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   1502 }
   1503 
   1504 TEST_F(GLES2DecoderWithShaderTest, GetActiveUniformBadSharedMemoryFails) {
   1505   const GLuint kUniformIndex = 1;
   1506   const uint32 kBucketId = 123;
   1507   GetActiveUniform cmd;
   1508   typedef GetActiveUniform::Result Result;
   1509   cmd.Init(client_program_id_, kUniformIndex, kBucketId,
   1510            kInvalidSharedMemoryId, shared_memory_offset_);
   1511   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   1512   cmd.Init(client_program_id_, kUniformIndex, kBucketId,
   1513            shared_memory_id_, kInvalidSharedMemoryOffset);
   1514   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   1515 }
   1516 
   1517 TEST_F(GLES2DecoderWithShaderTest, GetActiveAttribSucceeds) {
   1518   const GLuint kAttribIndex = 1;
   1519   const uint32 kBucketId = 123;
   1520   GetActiveAttrib cmd;
   1521   typedef GetActiveAttrib::Result Result;
   1522   Result* result = static_cast<Result*>(shared_memory_address_);
   1523   result->success = 0;
   1524   cmd.Init(client_program_id_, kAttribIndex, kBucketId,
   1525            shared_memory_id_, shared_memory_offset_);
   1526   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1527   EXPECT_NE(0, result->success);
   1528   EXPECT_EQ(kAttrib2Size, result->size);
   1529   EXPECT_EQ(kAttrib2Type, result->type);
   1530   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1531   CommonDecoder::Bucket* bucket = decoder_->GetBucket(kBucketId);
   1532   ASSERT_TRUE(bucket != NULL);
   1533   EXPECT_EQ(0, memcmp(bucket->GetData(0, bucket->size()), kAttrib2Name,
   1534                       bucket->size()));
   1535 }
   1536 
   1537 TEST_F(GLES2DecoderWithShaderTest, GetActiveAttribResultNotInitFails) {
   1538   const GLuint kAttribIndex = 1;
   1539   const uint32 kBucketId = 123;
   1540   GetActiveAttrib cmd;
   1541   typedef GetActiveAttrib::Result Result;
   1542   Result* result = static_cast<Result*>(shared_memory_address_);
   1543   result->success = 1;
   1544   cmd.Init(client_program_id_, kAttribIndex, kBucketId,
   1545            shared_memory_id_, shared_memory_offset_);
   1546   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   1547 }
   1548 
   1549 TEST_F(GLES2DecoderWithShaderTest, GetActiveAttribBadProgramFails) {
   1550   const GLuint kAttribIndex = 1;
   1551   const uint32 kBucketId = 123;
   1552   GetActiveAttrib cmd;
   1553   typedef GetActiveAttrib::Result Result;
   1554   Result* result = static_cast<Result*>(shared_memory_address_);
   1555   result->success = 0;
   1556   cmd.Init(kInvalidClientId, kAttribIndex, kBucketId,
   1557            shared_memory_id_, shared_memory_offset_);
   1558   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1559   EXPECT_EQ(0, result->success);
   1560   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   1561 #if GLES2_TEST_SHADER_VS_PROGRAM_IDS
   1562   result->success = 0;
   1563   cmd.Init(client_shader_id_, kAttribIndex, kBucketId,
   1564            shared_memory_id_, shared_memory_offset_);
   1565   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1566   EXPECT_EQ(0, result->success);
   1567   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   1568 #endif  // GLES2_TEST_SHADER_VS_PROGRAM_IDS
   1569 }
   1570 
   1571 TEST_F(GLES2DecoderWithShaderTest, GetActiveAttribBadIndexFails) {
   1572   const uint32 kBucketId = 123;
   1573   GetActiveAttrib cmd;
   1574   typedef GetActiveAttrib::Result Result;
   1575   Result* result = static_cast<Result*>(shared_memory_address_);
   1576   result->success = 0;
   1577   cmd.Init(client_program_id_, kBadAttribIndex, kBucketId,
   1578            shared_memory_id_, shared_memory_offset_);
   1579   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1580   EXPECT_EQ(0, result->success);
   1581   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   1582 }
   1583 
   1584 TEST_F(GLES2DecoderWithShaderTest, GetActiveAttribBadSharedMemoryFails) {
   1585   const GLuint kAttribIndex = 1;
   1586   const uint32 kBucketId = 123;
   1587   GetActiveAttrib cmd;
   1588   typedef GetActiveAttrib::Result Result;
   1589   cmd.Init(client_program_id_, kAttribIndex, kBucketId,
   1590            kInvalidSharedMemoryId, shared_memory_offset_);
   1591   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   1592   cmd.Init(client_program_id_, kAttribIndex, kBucketId,
   1593            shared_memory_id_, kInvalidSharedMemoryOffset);
   1594   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   1595 }
   1596 
   1597 TEST_F(GLES2DecoderWithShaderTest, GetShaderInfoLogValidArgs) {
   1598   const char* kInfo = "hello";
   1599   const uint32 kBucketId = 123;
   1600   CompileShader compile_cmd;
   1601   GetShaderInfoLog cmd;
   1602   EXPECT_CALL(*gl_, ShaderSource(kServiceShaderId, 1, _, _));
   1603   EXPECT_CALL(*gl_, CompileShader(kServiceShaderId));
   1604   EXPECT_CALL(*gl_, GetShaderiv(kServiceShaderId, GL_COMPILE_STATUS, _))
   1605       .WillOnce(SetArgumentPointee<2>(GL_FALSE))
   1606       .RetiresOnSaturation();
   1607   EXPECT_CALL(*gl_, GetShaderiv(kServiceShaderId, GL_INFO_LOG_LENGTH, _))
   1608       .WillOnce(SetArgumentPointee<2>(strlen(kInfo) + 1))
   1609       .RetiresOnSaturation();
   1610   EXPECT_CALL(
   1611       *gl_, GetShaderInfoLog(kServiceShaderId, strlen(kInfo) + 1, _, _))
   1612       .WillOnce(DoAll(SetArgumentPointee<2>(strlen(kInfo)),
   1613                       SetArrayArgument<3>(kInfo, kInfo + strlen(kInfo) + 1)));
   1614   compile_cmd.Init(client_shader_id_);
   1615   cmd.Init(client_shader_id_, kBucketId);
   1616   EXPECT_EQ(error::kNoError, ExecuteCmd(compile_cmd));
   1617   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1618   CommonDecoder::Bucket* bucket = decoder_->GetBucket(kBucketId);
   1619   ASSERT_TRUE(bucket != NULL);
   1620   EXPECT_EQ(strlen(kInfo) + 1, bucket->size());
   1621   EXPECT_EQ(0, memcmp(bucket->GetData(0, bucket->size()), kInfo,
   1622                       bucket->size()));
   1623   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1624 }
   1625 
   1626 TEST_F(GLES2DecoderWithShaderTest, GetShaderInfoLogInvalidArgs) {
   1627   const uint32 kBucketId = 123;
   1628   GetShaderInfoLog cmd;
   1629   cmd.Init(kInvalidClientId, kBucketId);
   1630   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1631   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   1632 }
   1633 
   1634 TEST_F(GLES2DecoderTest, GetIntegervCached) {
   1635   struct TestInfo {
   1636     GLenum pname;
   1637     GLint expected;
   1638   };
   1639   TestInfo tests[] = {
   1640     { GL_MAX_TEXTURE_SIZE, TestHelper::kMaxTextureSize, },
   1641     { GL_MAX_CUBE_MAP_TEXTURE_SIZE, TestHelper::kMaxCubeMapTextureSize, },
   1642     { GL_MAX_RENDERBUFFER_SIZE, TestHelper::kMaxRenderbufferSize, },
   1643   };
   1644   typedef GetIntegerv::Result Result;
   1645   for (size_t ii = 0; ii < sizeof(tests) / sizeof(tests[0]); ++ii) {
   1646     const TestInfo& test = tests[ii];
   1647     Result* result = static_cast<Result*>(shared_memory_address_);
   1648     EXPECT_CALL(*gl_, GetError())
   1649         .WillOnce(Return(GL_NO_ERROR))
   1650         .WillOnce(Return(GL_NO_ERROR))
   1651         .RetiresOnSaturation();
   1652     EXPECT_CALL(*gl_, GetIntegerv(test.pname, _))
   1653         .Times(0);
   1654     result->size = 0;
   1655     GetIntegerv cmd2;
   1656     cmd2.Init(test.pname, shared_memory_id_, shared_memory_offset_);
   1657     EXPECT_EQ(error::kNoError, ExecuteCmd(cmd2));
   1658     EXPECT_EQ(
   1659         decoder_->GetGLES2Util()->GLGetNumValuesReturned(test.pname),
   1660         result->GetNumResults());
   1661     EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1662     EXPECT_EQ(test.expected, result->GetData()[0]);
   1663   }
   1664 }
   1665 
   1666 TEST_F(GLES2DecoderTest, CompileShaderValidArgs) {
   1667   EXPECT_CALL(*gl_, ShaderSource(kServiceShaderId, 1, _, _));
   1668   EXPECT_CALL(*gl_, CompileShader(kServiceShaderId));
   1669   EXPECT_CALL(*gl_, GetShaderiv(kServiceShaderId, GL_COMPILE_STATUS, _))
   1670       .WillOnce(SetArgumentPointee<2>(GL_TRUE))
   1671       .RetiresOnSaturation();
   1672   CompileShader cmd;
   1673   cmd.Init(client_shader_id_);
   1674   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1675 }
   1676 
   1677 TEST_F(GLES2DecoderTest, CompileShaderInvalidArgs) {
   1678   CompileShader cmd;
   1679   cmd.Init(kInvalidClientId);
   1680   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1681   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   1682 #if GLES2_TEST_SHADER_VS_PROGRAM_IDS
   1683   cmd.Init(client_program_id_);
   1684   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1685   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   1686 #endif  // GLES2_TEST_SHADER_VS_PROGRAM_IDS
   1687 }
   1688 
   1689 TEST_F(GLES2DecoderTest, ShaderSourceAndGetShaderSourceValidArgs) {
   1690   const uint32 kBucketId = 123;
   1691   const char kSource[] = "hello";
   1692   const uint32 kSourceSize = sizeof(kSource) - 1;
   1693   memcpy(shared_memory_address_, kSource, kSourceSize);
   1694   ShaderSource cmd;
   1695   cmd.Init(client_shader_id_,
   1696            kSharedMemoryId, kSharedMemoryOffset, kSourceSize);
   1697   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1698   memset(shared_memory_address_, 0, kSourceSize);
   1699   GetShaderSource get_cmd;
   1700   get_cmd.Init(client_shader_id_, kBucketId);
   1701   EXPECT_EQ(error::kNoError, ExecuteCmd(get_cmd));
   1702   CommonDecoder::Bucket* bucket = decoder_->GetBucket(kBucketId);
   1703   ASSERT_TRUE(bucket != NULL);
   1704   EXPECT_EQ(kSourceSize + 1, bucket->size());
   1705   EXPECT_EQ(0, memcmp(bucket->GetData(0, bucket->size()), kSource,
   1706                       bucket->size()));
   1707 }
   1708 
   1709 TEST_F(GLES2DecoderTest, ShaderSourceInvalidArgs) {
   1710   const char kSource[] = "hello";
   1711   const uint32 kSourceSize = sizeof(kSource) - 1;
   1712   memcpy(shared_memory_address_, kSource, kSourceSize);
   1713   ShaderSource cmd;
   1714   cmd.Init(kInvalidClientId,
   1715            kSharedMemoryId, kSharedMemoryOffset, kSourceSize);
   1716   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1717   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   1718 #if GLES2_TEST_SHADER_VS_PROGRAM_IDS
   1719   cmd.Init(client_program_id_,
   1720            kSharedMemoryId, kSharedMemoryOffset, kSourceSize);
   1721   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1722   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   1723 #endif  // GLES2_TEST_SHADER_VS_PROGRAM_IDS
   1724   cmd.Init(client_shader_id_,
   1725            kInvalidSharedMemoryId, kSharedMemoryOffset, kSourceSize);
   1726   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   1727   cmd.Init(client_shader_id_,
   1728            kSharedMemoryId, kInvalidSharedMemoryOffset, kSourceSize);
   1729   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   1730   cmd.Init(client_shader_id_,
   1731            kSharedMemoryId, kSharedMemoryOffset, kSharedBufferSize);
   1732   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   1733 }
   1734 
   1735 TEST_F(GLES2DecoderTest, ShaderSourceBucketAndGetShaderSourceValidArgs) {
   1736   const uint32 kInBucketId = 123;
   1737   const uint32 kOutBucketId = 125;
   1738   const char kSource[] = "hello";
   1739   const uint32 kSourceSize = sizeof(kSource) - 1;
   1740   SetBucketAsCString(kInBucketId, kSource);
   1741   ShaderSourceBucket cmd;
   1742   cmd.Init(client_shader_id_, kInBucketId);
   1743   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1744   ClearSharedMemory();
   1745   GetShaderSource get_cmd;
   1746   get_cmd.Init(client_shader_id_, kOutBucketId);
   1747   EXPECT_EQ(error::kNoError, ExecuteCmd(get_cmd));
   1748   CommonDecoder::Bucket* bucket = decoder_->GetBucket(kOutBucketId);
   1749   ASSERT_TRUE(bucket != NULL);
   1750   EXPECT_EQ(kSourceSize + 1, bucket->size());
   1751   EXPECT_EQ(0, memcmp(bucket->GetData(0, bucket->size()), kSource,
   1752                       bucket->size()));
   1753 }
   1754 
   1755 TEST_F(GLES2DecoderTest, ShaderSourceBucketInvalidArgs) {
   1756   const uint32 kBucketId = 123;
   1757   const char kSource[] = "hello";
   1758   const uint32 kSourceSize = sizeof(kSource) - 1;
   1759   memcpy(shared_memory_address_, kSource, kSourceSize);
   1760   ShaderSourceBucket cmd;
   1761   // Test no bucket.
   1762   cmd.Init(client_texture_id_, kBucketId);
   1763   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   1764   // Test invalid client.
   1765   SetBucketAsCString(kBucketId, kSource);
   1766   cmd.Init(kInvalidClientId, kBucketId);
   1767   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1768   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   1769 }
   1770 
   1771 TEST_F(GLES2DecoderTest, ShaderSourceStripComments) {
   1772   const uint32 kInBucketId = 123;
   1773   const char kSource[] = "hello/*te\ast*/world//a\ab";
   1774   SetBucketAsCString(kInBucketId, kSource);
   1775   ShaderSourceBucket cmd;
   1776   cmd.Init(client_shader_id_, kInBucketId);
   1777   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1778   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1779 }
   1780 
   1781 TEST_F(GLES2DecoderTest, GenerateMipmapWrongFormatsFails) {
   1782   EXPECT_CALL(*gl_, GenerateMipmapEXT(_))
   1783        .Times(0);
   1784   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   1785   DoTexImage2D(
   1786       GL_TEXTURE_2D, 0, GL_RGBA, 16, 17, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   1787       0, 0);
   1788   GenerateMipmap cmd;
   1789   cmd.Init(GL_TEXTURE_2D);
   1790   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1791   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   1792 }
   1793 
   1794 TEST_F(GLES2DecoderTest, GenerateMipmapHandlesOutOfMemory) {
   1795   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   1796   TextureManager* manager = group().texture_manager();
   1797   TextureRef* texture_ref = manager->GetTexture(client_texture_id_);
   1798   ASSERT_TRUE(texture_ref != NULL);
   1799   Texture* texture = texture_ref->texture();
   1800   GLint width = 0;
   1801   GLint height = 0;
   1802   EXPECT_FALSE(texture->GetLevelSize(GL_TEXTURE_2D, 2, &width, &height));
   1803   DoTexImage2D(
   1804       GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   1805       kSharedMemoryId, kSharedMemoryOffset);
   1806   EXPECT_CALL(*gl_, TexParameteri(
   1807       GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST))
   1808       .Times(1)
   1809       .RetiresOnSaturation();
   1810   EXPECT_CALL(*gl_, GenerateMipmapEXT(GL_TEXTURE_2D))
   1811       .Times(1);
   1812   EXPECT_CALL(*gl_, TexParameteri(
   1813       GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR))
   1814       .Times(1)
   1815       .RetiresOnSaturation();
   1816   EXPECT_CALL(*gl_, GetError())
   1817       .WillOnce(Return(GL_NO_ERROR))
   1818       .WillOnce(Return(GL_OUT_OF_MEMORY))
   1819       .RetiresOnSaturation();
   1820   GenerateMipmap cmd;
   1821   cmd.Init(GL_TEXTURE_2D);
   1822   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1823   EXPECT_EQ(GL_OUT_OF_MEMORY, GetGLError());
   1824   EXPECT_FALSE(texture->GetLevelSize(GL_TEXTURE_2D, 2, &width, &height));
   1825 }
   1826 
   1827 TEST_F(GLES2DecoderTest, GenerateMipmapClearsUnclearedTexture) {
   1828   EXPECT_CALL(*gl_, GenerateMipmapEXT(_))
   1829        .Times(0);
   1830   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   1831   DoTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   1832                0, 0);
   1833   SetupClearTextureExpections(
   1834       kServiceTextureId, kServiceTextureId, GL_TEXTURE_2D, GL_TEXTURE_2D,
   1835       0, GL_RGBA, GL_UNSIGNED_BYTE, 2, 2);
   1836   EXPECT_CALL(*gl_, TexParameteri(
   1837       GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST))
   1838       .Times(1)
   1839       .RetiresOnSaturation();
   1840   EXPECT_CALL(*gl_, GenerateMipmapEXT(GL_TEXTURE_2D));
   1841   EXPECT_CALL(*gl_, TexParameteri(
   1842       GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR))
   1843       .Times(1)
   1844       .RetiresOnSaturation();
   1845   EXPECT_CALL(*gl_, GetError())
   1846       .WillOnce(Return(GL_NO_ERROR))
   1847       .WillOnce(Return(GL_NO_ERROR))
   1848       .RetiresOnSaturation();
   1849   GenerateMipmap cmd;
   1850   cmd.Init(GL_TEXTURE_2D);
   1851   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1852   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1853 }
   1854 
   1855 TEST_F(GLES2DecoderWithShaderTest, Uniform1iValidArgs) {
   1856   EXPECT_CALL(*gl_, Uniform1i(kUniform1RealLocation, 2));
   1857   Uniform1i cmd;
   1858   cmd.Init(kUniform1FakeLocation, 2);
   1859   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1860 }
   1861 
   1862 TEST_F(GLES2DecoderWithShaderTest, Uniform1ivValidArgs) {
   1863   EXPECT_CALL(
   1864       *gl_, Uniform1iv(kUniform1RealLocation, 1,
   1865           reinterpret_cast<const GLint*>(shared_memory_address_)));
   1866   Uniform1iv cmd;
   1867   cmd.Init(kUniform1FakeLocation,
   1868            1, shared_memory_id_, shared_memory_offset_);
   1869   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1870 }
   1871 
   1872 TEST_F(GLES2DecoderWithShaderTest, Uniform1ivInvalidArgs2_0) {
   1873   EXPECT_CALL(*gl_, Uniform1iv(_, _, _)).Times(0);
   1874   Uniform1iv cmd;
   1875   cmd.Init(kUniform1FakeLocation,
   1876            1, kInvalidSharedMemoryId, 0);
   1877   EXPECT_EQ(error::kOutOfBounds, ExecuteCmd(cmd));
   1878 }
   1879 
   1880 TEST_F(GLES2DecoderWithShaderTest, Uniform1ivInvalidArgs2_1) {
   1881   EXPECT_CALL(*gl_, Uniform1iv(_, _, _)).Times(0);
   1882   Uniform1iv cmd;
   1883   cmd.Init(kUniform1FakeLocation,
   1884            1, shared_memory_id_, kInvalidSharedMemoryOffset);
   1885   EXPECT_EQ(error::kOutOfBounds, ExecuteCmd(cmd));
   1886 }
   1887 
   1888 TEST_F(GLES2DecoderWithShaderTest, Uniform1ivImmediateValidArgs) {
   1889   Uniform1ivImmediate& cmd = *GetImmediateAs<Uniform1ivImmediate>();
   1890   EXPECT_CALL(
   1891       *gl_,
   1892       Uniform1iv(kUniform1RealLocation, 1,
   1893           reinterpret_cast<GLint*>(ImmediateDataAddress(&cmd))));
   1894   GLint temp[1 * 2] = { 0, };
   1895   cmd.Init(kUniform1FakeLocation, 1,
   1896            &temp[0]);
   1897   EXPECT_EQ(error::kNoError,
   1898             ExecuteImmediateCmd(cmd, sizeof(temp)));
   1899 }
   1900 
   1901 TEST_F(GLES2DecoderWithShaderTest, Uniform1ivInvalidValidArgs) {
   1902   EXPECT_CALL(*gl_, Uniform1iv(_, _, _)).Times(0);
   1903   Uniform1iv cmd;
   1904   cmd.Init(kUniform1FakeLocation,
   1905            2, shared_memory_id_, shared_memory_offset_);
   1906   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1907   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   1908 }
   1909 
   1910 TEST_F(GLES2DecoderWithShaderTest, Uniform1ivZeroCount) {
   1911   EXPECT_CALL(*gl_, Uniform1iv(_, _, _)).Times(0);
   1912   Uniform1iv cmd;
   1913   cmd.Init(kUniform1FakeLocation,
   1914            0, shared_memory_id_, shared_memory_offset_);
   1915   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1916   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1917 }
   1918 
   1919 TEST_F(GLES2DecoderWithShaderTest, Uniform1iSamplerIsLmited) {
   1920   EXPECT_CALL(*gl_, Uniform1i(_, _)).Times(0);
   1921   Uniform1i cmd;
   1922   cmd.Init(
   1923       kUniform1FakeLocation,
   1924       kNumTextureUnits);
   1925   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1926   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   1927 }
   1928 
   1929 TEST_F(GLES2DecoderWithShaderTest, Uniform1ivSamplerIsLimited) {
   1930   EXPECT_CALL(*gl_, Uniform1iv(_, _, _)).Times(0);
   1931   Uniform1ivImmediate& cmd = *GetImmediateAs<Uniform1ivImmediate>();
   1932   GLint temp[] = { kNumTextureUnits };
   1933   cmd.Init(kUniform1FakeLocation, 1,
   1934            &temp[0]);
   1935   EXPECT_EQ(error::kNoError,
   1936             ExecuteImmediateCmd(cmd, sizeof(temp)));
   1937   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   1938 }
   1939 
   1940 TEST_F(GLES2DecoderWithShaderTest, BindBufferToDifferentTargetFails) {
   1941   // Bind the buffer to GL_ARRAY_BUFFER
   1942   DoBindBuffer(GL_ARRAY_BUFFER, client_buffer_id_, kServiceBufferId);
   1943   // Attempt to rebind to GL_ELEMENT_ARRAY_BUFFER
   1944   // NOTE: Real GLES2 does not have this restriction but WebGL and we do.
   1945   // This can be restriction can be removed at runtime.
   1946   EXPECT_CALL(*gl_, BindBuffer(_, _))
   1947       .Times(0);
   1948   BindBuffer cmd;
   1949   cmd.Init(GL_ELEMENT_ARRAY_BUFFER, client_buffer_id_);
   1950   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1951   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   1952 }
   1953 
   1954 TEST_F(GLES2DecoderTest, ActiveTextureValidArgs) {
   1955   EXPECT_CALL(*gl_, ActiveTexture(GL_TEXTURE1));
   1956   SpecializedSetup<ActiveTexture, 0>(true);
   1957   ActiveTexture cmd;
   1958   cmd.Init(GL_TEXTURE1);
   1959   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1960   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1961 }
   1962 
   1963 TEST_F(GLES2DecoderTest, ActiveTextureInvalidArgs) {
   1964   EXPECT_CALL(*gl_, ActiveTexture(_)).Times(0);
   1965   SpecializedSetup<ActiveTexture, 0>(false);
   1966   ActiveTexture cmd;
   1967   cmd.Init(GL_TEXTURE0 - 1);
   1968   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1969   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
   1970   cmd.Init(kNumTextureUnits);
   1971   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1972   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
   1973 }
   1974 
   1975 TEST_F(GLES2DecoderTest, CheckFramebufferStatusWithNoBoundTarget) {
   1976   EXPECT_CALL(*gl_, CheckFramebufferStatusEXT(_))
   1977       .Times(0);
   1978   CheckFramebufferStatus::Result* result =
   1979       static_cast<CheckFramebufferStatus::Result*>(shared_memory_address_);
   1980   *result = 0;
   1981   CheckFramebufferStatus cmd;
   1982   cmd.Init(GL_FRAMEBUFFER, shared_memory_id_, shared_memory_offset_);
   1983   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1984   EXPECT_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE), *result);
   1985 }
   1986 
   1987 TEST_F(GLES2DecoderWithShaderTest, BindAndDeleteFramebuffer) {
   1988   SetupTexture();
   1989   AddExpectationsForSimulatedAttrib0(kNumVertices, 0);
   1990   SetupExpectationsForApplyingDefaultDirtyState();
   1991   DoBindFramebuffer(GL_FRAMEBUFFER, client_framebuffer_id_,
   1992                     kServiceFramebufferId);
   1993   DoDeleteFramebuffer(
   1994       client_framebuffer_id_, kServiceFramebufferId,
   1995       true, GL_FRAMEBUFFER, 0,
   1996       true, GL_FRAMEBUFFER, 0);
   1997   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   1998       .Times(1)
   1999       .RetiresOnSaturation();
   2000   DrawArrays cmd;
   2001   cmd.Init(GL_TRIANGLES, 0, kNumVertices);
   2002   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2003   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   2004 }
   2005 
   2006 TEST_F(GLES2DecoderTest, FramebufferRenderbufferWithNoBoundTarget) {
   2007   EXPECT_CALL(*gl_, FramebufferRenderbufferEXT(_, _, _, _))
   2008       .Times(0);
   2009   FramebufferRenderbuffer cmd;
   2010   cmd.Init(
   2011       GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER,
   2012       client_renderbuffer_id_);
   2013   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2014   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   2015 }
   2016 
   2017 TEST_F(GLES2DecoderTest, FramebufferTexture2DWithNoBoundTarget) {
   2018   EXPECT_CALL(*gl_, FramebufferTexture2DEXT(_, _, _, _, _))
   2019       .Times(0);
   2020   FramebufferTexture2D cmd;
   2021   cmd.Init(
   2022       GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, client_texture_id_,
   2023       0);
   2024   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2025   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   2026 }
   2027 
   2028 TEST_F(GLES2DecoderTest, GetFramebufferAttachmentParameterivWithNoBoundTarget) {
   2029   EXPECT_CALL(*gl_, GetError())
   2030       .WillOnce(Return(GL_NO_ERROR))
   2031       .WillOnce(Return(GL_NO_ERROR))
   2032       .RetiresOnSaturation();
   2033   EXPECT_CALL(*gl_, GetFramebufferAttachmentParameterivEXT(_, _, _, _))
   2034       .Times(0);
   2035   GetFramebufferAttachmentParameteriv cmd;
   2036   cmd.Init(
   2037       GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
   2038       GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, shared_memory_id_,
   2039       shared_memory_offset_);
   2040   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2041   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   2042 }
   2043 
   2044 TEST_F(GLES2DecoderTest, GetFramebufferAttachmentParameterivWithRenderbuffer) {
   2045   DoBindFramebuffer(GL_FRAMEBUFFER, client_framebuffer_id_,
   2046                     kServiceFramebufferId);
   2047   EXPECT_CALL(*gl_, GetError())
   2048       .WillOnce(Return(GL_NO_ERROR))
   2049       .RetiresOnSaturation();
   2050   EXPECT_CALL(*gl_, FramebufferRenderbufferEXT(
   2051       GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER,
   2052       kServiceRenderbufferId))
   2053       .Times(1)
   2054       .RetiresOnSaturation();
   2055   EXPECT_CALL(*gl_, GetError())
   2056       .WillOnce(Return(GL_NO_ERROR))
   2057       .RetiresOnSaturation();
   2058   EXPECT_CALL(*gl_, GetError())
   2059       .WillOnce(Return(GL_NO_ERROR))
   2060       .WillOnce(Return(GL_NO_ERROR))
   2061       .RetiresOnSaturation();
   2062   GetFramebufferAttachmentParameteriv::Result* result =
   2063       static_cast<GetFramebufferAttachmentParameteriv::Result*>(
   2064           shared_memory_address_);
   2065   result->size = 0;
   2066   const GLint* result_value = result->GetData();
   2067   FramebufferRenderbuffer fbrb_cmd;
   2068   GetFramebufferAttachmentParameteriv cmd;
   2069   fbrb_cmd.Init(
   2070       GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER,
   2071       client_renderbuffer_id_);
   2072   cmd.Init(
   2073       GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
   2074       GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, shared_memory_id_,
   2075       shared_memory_offset_);
   2076   EXPECT_EQ(error::kNoError, ExecuteCmd(fbrb_cmd));
   2077   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2078   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   2079   EXPECT_EQ(static_cast<GLuint>(*result_value), client_renderbuffer_id_);
   2080 }
   2081 
   2082 TEST_F(GLES2DecoderTest, GetFramebufferAttachmentParameterivWithTexture) {
   2083   DoBindFramebuffer(GL_FRAMEBUFFER, client_framebuffer_id_,
   2084                     kServiceFramebufferId);
   2085   EXPECT_CALL(*gl_, GetError())
   2086       .WillOnce(Return(GL_NO_ERROR))
   2087       .RetiresOnSaturation();
   2088   EXPECT_CALL(*gl_, FramebufferTexture2DEXT(
   2089       GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
   2090       kServiceTextureId, 0))
   2091       .Times(1)
   2092       .RetiresOnSaturation();
   2093   EXPECT_CALL(*gl_, GetError())
   2094       .WillOnce(Return(GL_NO_ERROR))
   2095       .RetiresOnSaturation();
   2096   EXPECT_CALL(*gl_, GetError())
   2097       .WillOnce(Return(GL_NO_ERROR))
   2098       .WillOnce(Return(GL_NO_ERROR))
   2099       .RetiresOnSaturation();
   2100   GetFramebufferAttachmentParameteriv::Result* result =
   2101       static_cast<GetFramebufferAttachmentParameteriv::Result*>(
   2102           shared_memory_address_);
   2103   result->SetNumResults(0);
   2104   const GLint* result_value = result->GetData();
   2105   FramebufferTexture2D fbtex_cmd;
   2106   GetFramebufferAttachmentParameteriv cmd;
   2107   fbtex_cmd.Init(
   2108       GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, client_texture_id_,
   2109       0);
   2110   cmd.Init(
   2111       GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
   2112       GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, shared_memory_id_,
   2113       shared_memory_offset_);
   2114   EXPECT_EQ(error::kNoError, ExecuteCmd(fbtex_cmd));
   2115   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2116   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   2117   EXPECT_EQ(static_cast<GLuint>(*result_value), client_texture_id_);
   2118 }
   2119 
   2120 TEST_F(GLES2DecoderTest, GetRenderbufferParameterivWithNoBoundTarget) {
   2121   EXPECT_CALL(*gl_, GetError())
   2122       .WillOnce(Return(GL_NO_ERROR))
   2123       .WillOnce(Return(GL_NO_ERROR))
   2124       .RetiresOnSaturation();
   2125   EXPECT_CALL(*gl_, GetRenderbufferParameterivEXT(_, _, _))
   2126       .Times(0);
   2127   GetRenderbufferParameteriv cmd;
   2128   cmd.Init(
   2129       GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, shared_memory_id_,
   2130       shared_memory_offset_);
   2131   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2132   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   2133 }
   2134 
   2135 TEST_F(GLES2DecoderTest, RenderbufferStorageWithNoBoundTarget) {
   2136   EXPECT_CALL(*gl_, RenderbufferStorageEXT(_, _, _, _))
   2137       .Times(0);
   2138   RenderbufferStorage cmd;
   2139   cmd.Init(GL_RENDERBUFFER, GL_RGBA4, 3, 4);
   2140   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2141   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   2142 }
   2143 
   2144 namespace {
   2145 
   2146 // A class to emulate glReadPixels
   2147 class ReadPixelsEmulator {
   2148  public:
   2149   // pack_alignment is the alignment you want ReadPixels to use
   2150   // when copying. The actual data passed in pixels should be contiguous.
   2151   ReadPixelsEmulator(GLsizei width, GLsizei height, GLint bytes_per_pixel,
   2152                      const void* src_pixels, const void* expected_pixels,
   2153                      GLint pack_alignment)
   2154       : width_(width),
   2155         height_(height),
   2156         pack_alignment_(pack_alignment),
   2157         bytes_per_pixel_(bytes_per_pixel),
   2158         src_pixels_(reinterpret_cast<const int8*>(src_pixels)),
   2159         expected_pixels_(reinterpret_cast<const int8*>(expected_pixels)) {
   2160   }
   2161 
   2162   void ReadPixels(
   2163       GLint x, GLint y, GLsizei width, GLsizei height,
   2164       GLenum format, GLenum type, void* pixels) const {
   2165     DCHECK_GE(x, 0);
   2166     DCHECK_GE(y, 0);
   2167     DCHECK_LE(x + width, width_);
   2168     DCHECK_LE(y + height, height_);
   2169     for (GLint yy = 0; yy < height; ++yy) {
   2170       const int8* src = GetPixelAddress(src_pixels_, x, y + yy);
   2171       const void* dst = ComputePackAlignmentAddress(0, yy, width, pixels);
   2172       memcpy(const_cast<void*>(dst), src, width * bytes_per_pixel_);
   2173     }
   2174   }
   2175 
   2176   bool CompareRowSegment(
   2177       GLint x, GLint y, GLsizei width, const void* data) const {
   2178     DCHECK(x + width <= width_ || width == 0);
   2179     return memcmp(data, GetPixelAddress(expected_pixels_, x, y),
   2180                   width * bytes_per_pixel_) == 0;
   2181   }
   2182 
   2183   // Helper to compute address of pixel in pack aligned data.
   2184   const void* ComputePackAlignmentAddress(
   2185       GLint x, GLint y, GLsizei width, const void* address) const {
   2186     GLint unpadded_row_size = ComputeImageDataSize(width, 1);
   2187     GLint two_rows_size = ComputeImageDataSize(width, 2);
   2188     GLsizei padded_row_size = two_rows_size - unpadded_row_size;
   2189     GLint offset = y * padded_row_size + x * bytes_per_pixel_;
   2190     return static_cast<const int8*>(address) + offset;
   2191   }
   2192 
   2193   GLint ComputeImageDataSize(GLint width, GLint height) const {
   2194     GLint row_size = width * bytes_per_pixel_;
   2195     if (height > 1) {
   2196       GLint temp = row_size + pack_alignment_ - 1;
   2197       GLint padded_row_size = (temp / pack_alignment_) * pack_alignment_;
   2198       GLint size_of_all_but_last_row = (height - 1) * padded_row_size;
   2199       return size_of_all_but_last_row + row_size;
   2200     } else {
   2201       return height * row_size;
   2202     }
   2203   }
   2204 
   2205  private:
   2206   const int8* GetPixelAddress(const int8* base, GLint x, GLint y) const {
   2207     return base + (width_ * y + x) * bytes_per_pixel_;
   2208   }
   2209 
   2210   GLsizei width_;
   2211   GLsizei height_;
   2212   GLint pack_alignment_;
   2213   GLint bytes_per_pixel_;
   2214   const int8* src_pixels_;
   2215   const int8* expected_pixels_;
   2216 };
   2217 
   2218 }  // anonymous namespace
   2219 
   2220 void GLES2DecoderTest::CheckReadPixelsOutOfRange(
   2221     GLint in_read_x, GLint in_read_y,
   2222     GLsizei in_read_width, GLsizei in_read_height,
   2223     bool init) {
   2224   const GLsizei kWidth = 5;
   2225   const GLsizei kHeight = 3;
   2226   const GLint kBytesPerPixel = 3;
   2227   const GLint kPackAlignment = 4;
   2228   const GLenum kFormat = GL_RGB;
   2229   static const int8 kSrcPixels[kWidth * kHeight * kBytesPerPixel] = {
   2230     12, 13, 14, 18, 19, 18, 19, 12, 13, 14, 18, 19, 18, 19, 13,
   2231     29, 28, 23, 22, 21, 22, 21, 29, 28, 23, 22, 21, 22, 21, 28,
   2232     31, 34, 39, 37, 32, 37, 32, 31, 34, 39, 37, 32, 37, 32, 34,
   2233   };
   2234 
   2235   ClearSharedMemory();
   2236 
   2237   // We need to setup an FBO so we can know the max size that ReadPixels will
   2238   // access
   2239   if (init) {
   2240     DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   2241     DoTexImage2D(
   2242         GL_TEXTURE_2D, 0, kFormat, kWidth, kHeight, 0,
   2243         kFormat, GL_UNSIGNED_BYTE, kSharedMemoryId,
   2244         kSharedMemoryOffset);
   2245     DoBindFramebuffer(
   2246         GL_FRAMEBUFFER, client_framebuffer_id_, kServiceFramebufferId);
   2247     DoFramebufferTexture2D(
   2248         GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
   2249         client_texture_id_, kServiceTextureId, 0, GL_NO_ERROR);
   2250     EXPECT_CALL(*gl_, CheckFramebufferStatusEXT(GL_FRAMEBUFFER))
   2251         .WillOnce(Return(GL_FRAMEBUFFER_COMPLETE))
   2252         .RetiresOnSaturation();
   2253   }
   2254 
   2255   ReadPixelsEmulator emu(
   2256       kWidth, kHeight, kBytesPerPixel, kSrcPixels, kSrcPixels, kPackAlignment);
   2257   typedef ReadPixels::Result Result;
   2258   Result* result = GetSharedMemoryAs<Result*>();
   2259   uint32 result_shm_id = kSharedMemoryId;
   2260   uint32 result_shm_offset = kSharedMemoryOffset;
   2261   uint32 pixels_shm_id = kSharedMemoryId;
   2262   uint32 pixels_shm_offset = kSharedMemoryOffset + sizeof(*result);
   2263   void* dest = &result[1];
   2264   EXPECT_CALL(*gl_, GetError())
   2265      .WillOnce(Return(GL_NO_ERROR))
   2266      .WillOnce(Return(GL_NO_ERROR))
   2267      .RetiresOnSaturation();
   2268   // ReadPixels will be called for valid size only even though the command
   2269   // is requesting a larger size.
   2270   GLint read_x = std::max(0, in_read_x);
   2271   GLint read_y = std::max(0, in_read_y);
   2272   GLint read_end_x = std::max(0, std::min(kWidth, in_read_x + in_read_width));
   2273   GLint read_end_y = std::max(0, std::min(kHeight, in_read_y + in_read_height));
   2274   GLint read_width = read_end_x - read_x;
   2275   GLint read_height = read_end_y - read_y;
   2276   if (read_width > 0 && read_height > 0) {
   2277     for (GLint yy = read_y; yy < read_end_y; ++yy) {
   2278       EXPECT_CALL(
   2279           *gl_, ReadPixels(read_x, yy, read_width, 1,
   2280                            kFormat, GL_UNSIGNED_BYTE, _))
   2281           .WillOnce(Invoke(&emu, &ReadPixelsEmulator::ReadPixels))
   2282           .RetiresOnSaturation();
   2283     }
   2284   }
   2285   ReadPixels cmd;
   2286   cmd.Init(in_read_x, in_read_y, in_read_width, in_read_height,
   2287            kFormat, GL_UNSIGNED_BYTE,
   2288            pixels_shm_id, pixels_shm_offset,
   2289            result_shm_id, result_shm_offset,
   2290            false);
   2291   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2292 
   2293   GLint unpadded_row_size = emu.ComputeImageDataSize(in_read_width, 1);
   2294   scoped_ptr<int8[]> zero(new int8[unpadded_row_size]);
   2295   scoped_ptr<int8[]> pack(new int8[kPackAlignment]);
   2296   memset(zero.get(), 0, unpadded_row_size);
   2297   memset(pack.get(), kInitialMemoryValue, kPackAlignment);
   2298   for (GLint yy = 0; yy < in_read_height; ++yy) {
   2299     const int8* row = static_cast<const int8*>(
   2300         emu.ComputePackAlignmentAddress(0, yy, in_read_width, dest));
   2301     GLint y = in_read_y + yy;
   2302     if (y < 0 || y >= kHeight) {
   2303       EXPECT_EQ(0, memcmp(zero.get(), row, unpadded_row_size));
   2304     } else {
   2305       // check off left.
   2306       GLint num_left_pixels = std::max(-in_read_x, 0);
   2307       GLint num_left_bytes = num_left_pixels * kBytesPerPixel;
   2308       EXPECT_EQ(0, memcmp(zero.get(), row, num_left_bytes));
   2309 
   2310       // check off right.
   2311       GLint num_right_pixels = std::max(in_read_x + in_read_width - kWidth, 0);
   2312       GLint num_right_bytes = num_right_pixels * kBytesPerPixel;
   2313       EXPECT_EQ(0, memcmp(zero.get(),
   2314                             row + unpadded_row_size - num_right_bytes,
   2315                             num_right_bytes));
   2316 
   2317       // check middle.
   2318       GLint x = std::max(in_read_x, 0);
   2319       GLint num_middle_pixels =
   2320           std::max(in_read_width - num_left_pixels - num_right_pixels, 0);
   2321       EXPECT_TRUE(emu.CompareRowSegment(
   2322           x, y, num_middle_pixels, row + num_left_bytes));
   2323     }
   2324 
   2325     // check padding
   2326     if (yy != in_read_height - 1) {
   2327       GLint num_padding_bytes =
   2328           (kPackAlignment - 1) - (unpadded_row_size % kPackAlignment);
   2329       EXPECT_EQ(0,
   2330                 memcmp(pack.get(), row + unpadded_row_size, num_padding_bytes));
   2331     }
   2332   }
   2333 }
   2334 
   2335 TEST_F(GLES2DecoderTest, ReadPixels) {
   2336   const GLsizei kWidth = 5;
   2337   const GLsizei kHeight = 3;
   2338   const GLint kBytesPerPixel = 3;
   2339   const GLint kPackAlignment = 4;
   2340   static const int8 kSrcPixels[kWidth * kHeight * kBytesPerPixel] = {
   2341     12, 13, 14, 18, 19, 18, 19, 12, 13, 14, 18, 19, 18, 19, 13,
   2342     29, 28, 23, 22, 21, 22, 21, 29, 28, 23, 22, 21, 22, 21, 28,
   2343     31, 34, 39, 37, 32, 37, 32, 31, 34, 39, 37, 32, 37, 32, 34,
   2344   };
   2345 
   2346   surface_->SetSize(gfx::Size(INT_MAX, INT_MAX));
   2347 
   2348   ReadPixelsEmulator emu(
   2349       kWidth, kHeight, kBytesPerPixel, kSrcPixels, kSrcPixels, kPackAlignment);
   2350   typedef ReadPixels::Result Result;
   2351   Result* result = GetSharedMemoryAs<Result*>();
   2352   uint32 result_shm_id = kSharedMemoryId;
   2353   uint32 result_shm_offset = kSharedMemoryOffset;
   2354   uint32 pixels_shm_id = kSharedMemoryId;
   2355   uint32 pixels_shm_offset = kSharedMemoryOffset + sizeof(*result);
   2356   void* dest = &result[1];
   2357   EXPECT_CALL(*gl_, GetError())
   2358      .WillOnce(Return(GL_NO_ERROR))
   2359      .WillOnce(Return(GL_NO_ERROR))
   2360      .RetiresOnSaturation();
   2361   EXPECT_CALL(
   2362       *gl_, ReadPixels(0, 0, kWidth, kHeight, GL_RGB, GL_UNSIGNED_BYTE, _))
   2363       .WillOnce(Invoke(&emu, &ReadPixelsEmulator::ReadPixels));
   2364   ReadPixels cmd;
   2365   cmd.Init(0, 0, kWidth, kHeight, GL_RGB, GL_UNSIGNED_BYTE,
   2366            pixels_shm_id, pixels_shm_offset,
   2367            result_shm_id, result_shm_offset,
   2368            false);
   2369   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2370   for (GLint yy = 0; yy < kHeight; ++yy) {
   2371     EXPECT_TRUE(emu.CompareRowSegment(
   2372         0, yy, kWidth,
   2373         emu.ComputePackAlignmentAddress(0, yy, kWidth, dest)));
   2374   }
   2375 }
   2376 
   2377 TEST_F(GLES2DecoderRGBBackbufferTest, ReadPixelsNoAlphaBackbuffer) {
   2378   const GLsizei kWidth = 3;
   2379   const GLsizei kHeight = 3;
   2380   const GLint kBytesPerPixel = 4;
   2381   const GLint kPackAlignment = 4;
   2382   static const uint8 kExpectedPixels[kWidth * kHeight * kBytesPerPixel] = {
   2383     12, 13, 14, 255, 19, 18, 19, 255, 13, 14, 18, 255,
   2384     29, 28, 23, 255, 21, 22, 21, 255, 28, 23, 22, 255,
   2385     31, 34, 39, 255, 32, 37, 32, 255, 34, 39, 37, 255,
   2386   };
   2387   static const uint8 kSrcPixels[kWidth * kHeight * kBytesPerPixel] = {
   2388     12, 13, 14, 18, 19, 18, 19, 12, 13, 14, 18, 19,
   2389     29, 28, 23, 22, 21, 22, 21, 29, 28, 23, 22, 21,
   2390     31, 34, 39, 37, 32, 37, 32, 31, 34, 39, 37, 32,
   2391   };
   2392 
   2393   surface_->SetSize(gfx::Size(INT_MAX, INT_MAX));
   2394 
   2395   ReadPixelsEmulator emu(
   2396       kWidth, kHeight, kBytesPerPixel, kSrcPixels, kExpectedPixels,
   2397       kPackAlignment);
   2398   typedef ReadPixels::Result Result;
   2399   Result* result = GetSharedMemoryAs<Result*>();
   2400   uint32 result_shm_id = kSharedMemoryId;
   2401   uint32 result_shm_offset = kSharedMemoryOffset;
   2402   uint32 pixels_shm_id = kSharedMemoryId;
   2403   uint32 pixels_shm_offset = kSharedMemoryOffset + sizeof(*result);
   2404   void* dest = &result[1];
   2405   EXPECT_CALL(*gl_, GetError())
   2406      .WillOnce(Return(GL_NO_ERROR))
   2407      .WillOnce(Return(GL_NO_ERROR))
   2408      .RetiresOnSaturation();
   2409   EXPECT_CALL(
   2410       *gl_, ReadPixels(0, 0, kWidth, kHeight, GL_RGBA, GL_UNSIGNED_BYTE, _))
   2411       .WillOnce(Invoke(&emu, &ReadPixelsEmulator::ReadPixels));
   2412   ReadPixels cmd;
   2413   cmd.Init(0, 0, kWidth, kHeight, GL_RGBA, GL_UNSIGNED_BYTE,
   2414            pixels_shm_id, pixels_shm_offset,
   2415            result_shm_id, result_shm_offset,
   2416            false);
   2417   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2418   for (GLint yy = 0; yy < kHeight; ++yy) {
   2419     EXPECT_TRUE(emu.CompareRowSegment(
   2420         0, yy, kWidth,
   2421         emu.ComputePackAlignmentAddress(0, yy, kWidth, dest)));
   2422   }
   2423 }
   2424 
   2425 TEST_F(GLES2DecoderTest, ReadPixelsOutOfRange) {
   2426   static GLint tests[][4] = {
   2427     { -2, -1, 9, 5, },  // out of range on all sides
   2428     { 2, 1, 9, 5, },  // out of range on right, bottom
   2429     { -7, -4, 9, 5, },  // out of range on left, top
   2430     { 0, -5, 9, 5, },  // completely off top
   2431     { 0, 3, 9, 5, },  // completely off bottom
   2432     { -9, 0, 9, 5, },  // completely off left
   2433     { 5, 0, 9, 5, },  // completely off right
   2434   };
   2435 
   2436   for (size_t tt = 0; tt < arraysize(tests); ++tt) {
   2437     CheckReadPixelsOutOfRange(
   2438         tests[tt][0], tests[tt][1], tests[tt][2], tests[tt][3], tt == 0);
   2439   }
   2440 }
   2441 
   2442 TEST_F(GLES2DecoderTest, ReadPixelsInvalidArgs) {
   2443   typedef ReadPixels::Result Result;
   2444   Result* result = GetSharedMemoryAs<Result*>();
   2445   uint32 result_shm_id = kSharedMemoryId;
   2446   uint32 result_shm_offset = kSharedMemoryOffset;
   2447   uint32 pixels_shm_id = kSharedMemoryId;
   2448   uint32 pixels_shm_offset = kSharedMemoryOffset + sizeof(*result);
   2449   EXPECT_CALL(*gl_, ReadPixels(_, _, _, _, _, _, _)).Times(0);
   2450   ReadPixels cmd;
   2451   cmd.Init(0, 0, -1, 1, GL_RGB, GL_UNSIGNED_BYTE,
   2452            pixels_shm_id, pixels_shm_offset,
   2453            result_shm_id, result_shm_offset,
   2454            false);
   2455   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2456   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   2457   cmd.Init(0, 0, 1, -1, GL_RGB, GL_UNSIGNED_BYTE,
   2458            pixels_shm_id, pixels_shm_offset,
   2459            result_shm_id, result_shm_offset,
   2460            false);
   2461   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2462   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   2463   cmd.Init(0, 0, 1, 1, GL_RGB, GL_INT,
   2464            pixels_shm_id, pixels_shm_offset,
   2465            result_shm_id, result_shm_offset,
   2466            false);
   2467   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2468   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
   2469   cmd.Init(0, 0, 1, 1, GL_RGB, GL_UNSIGNED_BYTE,
   2470            kInvalidSharedMemoryId, pixels_shm_offset,
   2471            result_shm_id, result_shm_offset,
   2472            false);
   2473   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   2474   cmd.Init(0, 0, 1, 1, GL_RGB, GL_UNSIGNED_BYTE,
   2475            pixels_shm_id, kInvalidSharedMemoryOffset,
   2476            result_shm_id, result_shm_offset,
   2477            false);
   2478   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   2479   cmd.Init(0, 0, 1, 1, GL_RGB, GL_UNSIGNED_BYTE,
   2480            pixels_shm_id, pixels_shm_offset,
   2481            kInvalidSharedMemoryId, result_shm_offset,
   2482            false);
   2483   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   2484   cmd.Init(0, 0, 1, 1, GL_RGB, GL_UNSIGNED_BYTE,
   2485            pixels_shm_id, pixels_shm_offset,
   2486            result_shm_id, kInvalidSharedMemoryOffset,
   2487            false);
   2488   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   2489 }
   2490 
   2491 TEST_F(GLES2DecoderTest, BindAttribLocation) {
   2492   const GLint kLocation = 2;
   2493   const char* kName = "testing";
   2494   const uint32 kNameSize = strlen(kName);
   2495   EXPECT_CALL(
   2496       *gl_, BindAttribLocation(kServiceProgramId, kLocation, StrEq(kName)))
   2497       .Times(1);
   2498   memcpy(shared_memory_address_, kName, kNameSize);
   2499   BindAttribLocation cmd;
   2500   cmd.Init(client_program_id_, kLocation, kSharedMemoryId, kSharedMemoryOffset,
   2501            kNameSize);
   2502   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2503 }
   2504 
   2505 TEST_F(GLES2DecoderTest, BindAttribLocationInvalidArgs) {
   2506   const GLint kLocation = 2;
   2507   const char* kName = "testing";
   2508   const char* kBadName = "test\aing";
   2509   const uint32 kNameSize = strlen(kName);
   2510   const uint32 kBadNameSize = strlen(kBadName);
   2511   EXPECT_CALL(*gl_, BindAttribLocation(_, _, _)).Times(0);
   2512   memcpy(shared_memory_address_, kName, kNameSize);
   2513   BindAttribLocation cmd;
   2514   cmd.Init(kInvalidClientId, kLocation,
   2515            kSharedMemoryId, kSharedMemoryOffset, kNameSize);
   2516   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2517   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   2518   cmd.Init(client_program_id_, kLocation,
   2519            kInvalidSharedMemoryId, kSharedMemoryOffset, kNameSize);
   2520   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   2521   cmd.Init(client_program_id_, kLocation,
   2522            kSharedMemoryId, kInvalidSharedMemoryOffset, kNameSize);
   2523   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   2524   cmd.Init(client_program_id_, kLocation,
   2525            kSharedMemoryId, kSharedMemoryOffset, kSharedBufferSize);
   2526   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   2527   memcpy(shared_memory_address_, kBadName, kBadNameSize);
   2528   cmd.Init(client_program_id_, kLocation,
   2529            kSharedMemoryId, kSharedMemoryOffset, kBadNameSize);
   2530   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2531   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   2532 }
   2533 
   2534 TEST_F(GLES2DecoderTest, BindAttribLocationBucket) {
   2535   const uint32 kBucketId = 123;
   2536   const GLint kLocation = 2;
   2537   const char* kName = "testing";
   2538   EXPECT_CALL(
   2539       *gl_, BindAttribLocation(kServiceProgramId, kLocation, StrEq(kName)))
   2540       .Times(1);
   2541   SetBucketAsCString(kBucketId, kName);
   2542   BindAttribLocationBucket cmd;
   2543   cmd.Init(client_program_id_, kLocation, kBucketId);
   2544   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2545 }
   2546 
   2547 TEST_F(GLES2DecoderTest, BindAttribLocationBucketInvalidArgs) {
   2548   const uint32 kBucketId = 123;
   2549   const GLint kLocation = 2;
   2550   const char* kName = "testing";
   2551   EXPECT_CALL(*gl_, BindAttribLocation(_, _, _)).Times(0);
   2552   BindAttribLocationBucket cmd;
   2553   // check bucket does not exist.
   2554   cmd.Init(client_program_id_, kLocation, kBucketId);
   2555   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   2556   // check bucket is empty.
   2557   SetBucketAsCString(kBucketId, NULL);
   2558   cmd.Init(client_program_id_, kLocation, kBucketId);
   2559   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   2560   // Check bad program id
   2561   SetBucketAsCString(kBucketId, kName);
   2562   cmd.Init(kInvalidClientId, kLocation, kBucketId);
   2563   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2564   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   2565 }
   2566 
   2567 TEST_F(GLES2DecoderWithShaderTest, GetAttribLocation) {
   2568   const uint32 kNameSize = strlen(kAttrib2Name);
   2569   const char* kNonExistentName = "foobar";
   2570   const uint32 kNonExistentNameSize = strlen(kNonExistentName);
   2571   typedef GetAttribLocation::Result Result;
   2572   Result* result = GetSharedMemoryAs<Result*>();
   2573   *result = -1;
   2574   char* name = GetSharedMemoryAsWithOffset<char*>(sizeof(*result));
   2575   const uint32 kNameOffset = kSharedMemoryOffset + sizeof(*result);
   2576   memcpy(name, kAttrib2Name, kNameSize);
   2577   GetAttribLocation cmd;
   2578   cmd.Init(client_program_id_,
   2579            kSharedMemoryId, kNameOffset,
   2580            kSharedMemoryId, kSharedMemoryOffset,
   2581            kNameSize);
   2582   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2583   EXPECT_EQ(kAttrib2Location, *result);
   2584   *result = -1;
   2585   memcpy(name, kNonExistentName, kNonExistentNameSize);
   2586   cmd.Init(client_program_id_,
   2587            kSharedMemoryId, kNameOffset,
   2588            kSharedMemoryId, kSharedMemoryOffset,
   2589            kNonExistentNameSize);
   2590   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2591   EXPECT_EQ(-1, *result);
   2592 }
   2593 
   2594 TEST_F(GLES2DecoderWithShaderTest, GetAttribLocationInvalidArgs) {
   2595   const uint32 kNameSize = strlen(kAttrib2Name);
   2596   const char* kBadName = "foo\abar";
   2597   const uint32 kBadNameSize = strlen(kBadName);
   2598   typedef GetAttribLocation::Result Result;
   2599   Result* result = GetSharedMemoryAs<Result*>();
   2600   *result = -1;
   2601   char* name = GetSharedMemoryAsWithOffset<char*>(sizeof(*result));
   2602   const uint32 kNameOffset = kSharedMemoryOffset + sizeof(*result);
   2603   memcpy(name, kAttrib2Name, kNameSize);
   2604   GetAttribLocation cmd;
   2605   cmd.Init(kInvalidClientId,
   2606            kSharedMemoryId, kNameOffset,
   2607            kSharedMemoryId, kSharedMemoryOffset,
   2608            kNameSize);
   2609   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2610   EXPECT_EQ(-1, *result);
   2611   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   2612   *result = -1;
   2613   cmd.Init(client_program_id_,
   2614            kInvalidSharedMemoryId, kNameOffset,
   2615            kSharedMemoryId, kSharedMemoryOffset,
   2616            kNameSize);
   2617   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   2618   EXPECT_EQ(-1, *result);
   2619   cmd.Init(client_program_id_,
   2620            kSharedMemoryId, kInvalidSharedMemoryOffset,
   2621            kSharedMemoryId, kSharedMemoryOffset,
   2622            kNameSize);
   2623   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   2624   EXPECT_EQ(-1, *result);
   2625   cmd.Init(client_program_id_,
   2626            kSharedMemoryId, kNameOffset,
   2627            kInvalidSharedMemoryId, kSharedMemoryOffset,
   2628            kNameSize);
   2629   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   2630   EXPECT_EQ(-1, *result);
   2631   cmd.Init(client_program_id_,
   2632            kSharedMemoryId, kNameOffset,
   2633            kSharedMemoryId, kInvalidSharedMemoryOffset,
   2634            kNameSize);
   2635   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   2636   EXPECT_EQ(-1, *result);
   2637   cmd.Init(client_program_id_,
   2638            kSharedMemoryId, kNameOffset,
   2639            kSharedMemoryId, kSharedMemoryOffset,
   2640            kSharedBufferSize);
   2641   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   2642   EXPECT_EQ(-1, *result);
   2643   memcpy(name, kBadName, kBadNameSize);
   2644   cmd.Init(client_program_id_,
   2645            kSharedMemoryId, kNameOffset,
   2646            kSharedMemoryId, kSharedMemoryOffset,
   2647            kBadNameSize);
   2648   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2649   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   2650 }
   2651 
   2652 TEST_F(GLES2DecoderWithShaderTest, GetAttribLocationBucket) {
   2653   const uint32 kBucketId = 123;
   2654   const char* kNonExistentName = "foobar";
   2655   typedef GetAttribLocationBucket::Result Result;
   2656   Result* result = GetSharedMemoryAs<Result*>();
   2657   SetBucketAsCString(kBucketId, kAttrib2Name);
   2658   *result = -1;
   2659   GetAttribLocationBucket cmd;
   2660   cmd.Init(client_program_id_, kBucketId,
   2661            kSharedMemoryId, kSharedMemoryOffset);
   2662   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2663   EXPECT_EQ(kAttrib2Location, *result);
   2664   SetBucketAsCString(kBucketId, kNonExistentName);
   2665   *result = -1;
   2666   cmd.Init(client_program_id_, kBucketId,
   2667            kSharedMemoryId, kSharedMemoryOffset);
   2668   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2669   EXPECT_EQ(-1, *result);
   2670 }
   2671 
   2672 TEST_F(GLES2DecoderWithShaderTest, GetAttribLocationBucketInvalidArgs) {
   2673   const uint32 kBucketId = 123;
   2674   typedef GetAttribLocationBucket::Result Result;
   2675   Result* result = GetSharedMemoryAs<Result*>();
   2676   *result = -1;
   2677   GetAttribLocationBucket cmd;
   2678   // Check no bucket
   2679   cmd.Init(client_program_id_, kBucketId,
   2680            kSharedMemoryId, kSharedMemoryOffset);
   2681   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   2682   EXPECT_EQ(-1, *result);
   2683   // Check bad program id.
   2684   SetBucketAsCString(kBucketId, kAttrib2Name);
   2685   cmd.Init(kInvalidClientId, kBucketId,
   2686            kSharedMemoryId, kSharedMemoryOffset);
   2687   *result = -1;
   2688   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2689   EXPECT_EQ(-1, *result);
   2690   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   2691   // Check bad memory
   2692   cmd.Init(client_program_id_, kBucketId,
   2693            kInvalidSharedMemoryId, kSharedMemoryOffset);
   2694   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   2695   cmd.Init(client_program_id_, kBucketId,
   2696            kSharedMemoryId, kInvalidSharedMemoryOffset);
   2697   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   2698 }
   2699 
   2700 TEST_F(GLES2DecoderWithShaderTest, GetUniformLocation) {
   2701   const uint32 kNameSize = strlen(kUniform2Name);
   2702   const char* kNonExistentName = "foobar";
   2703   const uint32 kNonExistentNameSize = strlen(kNonExistentName);
   2704   typedef GetUniformLocation::Result Result;
   2705   Result* result = GetSharedMemoryAs<Result*>();
   2706   *result = -1;
   2707   char* name = GetSharedMemoryAsWithOffset<char*>(sizeof(*result));
   2708   const uint32 kNameOffset = kSharedMemoryOffset + sizeof(*result);
   2709   memcpy(name, kUniform2Name, kNameSize);
   2710   GetUniformLocation cmd;
   2711   cmd.Init(client_program_id_,
   2712            kSharedMemoryId, kNameOffset,
   2713            kSharedMemoryId, kSharedMemoryOffset,
   2714            kNameSize);
   2715   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2716   EXPECT_EQ(kUniform2FakeLocation, *result);
   2717   memcpy(name, kNonExistentName, kNonExistentNameSize);
   2718   *result = -1;
   2719   cmd.Init(client_program_id_,
   2720            kSharedMemoryId, kNameOffset,
   2721            kSharedMemoryId, kSharedMemoryOffset,
   2722            kNonExistentNameSize);
   2723   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2724   EXPECT_EQ(-1, *result);
   2725 }
   2726 
   2727 TEST_F(GLES2DecoderWithShaderTest, GetUniformLocationInvalidArgs) {
   2728   const uint32 kNameSize = strlen(kUniform2Name);
   2729   const char* kBadName = "foo\abar";
   2730   const uint32 kBadNameSize = strlen(kBadName);
   2731   typedef GetUniformLocation::Result Result;
   2732   Result* result = GetSharedMemoryAs<Result*>();
   2733   *result = -1;
   2734   char* name = GetSharedMemoryAsWithOffset<char*>(sizeof(*result));
   2735   const uint32 kNameOffset = kSharedMemoryOffset + sizeof(*result);
   2736   memcpy(name, kUniform2Name, kNameSize);
   2737   GetUniformLocation cmd;
   2738   cmd.Init(kInvalidClientId,
   2739            kSharedMemoryId, kNameOffset,
   2740            kSharedMemoryId, kSharedMemoryOffset,
   2741            kNameSize);
   2742   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2743   EXPECT_EQ(-1, *result);
   2744   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   2745   *result = -1;
   2746   cmd.Init(client_program_id_,
   2747            kInvalidSharedMemoryId, kNameOffset,
   2748            kSharedMemoryId, kSharedMemoryOffset,
   2749            kNameSize);
   2750   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   2751   EXPECT_EQ(-1, *result);
   2752   cmd.Init(client_program_id_,
   2753            kSharedMemoryId, kInvalidSharedMemoryOffset,
   2754            kSharedMemoryId, kSharedMemoryOffset,
   2755            kNameSize);
   2756   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   2757   EXPECT_EQ(-1, *result);
   2758   cmd.Init(client_program_id_,
   2759            kSharedMemoryId, kNameOffset,
   2760            kInvalidSharedMemoryId, kSharedMemoryOffset,
   2761            kNameSize);
   2762   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   2763   EXPECT_EQ(-1, *result);
   2764   cmd.Init(client_program_id_,
   2765            kSharedMemoryId, kNameOffset,
   2766            kSharedMemoryId, kInvalidSharedMemoryOffset,
   2767            kNameSize);
   2768   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   2769   EXPECT_EQ(-1, *result);
   2770   cmd.Init(client_program_id_,
   2771            kSharedMemoryId, kNameOffset,
   2772            kSharedMemoryId, kSharedMemoryOffset,
   2773            kSharedBufferSize);
   2774   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   2775   EXPECT_EQ(-1, *result);
   2776   memcpy(name, kBadName, kBadNameSize);
   2777   cmd.Init(client_program_id_,
   2778            kSharedMemoryId, kNameOffset,
   2779            kSharedMemoryId, kSharedMemoryOffset,
   2780            kBadNameSize);
   2781   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2782   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   2783 }
   2784 
   2785 TEST_F(GLES2DecoderWithShaderTest, GetUniformLocationBucket) {
   2786   const uint32 kBucketId = 123;
   2787   const char* kNonExistentName = "foobar";
   2788   typedef GetUniformLocationBucket::Result Result;
   2789   Result* result = GetSharedMemoryAs<Result*>();
   2790   SetBucketAsCString(kBucketId, kUniform2Name);
   2791   *result = -1;
   2792   GetUniformLocationBucket cmd;
   2793   cmd.Init(client_program_id_, kBucketId,
   2794            kSharedMemoryId, kSharedMemoryOffset);
   2795   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2796   EXPECT_EQ(kUniform2FakeLocation, *result);
   2797   SetBucketAsCString(kBucketId, kNonExistentName);
   2798   *result = -1;
   2799   cmd.Init(client_program_id_, kBucketId,
   2800            kSharedMemoryId, kSharedMemoryOffset);
   2801   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2802   EXPECT_EQ(-1, *result);
   2803 }
   2804 
   2805 TEST_F(GLES2DecoderWithShaderTest, GetUniformLocationBucketInvalidArgs) {
   2806   const uint32 kBucketId = 123;
   2807   typedef GetUniformLocationBucket::Result Result;
   2808   Result* result = GetSharedMemoryAs<Result*>();
   2809   *result = -1;
   2810   GetUniformLocationBucket cmd;
   2811   // Check no bucket
   2812   cmd.Init(client_program_id_, kBucketId,
   2813            kSharedMemoryId, kSharedMemoryOffset);
   2814   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   2815   EXPECT_EQ(-1, *result);
   2816   // Check bad program id.
   2817   SetBucketAsCString(kBucketId, kUniform2Name);
   2818   cmd.Init(kInvalidClientId, kBucketId,
   2819            kSharedMemoryId, kSharedMemoryOffset);
   2820   *result = -1;
   2821   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2822   EXPECT_EQ(-1, *result);
   2823   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   2824   // Check bad memory
   2825   cmd.Init(client_program_id_, kBucketId,
   2826            kInvalidSharedMemoryId, kSharedMemoryOffset);
   2827   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   2828   cmd.Init(client_program_id_, kBucketId,
   2829            kSharedMemoryId, kInvalidSharedMemoryOffset);
   2830   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   2831 }
   2832 
   2833 TEST_F(GLES2DecoderWithShaderTest, GetMaxValueInBufferCHROMIUM) {
   2834   SetupIndexBuffer();
   2835   GetMaxValueInBufferCHROMIUM::Result* result =
   2836       static_cast<GetMaxValueInBufferCHROMIUM::Result*>(shared_memory_address_);
   2837   *result = 0;
   2838 
   2839   GetMaxValueInBufferCHROMIUM cmd;
   2840   cmd.Init(client_element_buffer_id_, kValidIndexRangeCount, GL_UNSIGNED_SHORT,
   2841            kValidIndexRangeStart * 2, kSharedMemoryId, kSharedMemoryOffset);
   2842   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2843   EXPECT_EQ(7u, *result);
   2844   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   2845   cmd.Init(client_element_buffer_id_, kValidIndexRangeCount + 1,
   2846            GL_UNSIGNED_SHORT,
   2847            kValidIndexRangeStart * 2, kSharedMemoryId, kSharedMemoryOffset);
   2848   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2849   EXPECT_EQ(100u, *result);
   2850   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   2851 
   2852   cmd.Init(kInvalidClientId, kValidIndexRangeCount,
   2853            GL_UNSIGNED_SHORT,
   2854            kValidIndexRangeStart * 2, kSharedMemoryId, kSharedMemoryOffset);
   2855   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2856   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   2857   cmd.Init(client_element_buffer_id_, kOutOfRangeIndexRangeEnd,
   2858            GL_UNSIGNED_SHORT,
   2859            kValidIndexRangeStart * 2, kSharedMemoryId, kSharedMemoryOffset);
   2860   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2861   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   2862   cmd.Init(client_element_buffer_id_, kValidIndexRangeCount + 1,
   2863            GL_UNSIGNED_SHORT,
   2864            kOutOfRangeIndexRangeEnd * 2, kSharedMemoryId, kSharedMemoryOffset);
   2865   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2866   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   2867   cmd.Init(client_element_buffer_id_, kValidIndexRangeCount + 1,
   2868            GL_UNSIGNED_SHORT,
   2869            kValidIndexRangeStart * 2, kSharedMemoryId, kSharedMemoryOffset);
   2870   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2871   cmd.Init(client_buffer_id_, kValidIndexRangeCount + 1,
   2872            GL_UNSIGNED_SHORT,
   2873            kValidIndexRangeStart * 2, kSharedMemoryId, kSharedMemoryOffset);
   2874   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2875   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   2876   cmd.Init(client_element_buffer_id_, kValidIndexRangeCount + 1,
   2877            GL_UNSIGNED_SHORT,
   2878            kValidIndexRangeStart * 2,
   2879            kInvalidSharedMemoryId, kSharedMemoryOffset);
   2880   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   2881   cmd.Init(client_element_buffer_id_, kValidIndexRangeCount + 1,
   2882            GL_UNSIGNED_SHORT,
   2883            kValidIndexRangeStart * 2,
   2884            kSharedMemoryId, kInvalidSharedMemoryOffset);
   2885   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   2886 }
   2887 
   2888 TEST_F(GLES2DecoderTest, SharedIds) {
   2889   GenSharedIdsCHROMIUM gen_cmd;
   2890   RegisterSharedIdsCHROMIUM reg_cmd;
   2891   DeleteSharedIdsCHROMIUM del_cmd;
   2892 
   2893   const GLuint kNamespaceId = id_namespaces::kTextures;
   2894   const GLuint kExpectedId1 = 1;
   2895   const GLuint kExpectedId2 = 2;
   2896   const GLuint kExpectedId3 = 4;
   2897   const GLuint kRegisterId = 3;
   2898   GLuint* ids = GetSharedMemoryAs<GLuint*>();
   2899   gen_cmd.Init(kNamespaceId, 0, 2, kSharedMemoryId, kSharedMemoryOffset);
   2900   EXPECT_EQ(error::kNoError, ExecuteCmd(gen_cmd));
   2901   IdAllocatorInterface* id_allocator = GetIdAllocator(kNamespaceId);
   2902   ASSERT_TRUE(id_allocator != NULL);
   2903   // This check is implementation dependant but it's kind of hard to check
   2904   // otherwise.
   2905   EXPECT_EQ(kExpectedId1, ids[0]);
   2906   EXPECT_EQ(kExpectedId2, ids[1]);
   2907   EXPECT_TRUE(id_allocator->InUse(kExpectedId1));
   2908   EXPECT_TRUE(id_allocator->InUse(kExpectedId2));
   2909   EXPECT_FALSE(id_allocator->InUse(kRegisterId));
   2910   EXPECT_FALSE(id_allocator->InUse(kExpectedId3));
   2911 
   2912   ClearSharedMemory();
   2913   ids[0] = kRegisterId;
   2914   reg_cmd.Init(kNamespaceId, 1, kSharedMemoryId, kSharedMemoryOffset);
   2915   EXPECT_EQ(error::kNoError, ExecuteCmd(reg_cmd));
   2916   EXPECT_TRUE(id_allocator->InUse(kExpectedId1));
   2917   EXPECT_TRUE(id_allocator->InUse(kExpectedId2));
   2918   EXPECT_TRUE(id_allocator->InUse(kRegisterId));
   2919   EXPECT_FALSE(id_allocator->InUse(kExpectedId3));
   2920 
   2921   ClearSharedMemory();
   2922   gen_cmd.Init(kNamespaceId, 0, 1, kSharedMemoryId, kSharedMemoryOffset);
   2923   EXPECT_EQ(error::kNoError, ExecuteCmd(gen_cmd));
   2924   EXPECT_EQ(kExpectedId3, ids[0]);
   2925   EXPECT_TRUE(id_allocator->InUse(kExpectedId1));
   2926   EXPECT_TRUE(id_allocator->InUse(kExpectedId2));
   2927   EXPECT_TRUE(id_allocator->InUse(kRegisterId));
   2928   EXPECT_TRUE(id_allocator->InUse(kExpectedId3));
   2929 
   2930   ClearSharedMemory();
   2931   ids[0] = kExpectedId1;
   2932   ids[1] = kRegisterId;
   2933   del_cmd.Init(kNamespaceId, 2, kSharedMemoryId, kSharedMemoryOffset);
   2934   EXPECT_EQ(error::kNoError, ExecuteCmd(del_cmd));
   2935   EXPECT_FALSE(id_allocator->InUse(kExpectedId1));
   2936   EXPECT_TRUE(id_allocator->InUse(kExpectedId2));
   2937   EXPECT_FALSE(id_allocator->InUse(kRegisterId));
   2938   EXPECT_TRUE(id_allocator->InUse(kExpectedId3));
   2939 
   2940   ClearSharedMemory();
   2941   ids[0] = kExpectedId3;
   2942   ids[1] = kExpectedId2;
   2943   del_cmd.Init(kNamespaceId, 2, kSharedMemoryId, kSharedMemoryOffset);
   2944   EXPECT_EQ(error::kNoError, ExecuteCmd(del_cmd));
   2945   EXPECT_FALSE(id_allocator->InUse(kExpectedId1));
   2946   EXPECT_FALSE(id_allocator->InUse(kExpectedId2));
   2947   EXPECT_FALSE(id_allocator->InUse(kRegisterId));
   2948   EXPECT_FALSE(id_allocator->InUse(kExpectedId3));
   2949 
   2950   // Check passing in an id_offset.
   2951   ClearSharedMemory();
   2952   const GLuint kOffset = 0xABCDEF;
   2953   gen_cmd.Init(kNamespaceId, kOffset, 2, kSharedMemoryId, kSharedMemoryOffset);
   2954   EXPECT_EQ(error::kNoError, ExecuteCmd(gen_cmd));
   2955   EXPECT_EQ(kOffset, ids[0]);
   2956   EXPECT_EQ(kOffset + 1, ids[1]);
   2957 }
   2958 
   2959 TEST_F(GLES2DecoderTest, GenSharedIdsCHROMIUMBadArgs) {
   2960   const GLuint kNamespaceId = id_namespaces::kTextures;
   2961   GenSharedIdsCHROMIUM cmd;
   2962   cmd.Init(kNamespaceId, 0, -1, kSharedMemoryId, kSharedMemoryOffset);
   2963   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   2964   cmd.Init(kNamespaceId, 0, 1, kInvalidSharedMemoryId, kSharedMemoryOffset);
   2965   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   2966   cmd.Init(kNamespaceId, 0, 1, kSharedMemoryId, kInvalidSharedMemoryOffset);
   2967   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   2968 }
   2969 
   2970 TEST_F(GLES2DecoderTest, RegisterSharedIdsCHROMIUMBadArgs) {
   2971   const GLuint kNamespaceId = id_namespaces::kTextures;
   2972   RegisterSharedIdsCHROMIUM cmd;
   2973   cmd.Init(kNamespaceId, -1, kSharedMemoryId, kSharedMemoryOffset);
   2974   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   2975   cmd.Init(kNamespaceId, 1, kInvalidSharedMemoryId, kSharedMemoryOffset);
   2976   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   2977   cmd.Init(kNamespaceId, 1, kSharedMemoryId, kInvalidSharedMemoryOffset);
   2978   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   2979 }
   2980 
   2981 TEST_F(GLES2DecoderTest, RegisterSharedIdsCHROMIUMDuplicateIds) {
   2982   const GLuint kNamespaceId = id_namespaces::kTextures;
   2983   const GLuint kRegisterId = 3;
   2984   RegisterSharedIdsCHROMIUM cmd;
   2985   GLuint* ids = GetSharedMemoryAs<GLuint*>();
   2986   ids[0] = kRegisterId;
   2987   cmd.Init(kNamespaceId, 1, kSharedMemoryId, kSharedMemoryOffset);
   2988   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2989   cmd.Init(kNamespaceId, 1, kSharedMemoryId, kSharedMemoryOffset);
   2990   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2991   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   2992 }
   2993 
   2994 TEST_F(GLES2DecoderTest, DeleteSharedIdsCHROMIUMBadArgs) {
   2995   const GLuint kNamespaceId = id_namespaces::kTextures;
   2996   DeleteSharedIdsCHROMIUM cmd;
   2997   cmd.Init(kNamespaceId, -1, kSharedMemoryId, kSharedMemoryOffset);
   2998   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   2999   cmd.Init(kNamespaceId, 1, kInvalidSharedMemoryId, kSharedMemoryOffset);
   3000   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   3001   cmd.Init(kNamespaceId, 1, kSharedMemoryId, kInvalidSharedMemoryOffset);
   3002   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   3003 }
   3004 
   3005 TEST_F(GLES2DecoderTest, TexSubImage2DValidArgs) {
   3006   const int kWidth = 16;
   3007   const int kHeight = 8;
   3008   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   3009   DoTexImage2D(
   3010       GL_TEXTURE_2D, 1, GL_RGBA, kWidth, kHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   3011       kSharedMemoryId, kSharedMemoryOffset);
   3012   EXPECT_CALL(*gl_, TexSubImage2D(
   3013       GL_TEXTURE_2D, 1, 1, 0, kWidth - 1, kHeight, GL_RGBA, GL_UNSIGNED_BYTE,
   3014       shared_memory_address_))
   3015       .Times(1)
   3016       .RetiresOnSaturation();
   3017   TexSubImage2D cmd;
   3018   cmd.Init(
   3019       GL_TEXTURE_2D, 1, 1, 0, kWidth - 1, kHeight, GL_RGBA, GL_UNSIGNED_BYTE,
   3020       kSharedMemoryId, kSharedMemoryOffset, GL_FALSE);
   3021   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   3022   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   3023 }
   3024 
   3025 TEST_F(GLES2DecoderTest, TexSubImage2DBadArgs) {
   3026   const int kWidth = 16;
   3027   const int kHeight = 8;
   3028   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   3029   DoTexImage2D(
   3030       GL_TEXTURE_2D, 1, GL_RGBA, kWidth, kHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   3031       0, 0);
   3032   TexSubImage2D cmd;
   3033   cmd.Init(GL_TEXTURE0, 1, 0, 0, kWidth, kHeight, GL_RGBA, GL_UNSIGNED_BYTE,
   3034            kSharedMemoryId, kSharedMemoryOffset, GL_FALSE);
   3035   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   3036   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
   3037   cmd.Init(GL_TEXTURE_2D, 1, 0, 0, kWidth, kHeight, GL_TRUE, GL_UNSIGNED_BYTE,
   3038            kSharedMemoryId, kSharedMemoryOffset, GL_FALSE);
   3039   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   3040   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
   3041   cmd.Init(GL_TEXTURE_2D, 1, 0, 0, kWidth, kHeight, GL_RGBA, GL_UNSIGNED_INT,
   3042            kSharedMemoryId, kSharedMemoryOffset, GL_FALSE);
   3043   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   3044   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
   3045   cmd.Init(GL_TEXTURE_2D, 1, -1, 0, kWidth, kHeight, GL_RGBA, GL_UNSIGNED_BYTE,
   3046            kSharedMemoryId, kSharedMemoryOffset, GL_FALSE);
   3047   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   3048   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   3049   cmd.Init(GL_TEXTURE_2D, 1, 1, 0, kWidth, kHeight, GL_RGBA, GL_UNSIGNED_BYTE,
   3050            kSharedMemoryId, kSharedMemoryOffset, GL_FALSE);
   3051   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   3052   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   3053   cmd.Init(GL_TEXTURE_2D, 1, 0, -1, kWidth, kHeight, GL_RGBA, GL_UNSIGNED_BYTE,
   3054            kSharedMemoryId, kSharedMemoryOffset, GL_FALSE);
   3055   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   3056   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   3057   cmd.Init(GL_TEXTURE_2D, 1, 0, 1, kWidth, kHeight, GL_RGBA, GL_UNSIGNED_BYTE,
   3058            kSharedMemoryId, kSharedMemoryOffset, GL_FALSE);
   3059   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   3060   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   3061   cmd.Init(GL_TEXTURE_2D, 1, 0, 0, kWidth + 1, kHeight, GL_RGBA,
   3062            GL_UNSIGNED_BYTE, kSharedMemoryId, kSharedMemoryOffset, GL_FALSE);
   3063   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   3064   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   3065   cmd.Init(GL_TEXTURE_2D, 1, 0, 0, kWidth, kHeight + 1, GL_RGBA,
   3066            GL_UNSIGNED_BYTE, kSharedMemoryId, kSharedMemoryOffset, GL_FALSE);
   3067   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   3068   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   3069   cmd.Init(GL_TEXTURE_2D, 1, 0, 0, kWidth, kHeight, GL_RGB, GL_UNSIGNED_BYTE,
   3070            kSharedMemoryId, kSharedMemoryOffset, GL_FALSE);
   3071   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   3072   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   3073   cmd.Init(GL_TEXTURE_2D, 1, 0, 0, kWidth, kHeight, GL_RGBA,
   3074            GL_UNSIGNED_SHORT_4_4_4_4, kSharedMemoryId, kSharedMemoryOffset,
   3075            GL_FALSE);
   3076   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   3077   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   3078   cmd.Init(GL_TEXTURE_2D, 1, 0, 0, kWidth, kHeight, GL_RGBA, GL_UNSIGNED_BYTE,
   3079            kInvalidSharedMemoryId, kSharedMemoryOffset, GL_FALSE);
   3080   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   3081   cmd.Init(GL_TEXTURE_2D, 1, 0, 0, kWidth, kHeight, GL_RGBA, GL_UNSIGNED_BYTE,
   3082            kSharedMemoryId, kInvalidSharedMemoryOffset, GL_FALSE);
   3083   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   3084 }
   3085 
   3086 TEST_F(GLES2DecoderTest, CopyTexSubImage2DValidArgs) {
   3087   const int kWidth = 16;
   3088   const int kHeight = 8;
   3089   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   3090   DoTexImage2D(
   3091       GL_TEXTURE_2D, 1, GL_RGBA, kWidth, kHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   3092       kSharedMemoryId, kSharedMemoryOffset);
   3093   EXPECT_CALL(*gl_, CopyTexSubImage2D(
   3094       GL_TEXTURE_2D, 1, 0, 0, 0, 0, kWidth, kHeight))
   3095       .Times(1)
   3096       .RetiresOnSaturation();
   3097   CopyTexSubImage2D cmd;
   3098   cmd.Init(GL_TEXTURE_2D, 1, 0, 0, 0, 0, kWidth, kHeight);
   3099   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   3100   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   3101 }
   3102 
   3103 TEST_F(GLES2DecoderTest, CopyTexSubImage2DBadArgs) {
   3104   const int kWidth = 16;
   3105   const int kHeight = 8;
   3106   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   3107   DoTexImage2D(
   3108       GL_TEXTURE_2D, 1, GL_RGBA, kWidth, kHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   3109       0, 0);
   3110   CopyTexSubImage2D cmd;
   3111   cmd.Init(GL_TEXTURE0, 1, 0, 0, 0, 0, kWidth, kHeight);
   3112   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   3113   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
   3114   cmd.Init(GL_TEXTURE_2D, 1, -1, 0, 0, 0, kWidth, kHeight);
   3115   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   3116   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   3117   cmd.Init(GL_TEXTURE_2D, 1, 1, 0, 0, 0, kWidth, kHeight);
   3118   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   3119   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   3120   cmd.Init(GL_TEXTURE_2D, 1, 0, -1, 0, 0, kWidth, kHeight);
   3121   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   3122   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   3123   cmd.Init(GL_TEXTURE_2D, 1, 0, 1, 0, 0, kWidth, kHeight);
   3124   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   3125   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   3126   cmd.Init(GL_TEXTURE_2D, 1, 0, 0, 0, 0, kWidth + 1, kHeight);
   3127   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   3128   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   3129   cmd.Init(GL_TEXTURE_2D, 1, 0, 0, 0, 0, kWidth, kHeight + 1);
   3130   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   3131   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   3132 }
   3133 
   3134 // Check that if a renderbuffer is attached and GL returns
   3135 // GL_FRAMEBUFFER_COMPLETE that the buffer is cleared and state is restored.
   3136 TEST_F(GLES2DecoderTest, FramebufferRenderbufferClearColor) {
   3137   DoBindFramebuffer(GL_FRAMEBUFFER, client_framebuffer_id_,
   3138                     kServiceFramebufferId);
   3139   ClearColor color_cmd;
   3140   ColorMask color_mask_cmd;
   3141   Enable enable_cmd;
   3142   FramebufferRenderbuffer cmd;
   3143   color_cmd.Init(0.1f, 0.2f, 0.3f, 0.4f);
   3144   color_mask_cmd.Init(0, 1, 0, 1);
   3145   enable_cmd.Init(GL_SCISSOR_TEST);
   3146   cmd.Init(
   3147       GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER,
   3148       client_renderbuffer_id_);
   3149   InSequence sequence;
   3150   EXPECT_CALL(*gl_, ClearColor(0.1f, 0.2f, 0.3f, 0.4f))
   3151       .Times(1)
   3152       .RetiresOnSaturation();
   3153   EXPECT_CALL(*gl_, GetError())
   3154       .WillOnce(Return(GL_NO_ERROR))
   3155       .RetiresOnSaturation();
   3156   EXPECT_CALL(*gl_, FramebufferRenderbufferEXT(
   3157       GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER,
   3158       kServiceRenderbufferId))
   3159       .Times(1)
   3160       .RetiresOnSaturation();
   3161   EXPECT_CALL(*gl_, GetError())
   3162       .WillOnce(Return(GL_NO_ERROR))
   3163       .RetiresOnSaturation();
   3164   EXPECT_EQ(error::kNoError, ExecuteCmd(color_cmd));
   3165   EXPECT_EQ(error::kNoError, ExecuteCmd(color_mask_cmd));
   3166   EXPECT_EQ(error::kNoError, ExecuteCmd(enable_cmd));
   3167   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   3168 }
   3169 
   3170 TEST_F(GLES2DecoderTest, FramebufferRenderbufferClearDepth) {
   3171   DoBindFramebuffer(GL_FRAMEBUFFER, client_framebuffer_id_,
   3172                     kServiceFramebufferId);
   3173   ClearDepthf depth_cmd;
   3174   DepthMask depth_mask_cmd;
   3175   FramebufferRenderbuffer cmd;
   3176   depth_cmd.Init(0.5f);
   3177   depth_mask_cmd.Init(false);
   3178   cmd.Init(
   3179       GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER,
   3180       client_renderbuffer_id_);
   3181   InSequence sequence;
   3182   EXPECT_CALL(*gl_, ClearDepth(0.5f))
   3183       .Times(1)
   3184       .RetiresOnSaturation();
   3185   EXPECT_CALL(*gl_, GetError())
   3186       .WillOnce(Return(GL_NO_ERROR))
   3187       .RetiresOnSaturation();
   3188   EXPECT_CALL(*gl_, FramebufferRenderbufferEXT(
   3189       GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER,
   3190       kServiceRenderbufferId))
   3191       .Times(1)
   3192       .RetiresOnSaturation();
   3193   EXPECT_CALL(*gl_, GetError())
   3194       .WillOnce(Return(GL_NO_ERROR))
   3195       .RetiresOnSaturation();
   3196   EXPECT_EQ(error::kNoError, ExecuteCmd(depth_cmd));
   3197   EXPECT_EQ(error::kNoError, ExecuteCmd(depth_mask_cmd));
   3198   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   3199 }
   3200 
   3201 TEST_F(GLES2DecoderTest, FramebufferRenderbufferClearStencil) {
   3202   DoBindFramebuffer(GL_FRAMEBUFFER, client_framebuffer_id_,
   3203                     kServiceFramebufferId);
   3204   ClearStencil stencil_cmd;
   3205   StencilMaskSeparate stencil_mask_separate_cmd;
   3206   FramebufferRenderbuffer cmd;
   3207   stencil_cmd.Init(123);
   3208   stencil_mask_separate_cmd.Init(GL_BACK, 0x1234u);
   3209   cmd.Init(
   3210       GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER,
   3211       client_renderbuffer_id_);
   3212   InSequence sequence;
   3213   EXPECT_CALL(*gl_, ClearStencil(123))
   3214       .Times(1)
   3215       .RetiresOnSaturation();
   3216   EXPECT_CALL(*gl_, GetError())
   3217       .WillOnce(Return(GL_NO_ERROR))
   3218       .RetiresOnSaturation();
   3219   EXPECT_CALL(*gl_, FramebufferRenderbufferEXT(
   3220       GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER,
   3221       kServiceRenderbufferId))
   3222       .Times(1)
   3223       .RetiresOnSaturation();
   3224   EXPECT_CALL(*gl_, GetError())
   3225       .WillOnce(Return(GL_NO_ERROR))
   3226       .RetiresOnSaturation();
   3227   EXPECT_EQ(error::kNoError, ExecuteCmd(stencil_cmd));
   3228   EXPECT_EQ(error::kNoError, ExecuteCmd(stencil_mask_separate_cmd));
   3229   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   3230 }
   3231 
   3232 TEST_F(GLES2DecoderTest, IsBuffer) {
   3233   EXPECT_FALSE(DoIsBuffer(client_buffer_id_));
   3234   DoBindBuffer(GL_ARRAY_BUFFER, client_buffer_id_, kServiceBufferId);
   3235   EXPECT_TRUE(DoIsBuffer(client_buffer_id_));
   3236   DoDeleteBuffer(client_buffer_id_, kServiceBufferId);
   3237   EXPECT_FALSE(DoIsBuffer(client_buffer_id_));
   3238 }
   3239 
   3240 TEST_F(GLES2DecoderTest, IsFramebuffer) {
   3241   EXPECT_FALSE(DoIsFramebuffer(client_framebuffer_id_));
   3242   DoBindFramebuffer(GL_FRAMEBUFFER, client_framebuffer_id_,
   3243                     kServiceFramebufferId);
   3244   EXPECT_TRUE(DoIsFramebuffer(client_framebuffer_id_));
   3245   DoDeleteFramebuffer(
   3246       client_framebuffer_id_, kServiceFramebufferId,
   3247       true, GL_FRAMEBUFFER, 0,
   3248       true, GL_FRAMEBUFFER, 0);
   3249   EXPECT_FALSE(DoIsFramebuffer(client_framebuffer_id_));
   3250 }
   3251 
   3252 TEST_F(GLES2DecoderTest, IsProgram) {
   3253   // IsProgram is true as soon as the program is created.
   3254   EXPECT_TRUE(DoIsProgram(client_program_id_));
   3255   EXPECT_CALL(*gl_, DeleteProgram(kServiceProgramId))
   3256       .Times(1)
   3257       .RetiresOnSaturation();
   3258   DoDeleteProgram(client_program_id_, kServiceProgramId);
   3259   EXPECT_FALSE(DoIsProgram(client_program_id_));
   3260 
   3261 }
   3262 
   3263 TEST_F(GLES2DecoderTest, IsRenderbuffer) {
   3264   EXPECT_FALSE(DoIsRenderbuffer(client_renderbuffer_id_));
   3265   DoBindRenderbuffer(GL_RENDERBUFFER, client_renderbuffer_id_,
   3266                     kServiceRenderbufferId);
   3267   EXPECT_TRUE(DoIsRenderbuffer(client_renderbuffer_id_));
   3268   DoDeleteRenderbuffer(client_renderbuffer_id_, kServiceRenderbufferId);
   3269   EXPECT_FALSE(DoIsRenderbuffer(client_renderbuffer_id_));
   3270 }
   3271 
   3272 TEST_F(GLES2DecoderTest, IsShader) {
   3273   // IsShader is true as soon as the program is created.
   3274   EXPECT_TRUE(DoIsShader(client_shader_id_));
   3275   DoDeleteShader(client_shader_id_, kServiceShaderId);
   3276   EXPECT_FALSE(DoIsShader(client_shader_id_));
   3277 }
   3278 
   3279 TEST_F(GLES2DecoderTest, IsTexture) {
   3280   EXPECT_FALSE(DoIsTexture(client_texture_id_));
   3281   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   3282   EXPECT_TRUE(DoIsTexture(client_texture_id_));
   3283   DoDeleteTexture(client_texture_id_, kServiceTextureId);
   3284   EXPECT_FALSE(DoIsTexture(client_texture_id_));
   3285 }
   3286 
   3287 #if 0  // Turn this test on once we allow GL_DEPTH_STENCIL_ATTACHMENT
   3288 TEST_F(GLES2DecoderTest, FramebufferRenderbufferClearDepthStencil) {
   3289   DoBindFramebuffer(GL_FRAMEBUFFER, client_framebuffer_id_,
   3290                     kServiceFramebufferId);
   3291   ClearDepthf depth_cmd;
   3292   ClearStencil stencil_cmd;
   3293   FramebufferRenderbuffer cmd;
   3294   depth_cmd.Init(0.5f);
   3295   stencil_cmd.Init(123);
   3296   cmd.Init(
   3297       GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER,
   3298       client_renderbuffer_id_);
   3299   InSequence sequence;
   3300   EXPECT_CALL(*gl_, ClearDepth(0.5f))
   3301       .Times(1)
   3302       .RetiresOnSaturation();
   3303   EXPECT_CALL(*gl_, ClearStencil(123))
   3304       .Times(1)
   3305       .RetiresOnSaturation();
   3306   EXPECT_CALL(*gl_, FramebufferRenderbufferEXT(
   3307       GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER,
   3308       kServiceRenderbufferId))
   3309       .Times(1)
   3310       .RetiresOnSaturation();
   3311   EXPECT_EQ(error::kNoError, ExecuteCmd(depth_cmd));
   3312   EXPECT_EQ(error::kNoError, ExecuteCmd(stencil_cmd));
   3313   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   3314 }
   3315 #endif
   3316 
   3317 TEST_F(GLES2DecoderWithShaderTest, VertexAttribPointer) {
   3318   SetupVertexBuffer();
   3319   static const GLenum types[] = {
   3320     GL_BYTE,
   3321     GL_UNSIGNED_BYTE,
   3322     GL_SHORT,
   3323     GL_UNSIGNED_SHORT,
   3324     GL_FLOAT,
   3325     GL_FIXED,
   3326     GL_INT,
   3327     GL_UNSIGNED_INT,
   3328   };
   3329   static const GLsizei sizes[] = {
   3330     1,
   3331     1,
   3332     2,
   3333     2,
   3334     4,
   3335     4,
   3336     4,
   3337     4,
   3338   };
   3339   static const GLuint indices[] = {
   3340     0,
   3341     1,
   3342     kNumVertexAttribs - 1,
   3343     kNumVertexAttribs,
   3344   };
   3345   static const GLsizei offset_mult[] = {
   3346     0,
   3347     0,
   3348     1,
   3349     1,
   3350     2,
   3351     1000,
   3352   };
   3353   static const GLsizei offset_offset[] = {
   3354     0,
   3355     1,
   3356     0,
   3357     1,
   3358     0,
   3359     0,
   3360   };
   3361   static const GLsizei stride_mult[] = {
   3362     -1,
   3363     0,
   3364     0,
   3365     1,
   3366     1,
   3367     2,
   3368     1000,
   3369   };
   3370   static const GLsizei stride_offset[] = {
   3371     0,
   3372     0,
   3373     1,
   3374     0,
   3375     1,
   3376     0,
   3377     0,
   3378   };
   3379   for (size_t tt = 0; tt < arraysize(types); ++tt) {
   3380     GLenum type = types[tt];
   3381     GLsizei num_bytes = sizes[tt];
   3382     for (size_t ii = 0; ii < arraysize(indices); ++ii) {
   3383       GLuint index = indices[ii];
   3384       for (GLint size = 0; size < 5; ++size) {
   3385         for (size_t oo = 0; oo < arraysize(offset_mult); ++oo) {
   3386           GLuint offset = num_bytes * offset_mult[oo] + offset_offset[oo];
   3387           for (size_t ss = 0; ss < arraysize(stride_mult); ++ss) {
   3388             GLsizei stride = num_bytes * stride_mult[ss] + stride_offset[ss];
   3389             for (int normalize = 0; normalize < 2; ++normalize) {
   3390               bool index_good = index < static_cast<GLuint>(kNumVertexAttribs);
   3391               bool size_good = (size > 0 && size < 5);
   3392               bool offset_good = (offset % num_bytes == 0);
   3393               bool stride_good = (stride % num_bytes == 0) && stride >= 0 &&
   3394                                  stride <= 255;
   3395               bool type_good = (type != GL_INT && type != GL_UNSIGNED_INT &&
   3396                                 type != GL_FIXED);
   3397               bool good = size_good && offset_good && stride_good &&
   3398                           type_good && index_good;
   3399               bool call = good && (type != GL_FIXED);
   3400               if (call) {
   3401                 EXPECT_CALL(*gl_, VertexAttribPointer(
   3402                     index, size, type, normalize, stride,
   3403                     BufferOffset(offset)));
   3404               }
   3405               VertexAttribPointer cmd;
   3406               cmd.Init(index, size, type, normalize, stride, offset);
   3407               EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   3408               if (good) {
   3409                 EXPECT_EQ(GL_NO_ERROR, GetGLError());
   3410               } else if (size_good &&
   3411                          offset_good &&
   3412                          stride_good &&
   3413                          type_good &&
   3414                          !index_good) {
   3415                 EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   3416               } else if (size_good &&
   3417                          offset_good &&
   3418                          stride_good &&
   3419                          !type_good &&
   3420                          index_good) {
   3421                 EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
   3422               } else if (size_good &&
   3423                          offset_good &&
   3424                          !stride_good &&
   3425                          type_good &&
   3426                          index_good) {
   3427                 if (stride < 0 || stride > 255) {
   3428                   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   3429                 } else {
   3430                   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   3431                 }
   3432               } else if (size_good &&
   3433                          !offset_good &&
   3434                          stride_good &&
   3435                          type_good &&
   3436                          index_good) {
   3437                 EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   3438               } else if (!size_good &&
   3439                          offset_good &&
   3440                          stride_good &&
   3441                          type_good &&
   3442                          index_good) {
   3443                 EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   3444               } else {
   3445                 EXPECT_NE(GL_NO_ERROR, GetGLError());
   3446               }
   3447             }
   3448           }
   3449         }
   3450       }
   3451     }
   3452   }
   3453 }
   3454 
   3455 // Test that with an RGB backbuffer if we set the color mask to 1,1,1,1 it is
   3456 // set to 1,1,1,0 at Draw time but is 1,1,1,1 at query time.
   3457 TEST_F(GLES2DecoderRGBBackbufferTest, RGBBackbufferColorMask) {
   3458   ColorMask cmd;
   3459   cmd.Init(true, true, true, true);
   3460   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   3461   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   3462 
   3463   SetupTexture();
   3464   AddExpectationsForSimulatedAttrib0(kNumVertices, 0);
   3465   SetupExpectationsForApplyingDirtyState(
   3466       true,    // Framebuffer is RGB
   3467       false,   // Framebuffer has depth
   3468       false,   // Framebuffer has stencil
   3469       0x1110,  // color bits
   3470       false,   // depth mask
   3471       false,   // depth enabled
   3472       0,       // front stencil mask
   3473       0,       // back stencil mask
   3474       false,   // stencil enabled
   3475       false,   // cull_face_enabled
   3476       false,   // scissor_test_enabled
   3477       false);  // blend_enabled
   3478 
   3479   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   3480       .Times(1)
   3481       .RetiresOnSaturation();
   3482   DrawArrays draw_cmd;
   3483   draw_cmd.Init(GL_TRIANGLES, 0, kNumVertices);
   3484   EXPECT_EQ(error::kNoError, ExecuteCmd(draw_cmd));
   3485   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   3486 
   3487   EXPECT_CALL(*gl_, GetError())
   3488       .WillOnce(Return(GL_NO_ERROR))
   3489       .WillOnce(Return(GL_NO_ERROR))
   3490       .RetiresOnSaturation();
   3491   typedef GetIntegerv::Result Result;
   3492   Result* result = static_cast<Result*>(shared_memory_address_);
   3493   EXPECT_CALL(*gl_, GetIntegerv(GL_COLOR_WRITEMASK, result->GetData()))
   3494       .Times(0);
   3495   result->size = 0;
   3496   GetIntegerv cmd2;
   3497   cmd2.Init(GL_COLOR_WRITEMASK, shared_memory_id_, shared_memory_offset_);
   3498   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd2));
   3499   EXPECT_EQ(
   3500       decoder_->GetGLES2Util()->GLGetNumValuesReturned(GL_COLOR_WRITEMASK),
   3501       result->GetNumResults());
   3502   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   3503   EXPECT_EQ(1, result->GetData()[0]);
   3504   EXPECT_EQ(1, result->GetData()[1]);
   3505   EXPECT_EQ(1, result->GetData()[2]);
   3506   EXPECT_EQ(1, result->GetData()[3]);
   3507 }
   3508 
   3509 // Test that with no depth if we set DepthMask true that it's set to false at
   3510 // draw time but querying it returns true.
   3511 TEST_F(GLES2DecoderRGBBackbufferTest, RGBBackbufferDepthMask) {
   3512   EXPECT_CALL(*gl_, DepthMask(true))
   3513       .Times(0)
   3514       .RetiresOnSaturation();
   3515   DepthMask cmd;
   3516   cmd.Init(true);
   3517   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   3518   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   3519 
   3520   SetupTexture();
   3521   AddExpectationsForSimulatedAttrib0(kNumVertices, 0);
   3522   SetupExpectationsForApplyingDirtyState(
   3523       true,    // Framebuffer is RGB
   3524       false,   // Framebuffer has depth
   3525       false,   // Framebuffer has stencil
   3526       0x1110,  // color bits
   3527       false,   // depth mask
   3528       false,   // depth enabled
   3529       0,       // front stencil mask
   3530       0,       // back stencil mask
   3531       false,   // stencil enabled
   3532       false,   // cull_face_enabled
   3533       false,   // scissor_test_enabled
   3534       false);  // blend_enabled
   3535 
   3536   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   3537       .Times(1)
   3538       .RetiresOnSaturation();
   3539   DrawArrays draw_cmd;
   3540   draw_cmd.Init(GL_TRIANGLES, 0, kNumVertices);
   3541   EXPECT_EQ(error::kNoError, ExecuteCmd(draw_cmd));
   3542   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   3543 
   3544   EXPECT_CALL(*gl_, GetError())
   3545       .WillOnce(Return(GL_NO_ERROR))
   3546       .WillOnce(Return(GL_NO_ERROR))
   3547       .RetiresOnSaturation();
   3548   typedef GetIntegerv::Result Result;
   3549   Result* result = static_cast<Result*>(shared_memory_address_);
   3550   EXPECT_CALL(*gl_, GetIntegerv(GL_DEPTH_WRITEMASK, result->GetData()))
   3551       .Times(0);
   3552   result->size = 0;
   3553   GetIntegerv cmd2;
   3554   cmd2.Init(GL_DEPTH_WRITEMASK, shared_memory_id_, shared_memory_offset_);
   3555   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd2));
   3556   EXPECT_EQ(
   3557       decoder_->GetGLES2Util()->GLGetNumValuesReturned(GL_DEPTH_WRITEMASK),
   3558       result->GetNumResults());
   3559   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   3560   EXPECT_EQ(1, result->GetData()[0]);
   3561 }
   3562 
   3563 // Test that with no stencil if we set the stencil mask it's still set to 0 at
   3564 // draw time but gets our value if we query.
   3565 TEST_F(GLES2DecoderRGBBackbufferTest, RGBBackbufferStencilMask) {
   3566   const GLint kMask = 123;
   3567   EXPECT_CALL(*gl_, StencilMask(kMask))
   3568       .Times(0)
   3569       .RetiresOnSaturation();
   3570   StencilMask cmd;
   3571   cmd.Init(kMask);
   3572   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   3573   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   3574 
   3575   SetupTexture();
   3576   AddExpectationsForSimulatedAttrib0(kNumVertices, 0);
   3577   SetupExpectationsForApplyingDirtyState(
   3578       true,    // Framebuffer is RGB
   3579       false,   // Framebuffer has depth
   3580       false,   // Framebuffer has stencil
   3581       0x1110,  // color bits
   3582       false,   // depth mask
   3583       false,   // depth enabled
   3584       0,       // front stencil mask
   3585       0,       // back stencil mask
   3586       false,   // stencil enabled
   3587       false,   // cull_face_enabled
   3588       false,   // scissor_test_enabled
   3589       false);  // blend_enabled
   3590 
   3591   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   3592       .Times(1)
   3593       .RetiresOnSaturation();
   3594   DrawArrays draw_cmd;
   3595   draw_cmd.Init(GL_TRIANGLES, 0, kNumVertices);
   3596   EXPECT_EQ(error::kNoError, ExecuteCmd(draw_cmd));
   3597   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   3598 
   3599   EXPECT_CALL(*gl_, GetError())
   3600       .WillOnce(Return(GL_NO_ERROR))
   3601       .WillOnce(Return(GL_NO_ERROR))
   3602       .RetiresOnSaturation();
   3603   typedef GetIntegerv::Result Result;
   3604   Result* result = static_cast<Result*>(shared_memory_address_);
   3605   EXPECT_CALL(*gl_, GetIntegerv(GL_STENCIL_WRITEMASK, result->GetData()))
   3606       .Times(0);
   3607   result->size = 0;
   3608   GetIntegerv cmd2;
   3609   cmd2.Init(GL_STENCIL_WRITEMASK, shared_memory_id_, shared_memory_offset_);
   3610   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd2));
   3611   EXPECT_EQ(
   3612       decoder_->GetGLES2Util()->GLGetNumValuesReturned(GL_STENCIL_WRITEMASK),
   3613       result->GetNumResults());
   3614   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   3615   EXPECT_EQ(kMask, result->GetData()[0]);
   3616 }
   3617 
   3618 // Test that if an FBO is bound we get the correct masks.
   3619 TEST_F(GLES2DecoderRGBBackbufferTest, RGBBackbufferColorMaskFBO) {
   3620   ColorMask cmd;
   3621   cmd.Init(true, true, true, true);
   3622   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   3623   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   3624 
   3625   SetupTexture();
   3626   SetupVertexBuffer();
   3627   DoEnableVertexAttribArray(0);
   3628   DoVertexAttribPointer(0, 2, GL_FLOAT, 0, 0);
   3629   DoEnableVertexAttribArray(1);
   3630   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
   3631   DoEnableVertexAttribArray(2);
   3632   DoVertexAttribPointer(2, 2, GL_FLOAT, 0, 0);
   3633   SetupExpectationsForApplyingDirtyState(
   3634       true,    // Framebuffer is RGB
   3635       false,   // Framebuffer has depth
   3636       false,   // Framebuffer has stencil
   3637       0x1110,  // color bits
   3638       false,   // depth mask
   3639       false,   // depth enabled
   3640       0,       // front stencil mask
   3641       0,       // back stencil mask
   3642       false,   // stencil enabled
   3643       false,   // cull_face_enabled
   3644       false,   // scissor_test_enabled
   3645       false);  // blend_enabled
   3646 
   3647   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   3648       .Times(1)
   3649       .RetiresOnSaturation();
   3650   DrawArrays draw_cmd;
   3651   draw_cmd.Init(GL_TRIANGLES, 0, kNumVertices);
   3652   EXPECT_EQ(error::kNoError, ExecuteCmd(draw_cmd));
   3653   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   3654 
   3655   // Check that no extra calls are made on the next draw.
   3656   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   3657       .Times(1)
   3658       .RetiresOnSaturation();
   3659   EXPECT_EQ(error::kNoError, ExecuteCmd(draw_cmd));
   3660   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   3661 
   3662   // Setup Frame buffer.
   3663   // needs to be 1x1 or else it's not renderable.
   3664   const GLsizei kWidth = 1;
   3665   const GLsizei kHeight = 1;
   3666   const GLenum kFormat = GL_RGB;
   3667   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   3668   // Pass some data so the texture will be marked as cleared.
   3669   DoTexImage2D(
   3670       GL_TEXTURE_2D, 0, kFormat, kWidth, kHeight, 0,
   3671       kFormat, GL_UNSIGNED_BYTE, kSharedMemoryId, kSharedMemoryOffset);
   3672   DoBindFramebuffer(
   3673       GL_FRAMEBUFFER, client_framebuffer_id_, kServiceFramebufferId);
   3674   DoFramebufferTexture2D(
   3675       GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
   3676       client_texture_id_, kServiceTextureId, 0, GL_NO_ERROR);
   3677   EXPECT_CALL(*gl_, CheckFramebufferStatusEXT(GL_FRAMEBUFFER))
   3678       .WillOnce(Return(GL_FRAMEBUFFER_COMPLETE))
   3679       .RetiresOnSaturation();
   3680 
   3681   // This time state needs to be set.
   3682   SetupExpectationsForApplyingDirtyState(
   3683       false,    // Framebuffer is RGB
   3684       false,   // Framebuffer has depth
   3685       false,   // Framebuffer has stencil
   3686       0x1110,  // color bits
   3687       false,   // depth mask
   3688       false,   // depth enabled
   3689       0,       // front stencil mask
   3690       0,       // back stencil mask
   3691       false,   // stencil enabled
   3692       false,   // cull_face_enabled
   3693       false,   // scissor_test_enabled
   3694       false);  // blend_enabled
   3695 
   3696   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   3697       .Times(1)
   3698       .RetiresOnSaturation();
   3699   EXPECT_EQ(error::kNoError, ExecuteCmd(draw_cmd));
   3700   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   3701 
   3702   // Check that no extra calls are made on the next draw.
   3703   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   3704       .Times(1)
   3705       .RetiresOnSaturation();
   3706   EXPECT_EQ(error::kNoError, ExecuteCmd(draw_cmd));
   3707   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   3708 
   3709   // Unbind
   3710   DoBindFramebuffer(GL_FRAMEBUFFER, 0, 0);
   3711 
   3712   SetupExpectationsForApplyingDirtyState(
   3713       true,    // Framebuffer is RGB
   3714       false,   // Framebuffer has depth
   3715       false,   // Framebuffer has stencil
   3716       0x1110,  // color bits
   3717       false,   // depth mask
   3718       false,   // depth enabled
   3719       0,       // front stencil mask
   3720       0,       // back stencil mask
   3721       false,   // stencil enabled
   3722       false,   // cull_face_enabled
   3723       false,   // scissor_test_enabled
   3724       false);  // blend_enabled
   3725 
   3726   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   3727       .Times(1)
   3728       .RetiresOnSaturation();
   3729   EXPECT_EQ(error::kNoError, ExecuteCmd(draw_cmd));
   3730   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   3731 }
   3732 
   3733 TEST_F(GLES2DecoderManualInitTest, ActualAlphaMatchesRequestedAlpha) {
   3734   InitDecoder(
   3735       "",      // extensions
   3736       true,    // has alpha
   3737       false,   // has depth
   3738       false,   // has stencil
   3739       true,    // request alpha
   3740       false,   // request depth
   3741       false,   // request stencil
   3742       true);   // bind generates resource
   3743 
   3744   EXPECT_CALL(*gl_, GetError())
   3745       .WillOnce(Return(GL_NO_ERROR))
   3746       .WillOnce(Return(GL_NO_ERROR))
   3747       .RetiresOnSaturation();
   3748   typedef GetIntegerv::Result Result;
   3749   Result* result = static_cast<Result*>(shared_memory_address_);
   3750   EXPECT_CALL(*gl_, GetIntegerv(GL_ALPHA_BITS, _))
   3751       .WillOnce(SetArgumentPointee<1>(8))
   3752       .RetiresOnSaturation();
   3753   result->size = 0;
   3754   GetIntegerv cmd2;
   3755   cmd2.Init(GL_ALPHA_BITS, shared_memory_id_, shared_memory_offset_);
   3756   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd2));
   3757   EXPECT_EQ(
   3758       decoder_->GetGLES2Util()->GLGetNumValuesReturned(GL_ALPHA_BITS),
   3759       result->GetNumResults());
   3760   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   3761   EXPECT_EQ(8, result->GetData()[0]);
   3762 }
   3763 
   3764 TEST_F(GLES2DecoderManualInitTest, ActualAlphaDoesNotMatchRequestedAlpha) {
   3765   InitDecoder(
   3766       "",      // extensions
   3767       true,    // has alpha
   3768       false,   // has depth
   3769       false,   // has stencil
   3770       false,   // request alpha
   3771       false,   // request depth
   3772       false,   // request stencil
   3773       true);   // bind generates resource
   3774 
   3775   EXPECT_CALL(*gl_, GetError())
   3776       .WillOnce(Return(GL_NO_ERROR))
   3777       .WillOnce(Return(GL_NO_ERROR))
   3778       .RetiresOnSaturation();
   3779   typedef GetIntegerv::Result Result;
   3780   Result* result = static_cast<Result*>(shared_memory_address_);
   3781   EXPECT_CALL(*gl_, GetIntegerv(GL_ALPHA_BITS, _))
   3782       .WillOnce(SetArgumentPointee<1>(8))
   3783       .RetiresOnSaturation();
   3784   result->size = 0;
   3785   GetIntegerv cmd2;
   3786   cmd2.Init(GL_ALPHA_BITS, shared_memory_id_, shared_memory_offset_);
   3787   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd2));
   3788   EXPECT_EQ(
   3789       decoder_->GetGLES2Util()->GLGetNumValuesReturned(GL_ALPHA_BITS),
   3790       result->GetNumResults());
   3791   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   3792   EXPECT_EQ(0, result->GetData()[0]);
   3793 }
   3794 
   3795 TEST_F(GLES2DecoderManualInitTest, ActualDepthMatchesRequestedDepth) {
   3796   InitDecoder(
   3797       "",      // extensions
   3798       false,   // has alpha
   3799       true,    // has depth
   3800       false,   // has stencil
   3801       false,   // request alpha
   3802       true,    // request depth
   3803       false,   // request stencil
   3804       true);   // bind generates resource
   3805 
   3806   EXPECT_CALL(*gl_, GetError())
   3807       .WillOnce(Return(GL_NO_ERROR))
   3808       .WillOnce(Return(GL_NO_ERROR))
   3809       .RetiresOnSaturation();
   3810   typedef GetIntegerv::Result Result;
   3811   Result* result = static_cast<Result*>(shared_memory_address_);
   3812   EXPECT_CALL(*gl_, GetIntegerv(GL_DEPTH_BITS, _))
   3813       .WillOnce(SetArgumentPointee<1>(24))
   3814       .RetiresOnSaturation();
   3815   result->size = 0;
   3816   GetIntegerv cmd2;
   3817   cmd2.Init(GL_DEPTH_BITS, shared_memory_id_, shared_memory_offset_);
   3818   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd2));
   3819   EXPECT_EQ(
   3820       decoder_->GetGLES2Util()->GLGetNumValuesReturned(GL_DEPTH_BITS),
   3821       result->GetNumResults());
   3822   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   3823   EXPECT_EQ(24, result->GetData()[0]);
   3824 }
   3825 
   3826 TEST_F(GLES2DecoderManualInitTest, ActualDepthDoesNotMatchRequestedDepth) {
   3827   InitDecoder(
   3828       "",      // extensions
   3829       false,   // has alpha
   3830       true,    // has depth
   3831       false,   // has stencil
   3832       false,   // request alpha
   3833       false,   // request depth
   3834       false,   // request stencil
   3835       true);   // bind generates resource
   3836 
   3837   EXPECT_CALL(*gl_, GetError())
   3838       .WillOnce(Return(GL_NO_ERROR))
   3839       .WillOnce(Return(GL_NO_ERROR))
   3840       .RetiresOnSaturation();
   3841   typedef GetIntegerv::Result Result;
   3842   Result* result = static_cast<Result*>(shared_memory_address_);
   3843   EXPECT_CALL(*gl_, GetIntegerv(GL_DEPTH_BITS, _))
   3844       .WillOnce(SetArgumentPointee<1>(24))
   3845       .RetiresOnSaturation();
   3846   result->size = 0;
   3847   GetIntegerv cmd2;
   3848   cmd2.Init(GL_DEPTH_BITS, shared_memory_id_, shared_memory_offset_);
   3849   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd2));
   3850   EXPECT_EQ(
   3851       decoder_->GetGLES2Util()->GLGetNumValuesReturned(GL_DEPTH_BITS),
   3852       result->GetNumResults());
   3853   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   3854   EXPECT_EQ(0, result->GetData()[0]);
   3855 }
   3856 
   3857 TEST_F(GLES2DecoderManualInitTest, ActualStencilMatchesRequestedStencil) {
   3858   InitDecoder(
   3859       "",      // extensions
   3860       false,   // has alpha
   3861       false,   // has depth
   3862       true,    // has stencil
   3863       false,   // request alpha
   3864       false,   // request depth
   3865       true,    // request stencil
   3866       true);   // bind generates resource
   3867 
   3868   EXPECT_CALL(*gl_, GetError())
   3869       .WillOnce(Return(GL_NO_ERROR))
   3870       .WillOnce(Return(GL_NO_ERROR))
   3871       .RetiresOnSaturation();
   3872   typedef GetIntegerv::Result Result;
   3873   Result* result = static_cast<Result*>(shared_memory_address_);
   3874   EXPECT_CALL(*gl_, GetIntegerv(GL_STENCIL_BITS, _))
   3875       .WillOnce(SetArgumentPointee<1>(8))
   3876       .RetiresOnSaturation();
   3877   result->size = 0;
   3878   GetIntegerv cmd2;
   3879   cmd2.Init(GL_STENCIL_BITS, shared_memory_id_, shared_memory_offset_);
   3880   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd2));
   3881   EXPECT_EQ(
   3882       decoder_->GetGLES2Util()->GLGetNumValuesReturned(GL_STENCIL_BITS),
   3883       result->GetNumResults());
   3884   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   3885   EXPECT_EQ(8, result->GetData()[0]);
   3886 }
   3887 
   3888 TEST_F(GLES2DecoderManualInitTest, ActualStencilDoesNotMatchRequestedStencil) {
   3889   InitDecoder(
   3890       "",      // extensions
   3891       false,   // has alpha
   3892       false,   // has depth
   3893       true,    // has stencil
   3894       false,   // request alpha
   3895       false,   // request depth
   3896       false,   // request stencil
   3897       true);   // bind generates resource
   3898 
   3899   EXPECT_CALL(*gl_, GetError())
   3900       .WillOnce(Return(GL_NO_ERROR))
   3901       .WillOnce(Return(GL_NO_ERROR))
   3902       .RetiresOnSaturation();
   3903   typedef GetIntegerv::Result Result;
   3904   Result* result = static_cast<Result*>(shared_memory_address_);
   3905   EXPECT_CALL(*gl_, GetIntegerv(GL_STENCIL_BITS, _))
   3906       .WillOnce(SetArgumentPointee<1>(8))
   3907       .RetiresOnSaturation();
   3908   result->size = 0;
   3909   GetIntegerv cmd2;
   3910   cmd2.Init(GL_STENCIL_BITS, shared_memory_id_, shared_memory_offset_);
   3911   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd2));
   3912   EXPECT_EQ(
   3913       decoder_->GetGLES2Util()->GLGetNumValuesReturned(GL_STENCIL_BITS),
   3914       result->GetNumResults());
   3915   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   3916   EXPECT_EQ(0, result->GetData()[0]);
   3917 }
   3918 
   3919 TEST_F(GLES2DecoderManualInitTest, DepthEnableWithDepth) {
   3920   InitDecoder(
   3921       "",      // extensions
   3922       false,   // has alpha
   3923       true,    // has depth
   3924       false,   // has stencil
   3925       false,   // request alpha
   3926       true,    // request depth
   3927       false,   // request stencil
   3928       true);   // bind generates resource
   3929 
   3930   Enable cmd;
   3931   cmd.Init(GL_DEPTH_TEST);
   3932   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   3933   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   3934 
   3935   SetupDefaultProgram();
   3936   SetupTexture();
   3937   AddExpectationsForSimulatedAttrib0(kNumVertices, 0);
   3938   SetupExpectationsForApplyingDirtyState(
   3939       true,    // Framebuffer is RGB
   3940       true,    // Framebuffer has depth
   3941       false,   // Framebuffer has stencil
   3942       0x1110,  // color bits
   3943       true,    // depth mask
   3944       true,    // depth enabled
   3945       0,       // front stencil mask
   3946       0,       // back stencil mask
   3947       false,   // stencil enabled
   3948       false,   // cull_face_enabled
   3949       false,   // scissor_test_enabled
   3950       false);  // blend_enabled
   3951 
   3952 
   3953   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   3954       .Times(1)
   3955       .RetiresOnSaturation();
   3956   DrawArrays draw_cmd;
   3957   draw_cmd.Init(GL_TRIANGLES, 0, kNumVertices);
   3958   EXPECT_EQ(error::kNoError, ExecuteCmd(draw_cmd));
   3959   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   3960 
   3961   EXPECT_CALL(*gl_, GetError())
   3962       .WillOnce(Return(GL_NO_ERROR))
   3963       .WillOnce(Return(GL_NO_ERROR))
   3964       .RetiresOnSaturation();
   3965   typedef GetIntegerv::Result Result;
   3966   Result* result = static_cast<Result*>(shared_memory_address_);
   3967   EXPECT_CALL(*gl_, GetIntegerv(GL_DEPTH_TEST, _))
   3968       .Times(0)
   3969       .RetiresOnSaturation();
   3970   result->size = 0;
   3971   GetIntegerv cmd2;
   3972   cmd2.Init(GL_DEPTH_TEST, shared_memory_id_, shared_memory_offset_);
   3973   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd2));
   3974   EXPECT_EQ(
   3975       decoder_->GetGLES2Util()->GLGetNumValuesReturned(GL_DEPTH_TEST),
   3976       result->GetNumResults());
   3977   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   3978   EXPECT_EQ(1, result->GetData()[0]);
   3979 }
   3980 
   3981 TEST_F(GLES2DecoderManualInitTest, DepthEnableWithoutRequestedDepth) {
   3982   InitDecoder(
   3983       "",      // extensions
   3984       false,   // has alpha
   3985       true,    // has depth
   3986       false,   // has stencil
   3987       false,   // request alpha
   3988       false,   // request depth
   3989       false,   // request stencil
   3990       true);   // bind generates resource
   3991 
   3992   Enable cmd;
   3993   cmd.Init(GL_DEPTH_TEST);
   3994   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   3995   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   3996 
   3997   SetupDefaultProgram();
   3998   SetupTexture();
   3999   AddExpectationsForSimulatedAttrib0(kNumVertices, 0);
   4000   SetupExpectationsForApplyingDirtyState(
   4001       true,    // Framebuffer is RGB
   4002       false,   // Framebuffer has depth
   4003       false,   // Framebuffer has stencil
   4004       0x1110,  // color bits
   4005       false,   // depth mask
   4006       false,   // depth enabled
   4007       0,       // front stencil mask
   4008       0,       // back stencil mask
   4009       false,   // stencil enabled
   4010       false,   // cull_face_enabled
   4011       false,   // scissor_test_enabled
   4012       false);  // blend_enabled
   4013 
   4014   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   4015       .Times(1)
   4016       .RetiresOnSaturation();
   4017   DrawArrays draw_cmd;
   4018   draw_cmd.Init(GL_TRIANGLES, 0, kNumVertices);
   4019   EXPECT_EQ(error::kNoError, ExecuteCmd(draw_cmd));
   4020   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   4021 
   4022   EXPECT_CALL(*gl_, GetError())
   4023       .WillOnce(Return(GL_NO_ERROR))
   4024       .WillOnce(Return(GL_NO_ERROR))
   4025       .RetiresOnSaturation();
   4026   typedef GetIntegerv::Result Result;
   4027   Result* result = static_cast<Result*>(shared_memory_address_);
   4028   EXPECT_CALL(*gl_, GetIntegerv(GL_DEPTH_TEST, _))
   4029       .Times(0)
   4030       .RetiresOnSaturation();
   4031   result->size = 0;
   4032   GetIntegerv cmd2;
   4033   cmd2.Init(GL_DEPTH_TEST, shared_memory_id_, shared_memory_offset_);
   4034   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd2));
   4035   EXPECT_EQ(
   4036       decoder_->GetGLES2Util()->GLGetNumValuesReturned(GL_DEPTH_TEST),
   4037       result->GetNumResults());
   4038   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   4039   EXPECT_EQ(1, result->GetData()[0]);
   4040 }
   4041 
   4042 TEST_F(GLES2DecoderManualInitTest, StencilEnableWithStencil) {
   4043   InitDecoder(
   4044       "",      // extensions
   4045       false,   // has alpha
   4046       false,   // has depth
   4047       true,    // has stencil
   4048       false,   // request alpha
   4049       false,   // request depth
   4050       true,    // request stencil
   4051       true);   // bind generates resource
   4052 
   4053   Enable cmd;
   4054   cmd.Init(GL_STENCIL_TEST);
   4055   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   4056   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   4057 
   4058   SetupDefaultProgram();
   4059   SetupTexture();
   4060   AddExpectationsForSimulatedAttrib0(kNumVertices, 0);
   4061   SetupExpectationsForApplyingDirtyState(
   4062       true,    // Framebuffer is RGB
   4063       false,   // Framebuffer has depth
   4064       true,    // Framebuffer has stencil
   4065       0x1110,  // color bits
   4066       false,   // depth mask
   4067       false,   // depth enabled
   4068       -1,      // front stencil mask
   4069       -1,      // back stencil mask
   4070       true,    // stencil enabled
   4071       false,   // cull_face_enabled
   4072       false,   // scissor_test_enabled
   4073       false);  // blend_enabled
   4074 
   4075   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   4076       .Times(1)
   4077       .RetiresOnSaturation();
   4078   DrawArrays draw_cmd;
   4079   draw_cmd.Init(GL_TRIANGLES, 0, kNumVertices);
   4080   EXPECT_EQ(error::kNoError, ExecuteCmd(draw_cmd));
   4081   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   4082 
   4083   EXPECT_CALL(*gl_, GetError())
   4084       .WillOnce(Return(GL_NO_ERROR))
   4085       .WillOnce(Return(GL_NO_ERROR))
   4086       .RetiresOnSaturation();
   4087   typedef GetIntegerv::Result Result;
   4088   Result* result = static_cast<Result*>(shared_memory_address_);
   4089   EXPECT_CALL(*gl_, GetIntegerv(GL_STENCIL_TEST, _))
   4090       .Times(0)
   4091       .RetiresOnSaturation();
   4092   result->size = 0;
   4093   GetIntegerv cmd2;
   4094   cmd2.Init(GL_STENCIL_TEST, shared_memory_id_, shared_memory_offset_);
   4095   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd2));
   4096   EXPECT_EQ(
   4097       decoder_->GetGLES2Util()->GLGetNumValuesReturned(GL_STENCIL_TEST),
   4098       result->GetNumResults());
   4099   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   4100   EXPECT_EQ(1, result->GetData()[0]);
   4101 }
   4102 
   4103 TEST_F(GLES2DecoderManualInitTest, StencilEnableWithoutRequestedStencil) {
   4104   InitDecoder(
   4105       "",      // extensions
   4106       false,   // has alpha
   4107       false,   // has depth
   4108       true,    // has stencil
   4109       false,   // request alpha
   4110       false,   // request depth
   4111       false,   // request stencil
   4112       true);   // bind generates resource
   4113 
   4114   Enable cmd;
   4115   cmd.Init(GL_STENCIL_TEST);
   4116   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   4117   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   4118 
   4119   SetupDefaultProgram();
   4120   SetupTexture();
   4121   AddExpectationsForSimulatedAttrib0(kNumVertices, 0);
   4122   SetupExpectationsForApplyingDirtyState(
   4123       true,    // Framebuffer is RGB
   4124       false,   // Framebuffer has depth
   4125       false,   // Framebuffer has stencil
   4126       0x1110,  // color bits
   4127       false,   // depth mask
   4128       false,   // depth enabled
   4129       0,       // front stencil mask
   4130       0,       // back stencil mask
   4131       false,   // stencil enabled
   4132       false,   // cull_face_enabled
   4133       false,   // scissor_test_enabled
   4134       false);  // blend_enabled
   4135 
   4136   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   4137       .Times(1)
   4138       .RetiresOnSaturation();
   4139   DrawArrays draw_cmd;
   4140   draw_cmd.Init(GL_TRIANGLES, 0, kNumVertices);
   4141   EXPECT_EQ(error::kNoError, ExecuteCmd(draw_cmd));
   4142   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   4143 
   4144   EXPECT_CALL(*gl_, GetError())
   4145       .WillOnce(Return(GL_NO_ERROR))
   4146       .WillOnce(Return(GL_NO_ERROR))
   4147       .RetiresOnSaturation();
   4148   typedef GetIntegerv::Result Result;
   4149   Result* result = static_cast<Result*>(shared_memory_address_);
   4150   EXPECT_CALL(*gl_, GetIntegerv(GL_STENCIL_TEST, _))
   4151       .Times(0)
   4152       .RetiresOnSaturation();
   4153   result->size = 0;
   4154   GetIntegerv cmd2;
   4155   cmd2.Init(GL_STENCIL_TEST, shared_memory_id_, shared_memory_offset_);
   4156   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd2));
   4157   EXPECT_EQ(
   4158       decoder_->GetGLES2Util()->GLGetNumValuesReturned(GL_STENCIL_TEST),
   4159       result->GetNumResults());
   4160   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   4161   EXPECT_EQ(1, result->GetData()[0]);
   4162 }
   4163 
   4164 TEST_F(GLES2DecoderManualInitTest, PackedDepthStencilReportsCorrectValues) {
   4165   InitDecoder(
   4166       "GL_OES_packed_depth_stencil",      // extensions
   4167       false,   // has alpha
   4168       true,    // has depth
   4169       true,    // has stencil
   4170       false,   // request alpha
   4171       true,    // request depth
   4172       true,    // request stencil
   4173       true);   // bind generates resource
   4174 
   4175   EXPECT_CALL(*gl_, GetError())
   4176       .WillOnce(Return(GL_NO_ERROR))
   4177       .WillOnce(Return(GL_NO_ERROR))
   4178       .WillOnce(Return(GL_NO_ERROR))
   4179       .WillOnce(Return(GL_NO_ERROR))
   4180       .RetiresOnSaturation();
   4181   typedef GetIntegerv::Result Result;
   4182   Result* result = static_cast<Result*>(shared_memory_address_);
   4183   result->size = 0;
   4184   GetIntegerv cmd2;
   4185   cmd2.Init(GL_STENCIL_BITS, shared_memory_id_, shared_memory_offset_);
   4186   EXPECT_CALL(*gl_, GetIntegerv(GL_STENCIL_BITS, _))
   4187       .WillOnce(SetArgumentPointee<1>(8))
   4188       .RetiresOnSaturation();
   4189   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd2));
   4190   EXPECT_EQ(
   4191       decoder_->GetGLES2Util()->GLGetNumValuesReturned(GL_STENCIL_BITS),
   4192       result->GetNumResults());
   4193   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   4194   EXPECT_EQ(8, result->GetData()[0]);
   4195   result->size = 0;
   4196   cmd2.Init(GL_DEPTH_BITS, shared_memory_id_, shared_memory_offset_);
   4197   EXPECT_CALL(*gl_, GetIntegerv(GL_DEPTH_BITS, _))
   4198       .WillOnce(SetArgumentPointee<1>(24))
   4199       .RetiresOnSaturation();
   4200   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd2));
   4201   EXPECT_EQ(
   4202       decoder_->GetGLES2Util()->GLGetNumValuesReturned(GL_DEPTH_BITS),
   4203       result->GetNumResults());
   4204   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   4205   EXPECT_EQ(24, result->GetData()[0]);
   4206 }
   4207 
   4208 TEST_F(GLES2DecoderManualInitTest, PackedDepthStencilNoRequestedStencil) {
   4209   InitDecoder(
   4210       "GL_OES_packed_depth_stencil",      // extensions
   4211       false,   // has alpha
   4212       true,    // has depth
   4213       true,    // has stencil
   4214       false,   // request alpha
   4215       true,    // request depth
   4216       false,   // request stencil
   4217       true);   // bind generates resource
   4218 
   4219   EXPECT_CALL(*gl_, GetError())
   4220       .WillOnce(Return(GL_NO_ERROR))
   4221       .WillOnce(Return(GL_NO_ERROR))
   4222       .WillOnce(Return(GL_NO_ERROR))
   4223       .WillOnce(Return(GL_NO_ERROR))
   4224       .RetiresOnSaturation();
   4225   typedef GetIntegerv::Result Result;
   4226   Result* result = static_cast<Result*>(shared_memory_address_);
   4227   result->size = 0;
   4228   GetIntegerv cmd2;
   4229   cmd2.Init(GL_STENCIL_BITS, shared_memory_id_, shared_memory_offset_);
   4230   EXPECT_CALL(*gl_, GetIntegerv(GL_STENCIL_BITS, _))
   4231       .WillOnce(SetArgumentPointee<1>(8))
   4232       .RetiresOnSaturation();
   4233   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd2));
   4234   EXPECT_EQ(
   4235       decoder_->GetGLES2Util()->GLGetNumValuesReturned(GL_STENCIL_BITS),
   4236       result->GetNumResults());
   4237   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   4238   EXPECT_EQ(0, result->GetData()[0]);
   4239   result->size = 0;
   4240   cmd2.Init(GL_DEPTH_BITS, shared_memory_id_, shared_memory_offset_);
   4241   EXPECT_CALL(*gl_, GetIntegerv(GL_DEPTH_BITS, _))
   4242       .WillOnce(SetArgumentPointee<1>(24))
   4243       .RetiresOnSaturation();
   4244   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd2));
   4245   EXPECT_EQ(
   4246       decoder_->GetGLES2Util()->GLGetNumValuesReturned(GL_DEPTH_BITS),
   4247       result->GetNumResults());
   4248   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   4249   EXPECT_EQ(24, result->GetData()[0]);
   4250 }
   4251 
   4252 TEST_F(GLES2DecoderManualInitTest, PackedDepthStencilRenderbufferDepth) {
   4253   InitDecoder(
   4254       "GL_OES_packed_depth_stencil",      // extensions
   4255       false,   // has alpha
   4256       false,   // has depth
   4257       false,   // has stencil
   4258       false,   // request alpha
   4259       false,   // request depth
   4260       false,   // request stencil
   4261       true);   // bind generates resource
   4262   DoBindRenderbuffer(GL_RENDERBUFFER, client_renderbuffer_id_,
   4263                     kServiceRenderbufferId);
   4264   DoBindFramebuffer(GL_FRAMEBUFFER, client_framebuffer_id_,
   4265                     kServiceFramebufferId);
   4266 
   4267   EXPECT_CALL(*gl_, GetError())
   4268       .WillOnce(Return(GL_NO_ERROR))  // for RenderbufferStoage
   4269       .WillOnce(Return(GL_NO_ERROR))
   4270       .WillOnce(Return(GL_NO_ERROR))  // for FramebufferRenderbuffer
   4271       .WillOnce(Return(GL_NO_ERROR))
   4272       .WillOnce(Return(GL_NO_ERROR))  // for GetIntegerv
   4273       .WillOnce(Return(GL_NO_ERROR))
   4274       .WillOnce(Return(GL_NO_ERROR))  // for GetIntegerv
   4275       .WillOnce(Return(GL_NO_ERROR))
   4276       .RetiresOnSaturation();
   4277 
   4278   EXPECT_CALL(*gl_, RenderbufferStorageEXT(
   4279       GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 100, 50))
   4280       .Times(1)
   4281       .RetiresOnSaturation();
   4282   RenderbufferStorage cmd;
   4283   cmd.Init(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 100, 50);
   4284   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   4285   EXPECT_CALL(*gl_, FramebufferRenderbufferEXT(
   4286       GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER,
   4287       kServiceRenderbufferId))
   4288       .Times(1)
   4289       .RetiresOnSaturation();
   4290   FramebufferRenderbuffer fbrb_cmd;
   4291   fbrb_cmd.Init(
   4292       GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER,
   4293       client_renderbuffer_id_);
   4294   EXPECT_EQ(error::kNoError, ExecuteCmd(fbrb_cmd));
   4295 
   4296   typedef GetIntegerv::Result Result;
   4297   Result* result = static_cast<Result*>(shared_memory_address_);
   4298   result->size = 0;
   4299   GetIntegerv cmd2;
   4300   cmd2.Init(GL_STENCIL_BITS, shared_memory_id_, shared_memory_offset_);
   4301   EXPECT_CALL(*gl_, GetIntegerv(GL_STENCIL_BITS, _))
   4302       .WillOnce(SetArgumentPointee<1>(8))
   4303       .RetiresOnSaturation();
   4304   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd2));
   4305   EXPECT_EQ(
   4306       decoder_->GetGLES2Util()->GLGetNumValuesReturned(GL_STENCIL_BITS),
   4307       result->GetNumResults());
   4308   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   4309   EXPECT_EQ(0, result->GetData()[0]);
   4310   result->size = 0;
   4311   cmd2.Init(GL_DEPTH_BITS, shared_memory_id_, shared_memory_offset_);
   4312   EXPECT_CALL(*gl_, GetIntegerv(GL_DEPTH_BITS, _))
   4313       .WillOnce(SetArgumentPointee<1>(24))
   4314       .RetiresOnSaturation();
   4315   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd2));
   4316   EXPECT_EQ(
   4317       decoder_->GetGLES2Util()->GLGetNumValuesReturned(GL_DEPTH_BITS),
   4318       result->GetNumResults());
   4319   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   4320   EXPECT_EQ(24, result->GetData()[0]);
   4321 }
   4322 
   4323 TEST_F(GLES2DecoderManualInitTest, PackedDepthStencilRenderbufferStencil) {
   4324   InitDecoder(
   4325       "GL_OES_packed_depth_stencil",      // extensions
   4326       false,   // has alpha
   4327       false,   // has depth
   4328       false,   // has stencil
   4329       false,   // request alpha
   4330       false,   // request depth
   4331       false,   // request stencil
   4332       true);   // bind generates resource
   4333   DoBindRenderbuffer(GL_RENDERBUFFER, client_renderbuffer_id_,
   4334                     kServiceRenderbufferId);
   4335   DoBindFramebuffer(GL_FRAMEBUFFER, client_framebuffer_id_,
   4336                     kServiceFramebufferId);
   4337 
   4338   EXPECT_CALL(*gl_, GetError())
   4339       .WillOnce(Return(GL_NO_ERROR))  // for RenderbufferStoage
   4340       .WillOnce(Return(GL_NO_ERROR))
   4341       .WillOnce(Return(GL_NO_ERROR))  // for FramebufferRenderbuffer
   4342       .WillOnce(Return(GL_NO_ERROR))
   4343       .WillOnce(Return(GL_NO_ERROR))  // for GetIntegerv
   4344       .WillOnce(Return(GL_NO_ERROR))
   4345       .WillOnce(Return(GL_NO_ERROR))  // for GetIntegerv
   4346       .WillOnce(Return(GL_NO_ERROR))
   4347       .RetiresOnSaturation();
   4348 
   4349   EXPECT_CALL(*gl_, RenderbufferStorageEXT(
   4350       GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 100, 50))
   4351       .Times(1)
   4352       .RetiresOnSaturation();
   4353   RenderbufferStorage cmd;
   4354   cmd.Init(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 100, 50);
   4355   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   4356   EXPECT_CALL(*gl_, FramebufferRenderbufferEXT(
   4357       GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER,
   4358       kServiceRenderbufferId))
   4359       .Times(1)
   4360       .RetiresOnSaturation();
   4361   FramebufferRenderbuffer fbrb_cmd;
   4362   fbrb_cmd.Init(
   4363       GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER,
   4364       client_renderbuffer_id_);
   4365   EXPECT_EQ(error::kNoError, ExecuteCmd(fbrb_cmd));
   4366 
   4367   typedef GetIntegerv::Result Result;
   4368   Result* result = static_cast<Result*>(shared_memory_address_);
   4369   result->size = 0;
   4370   GetIntegerv cmd2;
   4371   cmd2.Init(GL_STENCIL_BITS, shared_memory_id_, shared_memory_offset_);
   4372   EXPECT_CALL(*gl_, GetIntegerv(GL_STENCIL_BITS, _))
   4373       .WillOnce(SetArgumentPointee<1>(8))
   4374       .RetiresOnSaturation();
   4375   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd2));
   4376   EXPECT_EQ(
   4377       decoder_->GetGLES2Util()->GLGetNumValuesReturned(GL_STENCIL_BITS),
   4378       result->GetNumResults());
   4379   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   4380   EXPECT_EQ(8, result->GetData()[0]);
   4381   result->size = 0;
   4382   cmd2.Init(GL_DEPTH_BITS, shared_memory_id_, shared_memory_offset_);
   4383   EXPECT_CALL(*gl_, GetIntegerv(GL_DEPTH_BITS, _))
   4384       .WillOnce(SetArgumentPointee<1>(24))
   4385       .RetiresOnSaturation();
   4386   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd2));
   4387   EXPECT_EQ(
   4388       decoder_->GetGLES2Util()->GLGetNumValuesReturned(GL_DEPTH_BITS),
   4389       result->GetNumResults());
   4390   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   4391   EXPECT_EQ(0, result->GetData()[0]);
   4392 }
   4393 
   4394 TEST_F(GLES2DecoderTest, GetMultipleIntegervCHROMIUMValidArgs) {
   4395   const GLsizei kCount = 3;
   4396   GLenum* pnames = GetSharedMemoryAs<GLenum*>();
   4397   pnames[0] = GL_DEPTH_WRITEMASK;
   4398   pnames[1] = GL_COLOR_WRITEMASK;
   4399   pnames[2] = GL_STENCIL_WRITEMASK;
   4400   GLint* results =
   4401       GetSharedMemoryAsWithOffset<GLint*>(sizeof(*pnames) * kCount);
   4402 
   4403   GLsizei num_results = 0;
   4404   for (GLsizei ii = 0; ii < kCount; ++ii) {
   4405     num_results += decoder_->GetGLES2Util()->GLGetNumValuesReturned(pnames[ii]);
   4406   }
   4407   const GLsizei result_size = num_results * sizeof(*results);
   4408   memset(results,  0, result_size);
   4409 
   4410   const GLint kSentinel = 0x12345678;
   4411   results[num_results] = kSentinel;
   4412 
   4413   GetMultipleIntegervCHROMIUM cmd;
   4414   cmd.Init(
   4415       kSharedMemoryId, kSharedMemoryOffset, kCount,
   4416       kSharedMemoryId, kSharedMemoryOffset + sizeof(*pnames) * kCount,
   4417       result_size);
   4418 
   4419   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   4420   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   4421   EXPECT_EQ(1, results[0]);  // Depth writemask
   4422   EXPECT_EQ(1, results[1]);  // color writemask red
   4423   EXPECT_EQ(1, results[2]);  // color writemask green
   4424   EXPECT_EQ(1, results[3]);  // color writemask blue
   4425   EXPECT_EQ(1, results[4]);  // color writemask alpha
   4426   EXPECT_EQ(-1, results[5]);  // stencil writemask alpha
   4427   EXPECT_EQ(kSentinel, results[num_results]);  // End of results
   4428 }
   4429 
   4430 TEST_F(GLES2DecoderTest, GetMultipleIntegervCHROMIUMInvalidArgs) {
   4431   const GLsizei kCount = 3;
   4432   // Offset the pnames because GLGetError will use the first uint32.
   4433   const uint32 kPnameOffset = sizeof(uint32);
   4434   const uint32 kResultsOffset = kPnameOffset + sizeof(GLint) * kCount;
   4435   GLenum* pnames = GetSharedMemoryAsWithOffset<GLenum*>(kPnameOffset);
   4436   pnames[0] = GL_DEPTH_WRITEMASK;
   4437   pnames[1] = GL_COLOR_WRITEMASK;
   4438   pnames[2] = GL_STENCIL_WRITEMASK;
   4439   GLint* results = GetSharedMemoryAsWithOffset<GLint*>(kResultsOffset);
   4440 
   4441   GLsizei num_results = 0;
   4442   for (GLsizei ii = 0; ii < kCount; ++ii) {
   4443     num_results += decoder_->GetGLES2Util()->GLGetNumValuesReturned(pnames[ii]);
   4444   }
   4445   const GLsizei result_size = num_results * sizeof(*results);
   4446   memset(results,  0, result_size);
   4447 
   4448   const GLint kSentinel = 0x12345678;
   4449   results[num_results] = kSentinel;
   4450 
   4451   GetMultipleIntegervCHROMIUM cmd;
   4452   // Check bad pnames pointer.
   4453   cmd.Init(
   4454       kInvalidSharedMemoryId, kSharedMemoryOffset + kPnameOffset, kCount,
   4455       kSharedMemoryId, kSharedMemoryOffset + kResultsOffset,
   4456       result_size);
   4457   EXPECT_EQ(error::kOutOfBounds, ExecuteCmd(cmd));
   4458   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   4459   // Check bad pnames pointer.
   4460   cmd.Init(
   4461       kSharedMemoryId, kInvalidSharedMemoryOffset, kCount,
   4462       kSharedMemoryId, kSharedMemoryOffset + kResultsOffset,
   4463       result_size);
   4464   EXPECT_EQ(error::kOutOfBounds, ExecuteCmd(cmd));
   4465   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   4466   // Check bad count.
   4467   cmd.Init(
   4468       kSharedMemoryId, kSharedMemoryOffset + kPnameOffset, -1,
   4469       kSharedMemoryId, kSharedMemoryOffset + kResultsOffset,
   4470       result_size);
   4471   EXPECT_EQ(error::kOutOfBounds, ExecuteCmd(cmd));
   4472   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   4473   // Check bad results pointer.
   4474   cmd.Init(
   4475       kSharedMemoryId, kSharedMemoryOffset + kPnameOffset, kCount,
   4476       kInvalidSharedMemoryId, kSharedMemoryOffset + kResultsOffset,
   4477       result_size);
   4478   EXPECT_EQ(error::kOutOfBounds, ExecuteCmd(cmd));
   4479   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   4480   // Check bad results pointer.
   4481   cmd.Init(
   4482       kSharedMemoryId, kSharedMemoryOffset + kPnameOffset, kCount,
   4483       kSharedMemoryId, kInvalidSharedMemoryOffset,
   4484       result_size);
   4485   EXPECT_EQ(error::kOutOfBounds, ExecuteCmd(cmd));
   4486   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   4487   // Check bad size.
   4488   cmd.Init(
   4489       kSharedMemoryId, kSharedMemoryOffset + kPnameOffset, kCount,
   4490       kSharedMemoryId, kSharedMemoryOffset + kResultsOffset,
   4491       result_size + 1);
   4492   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   4493   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   4494   // Check bad size.
   4495   cmd.Init(
   4496       kSharedMemoryId, kSharedMemoryOffset + kPnameOffset, kCount,
   4497       kSharedMemoryId, kSharedMemoryOffset + kResultsOffset,
   4498       result_size - 1);
   4499   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   4500   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   4501   // Check bad enum.
   4502   cmd.Init(
   4503       kSharedMemoryId, kSharedMemoryOffset + kPnameOffset, kCount,
   4504       kSharedMemoryId, kSharedMemoryOffset + kResultsOffset,
   4505       result_size);
   4506   GLenum temp = pnames[2];
   4507   pnames[2] = GL_TRUE;
   4508   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   4509   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
   4510   pnames[2] = temp;
   4511   // Check results area has not been cleared by client.
   4512   results[1] = 1;
   4513   EXPECT_EQ(error::kInvalidArguments, ExecuteCmd(cmd));
   4514   // Check buffer is what we expect
   4515   EXPECT_EQ(0, results[0]);
   4516   EXPECT_EQ(1, results[1]);
   4517   EXPECT_EQ(0, results[2]);
   4518   EXPECT_EQ(0, results[3]);
   4519   EXPECT_EQ(0, results[4]);
   4520   EXPECT_EQ(0, results[5]);
   4521   EXPECT_EQ(kSentinel, results[num_results]);  // End of results
   4522 }
   4523 
   4524 TEST_F(GLES2DecoderTest, TexImage2DRedefinitionSucceeds) {
   4525   const int kWidth = 16;
   4526   const int kHeight = 8;
   4527   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   4528   EXPECT_CALL(*gl_, GetError())
   4529       .WillRepeatedly(Return(GL_NO_ERROR));
   4530   for (int ii = 0; ii < 2; ++ii) {
   4531     TexImage2D cmd;
   4532     if (ii == 0) {
   4533       EXPECT_CALL(*gl_, TexImage2D(
   4534           GL_TEXTURE_2D, 0, GL_RGBA, kWidth, kHeight, 0, GL_RGBA,
   4535           GL_UNSIGNED_BYTE, _))
   4536           .Times(1)
   4537           .RetiresOnSaturation();
   4538       cmd.Init(
   4539           GL_TEXTURE_2D, 0, GL_RGBA, kWidth, kHeight, 0, GL_RGBA,
   4540           GL_UNSIGNED_BYTE, kSharedMemoryId, kSharedMemoryOffset);
   4541     } else {
   4542       SetupClearTextureExpections(
   4543           kServiceTextureId, kServiceTextureId, GL_TEXTURE_2D, GL_TEXTURE_2D,
   4544           0, GL_RGBA, GL_UNSIGNED_BYTE, kWidth, kHeight);
   4545       cmd.Init(
   4546           GL_TEXTURE_2D, 0, GL_RGBA, kWidth, kHeight, 0, GL_RGBA,
   4547           GL_UNSIGNED_BYTE, 0, 0);
   4548     }
   4549     EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   4550     EXPECT_CALL(*gl_, TexSubImage2D(
   4551         GL_TEXTURE_2D, 0, 0, 0, kWidth, kHeight - 1, GL_RGBA, GL_UNSIGNED_BYTE,
   4552         shared_memory_address_))
   4553         .Times(1)
   4554         .RetiresOnSaturation();
   4555     // Consider this TexSubImage2D command part of the previous TexImage2D
   4556     // (last GL_TRUE argument). It will be skipped if there are bugs in the
   4557     // redefinition case.
   4558     TexSubImage2D cmd2;
   4559     cmd2.Init(
   4560         GL_TEXTURE_2D, 0, 0, 0, kWidth, kHeight - 1, GL_RGBA, GL_UNSIGNED_BYTE,
   4561         kSharedMemoryId, kSharedMemoryOffset, GL_TRUE);
   4562     EXPECT_EQ(error::kNoError, ExecuteCmd(cmd2));
   4563   }
   4564 }
   4565 
   4566 TEST_F(GLES2DecoderTest, TexImage2DGLError) {
   4567   GLenum target = GL_TEXTURE_2D;
   4568   GLint level = 0;
   4569   GLenum internal_format = GL_RGBA;
   4570   GLsizei width = 2;
   4571   GLsizei height = 4;
   4572   GLint border = 0;
   4573   GLenum format = GL_RGBA;
   4574   GLenum type = GL_UNSIGNED_BYTE;
   4575   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   4576   TextureManager* manager = group().texture_manager();
   4577   TextureRef* texture_ref = manager->GetTexture(client_texture_id_);
   4578   ASSERT_TRUE(texture_ref != NULL);
   4579   Texture* texture = texture_ref->texture();
   4580   EXPECT_FALSE(texture->GetLevelSize(GL_TEXTURE_2D, level, &width, &height));
   4581   EXPECT_CALL(*gl_, GetError())
   4582       .WillOnce(Return(GL_NO_ERROR))
   4583       .WillOnce(Return(GL_OUT_OF_MEMORY))
   4584       .RetiresOnSaturation();
   4585   EXPECT_CALL(*gl_, TexImage2D(target, level, internal_format,
   4586                                width, height, border, format, type, _))
   4587       .Times(1)
   4588       .RetiresOnSaturation();
   4589   TexImage2D cmd;
   4590   cmd.Init(target, level, internal_format, width, height, border, format,
   4591            type, kSharedMemoryId, kSharedMemoryOffset);
   4592   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   4593   EXPECT_EQ(GL_OUT_OF_MEMORY, GetGLError());
   4594   EXPECT_FALSE(texture->GetLevelSize(GL_TEXTURE_2D, level, &width, &height));
   4595 }
   4596 
   4597 TEST_F(GLES2DecoderTest, BufferDataGLError) {
   4598   GLenum target = GL_ARRAY_BUFFER;
   4599   GLsizeiptr size = 4;
   4600   DoBindBuffer(GL_ARRAY_BUFFER, client_buffer_id_, kServiceBufferId);
   4601   BufferManager* manager = group().buffer_manager();
   4602   Buffer* buffer = manager->GetBuffer(client_buffer_id_);
   4603   ASSERT_TRUE(buffer != NULL);
   4604   EXPECT_EQ(0, buffer->size());
   4605   EXPECT_CALL(*gl_, GetError())
   4606       .WillOnce(Return(GL_NO_ERROR))
   4607       .WillOnce(Return(GL_OUT_OF_MEMORY))
   4608       .RetiresOnSaturation();
   4609   EXPECT_CALL(*gl_, BufferData(target, size, _, GL_STREAM_DRAW))
   4610       .Times(1)
   4611       .RetiresOnSaturation();
   4612   BufferData cmd;
   4613   cmd.Init(target, size, 0, 0, GL_STREAM_DRAW);
   4614   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   4615   EXPECT_EQ(GL_OUT_OF_MEMORY, GetGLError());
   4616   EXPECT_EQ(0, buffer->size());
   4617 }
   4618 
   4619 TEST_F(GLES2DecoderTest, CopyTexImage2DGLError) {
   4620   GLenum target = GL_TEXTURE_2D;
   4621   GLint level = 0;
   4622   GLenum internal_format = GL_RGBA;
   4623   GLsizei width = 2;
   4624   GLsizei height = 4;
   4625   GLint border = 0;
   4626   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   4627   TextureManager* manager = group().texture_manager();
   4628   TextureRef* texture_ref = manager->GetTexture(client_texture_id_);
   4629   ASSERT_TRUE(texture_ref != NULL);
   4630   Texture* texture = texture_ref->texture();
   4631   EXPECT_FALSE(texture->GetLevelSize(GL_TEXTURE_2D, level, &width, &height));
   4632   EXPECT_CALL(*gl_, GetError())
   4633       .WillOnce(Return(GL_NO_ERROR))
   4634       .WillOnce(Return(GL_OUT_OF_MEMORY))
   4635       .RetiresOnSaturation();
   4636   EXPECT_CALL(*gl_, CopyTexImage2D(
   4637       target, level, internal_format, 0, 0, width, height, border))
   4638       .Times(1)
   4639       .RetiresOnSaturation();
   4640   CopyTexImage2D cmd;
   4641   cmd.Init(target, level, internal_format, 0, 0, width, height, border);
   4642   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   4643   EXPECT_EQ(GL_OUT_OF_MEMORY, GetGLError());
   4644   EXPECT_FALSE(texture->GetLevelSize(GL_TEXTURE_2D, level, &width, &height));
   4645 }
   4646 
   4647 TEST_F(GLES2DecoderTest, FramebufferRenderbufferGLError) {
   4648   DoBindFramebuffer(GL_FRAMEBUFFER, client_framebuffer_id_,
   4649                     kServiceFramebufferId);
   4650   EXPECT_CALL(*gl_, GetError())
   4651       .WillOnce(Return(GL_NO_ERROR))
   4652       .WillOnce(Return(GL_OUT_OF_MEMORY))
   4653       .RetiresOnSaturation();
   4654   EXPECT_CALL(*gl_, FramebufferRenderbufferEXT(
   4655       GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER,
   4656       kServiceRenderbufferId))
   4657       .Times(1)
   4658       .RetiresOnSaturation();
   4659   FramebufferRenderbuffer cmd;
   4660   cmd.Init(
   4661       GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER,
   4662       client_renderbuffer_id_);
   4663   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   4664   EXPECT_EQ(GL_OUT_OF_MEMORY, GetGLError());
   4665 }
   4666 
   4667 TEST_F(GLES2DecoderTest, FramebufferTexture2DGLError) {
   4668   const GLsizei kWidth = 5;
   4669   const GLsizei kHeight = 3;
   4670   const GLenum kFormat = GL_RGB;
   4671   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   4672   DoTexImage2D(GL_TEXTURE_2D, 0, kFormat, kWidth, kHeight, 0,
   4673                kFormat, GL_UNSIGNED_BYTE, 0, 0);
   4674   DoBindFramebuffer(GL_FRAMEBUFFER, client_framebuffer_id_,
   4675                     kServiceFramebufferId);
   4676   EXPECT_CALL(*gl_, GetError())
   4677       .WillOnce(Return(GL_NO_ERROR))
   4678       .WillOnce(Return(GL_OUT_OF_MEMORY))
   4679       .RetiresOnSaturation();
   4680   EXPECT_CALL(*gl_, FramebufferTexture2DEXT(
   4681       GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
   4682       kServiceTextureId, 0))
   4683       .Times(1)
   4684       .RetiresOnSaturation();
   4685   FramebufferTexture2D fbtex_cmd;
   4686   fbtex_cmd.Init(
   4687       GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, client_texture_id_,
   4688       0);
   4689   EXPECT_EQ(error::kNoError, ExecuteCmd(fbtex_cmd));
   4690   EXPECT_EQ(GL_OUT_OF_MEMORY, GetGLError());
   4691 }
   4692 
   4693 TEST_F(GLES2DecoderTest, RenderbufferStorageGLError) {
   4694   DoBindRenderbuffer(GL_RENDERBUFFER, client_renderbuffer_id_,
   4695                     kServiceRenderbufferId);
   4696   EXPECT_CALL(*gl_, GetError())
   4697       .WillOnce(Return(GL_NO_ERROR))
   4698       .WillOnce(Return(GL_OUT_OF_MEMORY))
   4699       .RetiresOnSaturation();
   4700   EXPECT_CALL(*gl_, RenderbufferStorageEXT(
   4701       GL_RENDERBUFFER, GL_RGBA, 100, 50))
   4702       .Times(1)
   4703       .RetiresOnSaturation();
   4704   RenderbufferStorage cmd;
   4705   cmd.Init(GL_RENDERBUFFER, GL_RGBA4, 100, 50);
   4706   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   4707   EXPECT_EQ(GL_OUT_OF_MEMORY, GetGLError());
   4708 }
   4709 
   4710 TEST_F(GLES2DecoderTest, RenderbufferStorageBadArgs) {
   4711   DoBindRenderbuffer(GL_RENDERBUFFER, client_renderbuffer_id_,
   4712                     kServiceRenderbufferId);
   4713   EXPECT_CALL(*gl_, RenderbufferStorageEXT(_, _, _, _))
   4714       .Times(0)
   4715       .RetiresOnSaturation();
   4716   RenderbufferStorage cmd;
   4717   cmd.Init(GL_RENDERBUFFER, GL_RGBA4, TestHelper::kMaxRenderbufferSize + 1, 1);
   4718   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   4719   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   4720   cmd.Init(GL_RENDERBUFFER, GL_RGBA4, 1, TestHelper::kMaxRenderbufferSize + 1);
   4721   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   4722   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   4723 }
   4724 
   4725 TEST_F(GLES2DecoderManualInitTest,
   4726        RenderbufferStorageMultisampleCHROMIUMGLError) {
   4727   InitDecoder(
   4728       "GL_EXT_framebuffer_multisample",  // extensions
   4729       false,   // has alpha
   4730       false,   // has depth
   4731       false,   // has stencil
   4732       false,   // request alpha
   4733       false,   // request depth
   4734       false,   // request stencil
   4735       true);   // bind generates resource
   4736   DoBindRenderbuffer(GL_RENDERBUFFER, client_renderbuffer_id_,
   4737                     kServiceRenderbufferId);
   4738   EXPECT_CALL(*gl_, GetError())
   4739       .WillOnce(Return(GL_NO_ERROR))
   4740       .WillOnce(Return(GL_OUT_OF_MEMORY))
   4741       .RetiresOnSaturation();
   4742   EXPECT_CALL(*gl_, RenderbufferStorageMultisampleEXT(
   4743       GL_RENDERBUFFER, 1, GL_RGBA, 100, 50))
   4744       .Times(1)
   4745       .RetiresOnSaturation();
   4746   RenderbufferStorageMultisampleCHROMIUM cmd;
   4747   cmd.Init(GL_RENDERBUFFER, 1, GL_RGBA4, 100, 50);
   4748   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   4749   EXPECT_EQ(GL_OUT_OF_MEMORY, GetGLError());
   4750 }
   4751 
   4752 TEST_F(GLES2DecoderManualInitTest,
   4753        RenderbufferStorageMultisampleCHROMIUMBadArgs) {
   4754   InitDecoder(
   4755       "GL_EXT_framebuffer_multisample",  // extensions
   4756       false,   // has alpha
   4757       false,   // has depth
   4758       false,   // has stencil
   4759       false,   // request alpha
   4760       false,   // request depth
   4761       false,   // request stencil
   4762       true);   // bind generates resource
   4763   DoBindRenderbuffer(GL_RENDERBUFFER, client_renderbuffer_id_,
   4764                     kServiceRenderbufferId);
   4765   EXPECT_CALL(*gl_, RenderbufferStorageMultisampleEXT(_, _, _, _, _))
   4766       .Times(0)
   4767       .RetiresOnSaturation();
   4768   RenderbufferStorageMultisampleCHROMIUM cmd;
   4769   cmd.Init(GL_RENDERBUFFER, TestHelper::kMaxSamples + 1,
   4770            GL_RGBA4, TestHelper::kMaxRenderbufferSize, 1);
   4771   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   4772   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   4773   cmd.Init(GL_RENDERBUFFER, TestHelper::kMaxSamples,
   4774            GL_RGBA4, TestHelper::kMaxRenderbufferSize + 1, 1);
   4775   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   4776   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   4777   cmd.Init(GL_RENDERBUFFER, TestHelper::kMaxSamples,
   4778            GL_RGBA4, 1, TestHelper::kMaxRenderbufferSize + 1);
   4779   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   4780   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   4781 }
   4782 
   4783 TEST_F(GLES2DecoderManualInitTest, RenderbufferStorageMultisampleCHROMIUM) {
   4784   InitDecoder(
   4785       "GL_EXT_framebuffer_multisample",  // extensions
   4786       false,   // has alpha
   4787       false,   // has depth
   4788       false,   // has stencil
   4789       false,   // request alpha
   4790       false,   // request depth
   4791       false,   // request stencil
   4792       false);  // bind generates resource
   4793   DoBindRenderbuffer(GL_RENDERBUFFER, client_renderbuffer_id_,
   4794                     kServiceRenderbufferId);
   4795   InSequence sequence;
   4796   EXPECT_CALL(*gl_, GetError())
   4797       .WillOnce(Return(GL_NO_ERROR))
   4798       .RetiresOnSaturation();
   4799   EXPECT_CALL(
   4800       *gl_,
   4801       RenderbufferStorageMultisampleEXT(GL_RENDERBUFFER,
   4802                                         TestHelper::kMaxSamples,
   4803                                         GL_RGBA,
   4804                                         TestHelper::kMaxRenderbufferSize,
   4805                                         1))
   4806       .Times(1)
   4807       .RetiresOnSaturation();
   4808   EXPECT_CALL(*gl_, GetError())
   4809       .WillOnce(Return(GL_NO_ERROR))
   4810       .RetiresOnSaturation();
   4811   RenderbufferStorageMultisampleCHROMIUM cmd;
   4812   cmd.Init(GL_RENDERBUFFER, TestHelper::kMaxSamples,
   4813            GL_RGBA4, TestHelper::kMaxRenderbufferSize, 1);
   4814   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   4815   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   4816 }
   4817 
   4818 TEST_F(GLES2DecoderManualInitTest,
   4819        RenderbufferStorageMultisampleEXTNotSupported) {
   4820   InitDecoder(
   4821       "GL_EXT_framebuffer_multisample",  // extensions
   4822       false,   // has alpha
   4823       false,   // has depth
   4824       false,   // has stencil
   4825       false,   // request alpha
   4826       false,   // request depth
   4827       false,   // request stencil
   4828       false);  // bind generates resource
   4829   DoBindRenderbuffer(GL_RENDERBUFFER, client_renderbuffer_id_,
   4830                     kServiceRenderbufferId);
   4831   InSequence sequence;
   4832   // GL_EXT_framebuffer_multisample uses RenderbufferStorageMultisampleCHROMIUM.
   4833   RenderbufferStorageMultisampleEXT cmd;
   4834   cmd.Init(GL_RENDERBUFFER, TestHelper::kMaxSamples,
   4835            GL_RGBA4, TestHelper::kMaxRenderbufferSize, 1);
   4836   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   4837   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   4838 }
   4839 
   4840 class GLES2DecoderMultisampledRenderToTextureTest
   4841     : public GLES2DecoderTestWithExtensions {};
   4842 
   4843 TEST_P(GLES2DecoderMultisampledRenderToTextureTest,
   4844        NotCompatibleWithRenderbufferStorageMultisampleCHROMIUM) {
   4845   DoBindRenderbuffer(GL_RENDERBUFFER, client_renderbuffer_id_,
   4846                     kServiceRenderbufferId);
   4847   RenderbufferStorageMultisampleCHROMIUM cmd;
   4848   cmd.Init(GL_RENDERBUFFER, TestHelper::kMaxSamples,
   4849            GL_RGBA4, TestHelper::kMaxRenderbufferSize, 1);
   4850   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   4851   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   4852 }
   4853 
   4854 TEST_P(GLES2DecoderMultisampledRenderToTextureTest,
   4855        RenderbufferStorageMultisampleEXT) {
   4856   DoBindRenderbuffer(GL_RENDERBUFFER, client_renderbuffer_id_,
   4857                     kServiceRenderbufferId);
   4858   InSequence sequence;
   4859   EXPECT_CALL(*gl_, GetError())
   4860       .WillOnce(Return(GL_NO_ERROR))
   4861       .RetiresOnSaturation();
   4862   if (strstr(GetParam(), "GL_IMG_multisampled_render_to_texture")) {
   4863     EXPECT_CALL(
   4864         *gl_,
   4865         RenderbufferStorageMultisampleIMG(GL_RENDERBUFFER,
   4866                                           TestHelper::kMaxSamples,
   4867                                           GL_RGBA,
   4868                                           TestHelper::kMaxRenderbufferSize,
   4869                                           1))
   4870         .Times(1)
   4871         .RetiresOnSaturation();
   4872   } else {
   4873     EXPECT_CALL(
   4874         *gl_,
   4875         RenderbufferStorageMultisampleEXT(GL_RENDERBUFFER,
   4876                                           TestHelper::kMaxSamples,
   4877                                           GL_RGBA,
   4878                                           TestHelper::kMaxRenderbufferSize,
   4879                                           1))
   4880         .Times(1)
   4881         .RetiresOnSaturation();
   4882   }
   4883   EXPECT_CALL(*gl_, GetError())
   4884       .WillOnce(Return(GL_NO_ERROR))
   4885       .RetiresOnSaturation();
   4886   RenderbufferStorageMultisampleEXT cmd;
   4887   cmd.Init(GL_RENDERBUFFER, TestHelper::kMaxSamples,
   4888            GL_RGBA4, TestHelper::kMaxRenderbufferSize, 1);
   4889   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   4890   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   4891 }
   4892 
   4893 INSTANTIATE_TEST_CASE_P(
   4894     GLES2DecoderMultisampledRenderToTextureTests,
   4895     GLES2DecoderMultisampledRenderToTextureTest,
   4896     ::testing::Values("GL_EXT_multisampled_render_to_texture",
   4897                       "GL_IMG_multisampled_render_to_texture"));
   4898 
   4899 TEST_F(GLES2DecoderTest, ReadPixelsGLError) {
   4900   GLenum kFormat = GL_RGBA;
   4901   GLint x = 0;
   4902   GLint y = 0;
   4903   GLsizei width = 2;
   4904   GLsizei height = 4;
   4905   typedef ReadPixels::Result Result;
   4906   Result* result = GetSharedMemoryAs<Result*>();
   4907   uint32 result_shm_id = kSharedMemoryId;
   4908   uint32 result_shm_offset = kSharedMemoryOffset;
   4909   uint32 pixels_shm_id = kSharedMemoryId;
   4910   uint32 pixels_shm_offset = kSharedMemoryOffset + sizeof(*result);
   4911   EXPECT_CALL(*gl_, GetError())
   4912       .WillOnce(Return(GL_NO_ERROR))
   4913       .WillOnce(Return(GL_OUT_OF_MEMORY))
   4914       .RetiresOnSaturation();
   4915   EXPECT_CALL(
   4916       *gl_, ReadPixels(x, y, width, height, kFormat, GL_UNSIGNED_BYTE, _))
   4917       .Times(1)
   4918       .RetiresOnSaturation();
   4919   ReadPixels cmd;
   4920   cmd.Init(x, y, width, height, kFormat, GL_UNSIGNED_BYTE,
   4921            pixels_shm_id, pixels_shm_offset,
   4922            result_shm_id, result_shm_offset,
   4923            false);
   4924   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   4925   EXPECT_EQ(GL_OUT_OF_MEMORY, GetGLError());
   4926 }
   4927 
   4928 static bool ValueInArray(GLint value, GLint* array, GLint count) {
   4929   for (GLint ii = 0; ii < count; ++ii) {
   4930     if (array[ii] == value) {
   4931       return true;
   4932     }
   4933   }
   4934   return false;
   4935 }
   4936 
   4937 TEST_F(GLES2DecoderManualInitTest, GetCompressedTextureFormats) {
   4938   InitDecoder(
   4939       "GL_EXT_texture_compression_s3tc",  // extensions
   4940       false,   // has alpha
   4941       false,   // has depth
   4942       false,   // has stencil
   4943       false,   // request alpha
   4944       false,   // request depth
   4945       false,   // request stencil
   4946       true);   // bind generates resource
   4947 
   4948   EXPECT_CALL(*gl_, GetError())
   4949       .WillOnce(Return(GL_NO_ERROR))
   4950       .WillOnce(Return(GL_NO_ERROR))
   4951       .WillOnce(Return(GL_NO_ERROR))
   4952       .WillOnce(Return(GL_NO_ERROR))
   4953       .RetiresOnSaturation();
   4954 
   4955   typedef GetIntegerv::Result Result;
   4956   Result* result = static_cast<Result*>(shared_memory_address_);
   4957   GetIntegerv cmd;
   4958   result->size = 0;
   4959   EXPECT_CALL(*gl_, GetIntegerv(_, _))
   4960       .Times(0)
   4961       .RetiresOnSaturation();
   4962   cmd.Init(
   4963       GL_NUM_COMPRESSED_TEXTURE_FORMATS,
   4964       shared_memory_id_, shared_memory_offset_);
   4965   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   4966   EXPECT_EQ(1, result->GetNumResults());
   4967   GLint num_formats = result->GetData()[0];
   4968   EXPECT_EQ(4, num_formats);
   4969   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   4970 
   4971   result->size = 0;
   4972   cmd.Init(
   4973       GL_COMPRESSED_TEXTURE_FORMATS,
   4974       shared_memory_id_, shared_memory_offset_);
   4975   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   4976   EXPECT_EQ(num_formats, result->GetNumResults());
   4977 
   4978   EXPECT_TRUE(ValueInArray(
   4979       GL_COMPRESSED_RGB_S3TC_DXT1_EXT,
   4980       result->GetData(), result->GetNumResults()));
   4981   EXPECT_TRUE(ValueInArray(
   4982       GL_COMPRESSED_RGBA_S3TC_DXT1_EXT,
   4983       result->GetData(), result->GetNumResults()));
   4984   EXPECT_TRUE(ValueInArray(
   4985       GL_COMPRESSED_RGBA_S3TC_DXT3_EXT,
   4986       result->GetData(), result->GetNumResults()));
   4987   EXPECT_TRUE(ValueInArray(
   4988       GL_COMPRESSED_RGBA_S3TC_DXT5_EXT,
   4989       result->GetData(), result->GetNumResults()));
   4990 
   4991   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   4992 }
   4993 
   4994 TEST_F(GLES2DecoderManualInitTest, GetNoCompressedTextureFormats) {
   4995   InitDecoder(
   4996       "",  // extensions
   4997       false,   // has alpha
   4998       false,   // has depth
   4999       false,   // has stencil
   5000       false,   // request alpha
   5001       false,   // request depth
   5002       false,   // request stencil
   5003       true);   // bind generates resource
   5004 
   5005   EXPECT_CALL(*gl_, GetError())
   5006       .WillOnce(Return(GL_NO_ERROR))
   5007       .WillOnce(Return(GL_NO_ERROR))
   5008       .WillOnce(Return(GL_NO_ERROR))
   5009       .WillOnce(Return(GL_NO_ERROR))
   5010       .RetiresOnSaturation();
   5011 
   5012   typedef GetIntegerv::Result Result;
   5013   Result* result = static_cast<Result*>(shared_memory_address_);
   5014   GetIntegerv cmd;
   5015   result->size = 0;
   5016   EXPECT_CALL(*gl_, GetIntegerv(_, _))
   5017       .Times(0)
   5018       .RetiresOnSaturation();
   5019   cmd.Init(
   5020       GL_NUM_COMPRESSED_TEXTURE_FORMATS,
   5021       shared_memory_id_, shared_memory_offset_);
   5022   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   5023   EXPECT_EQ(1, result->GetNumResults());
   5024   GLint num_formats = result->GetData()[0];
   5025   EXPECT_EQ(0, num_formats);
   5026   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   5027 
   5028   result->size = 0;
   5029   cmd.Init(
   5030       GL_COMPRESSED_TEXTURE_FORMATS,
   5031       shared_memory_id_, shared_memory_offset_);
   5032   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   5033   EXPECT_EQ(num_formats, result->GetNumResults());
   5034 
   5035   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   5036 }
   5037 
   5038 TEST_F(GLES2DecoderManualInitTest, CompressedTexImage2DBucketBadBucket) {
   5039   InitDecoder(
   5040       "GL_EXT_texture_compression_s3tc",  // extensions
   5041       false,   // has alpha
   5042       false,   // has depth
   5043       false,   // has stencil
   5044       false,   // request alpha
   5045       false,   // request depth
   5046       false,   // request stencil
   5047       true);   // bind generates resource
   5048 
   5049   const uint32 kBadBucketId = 123;
   5050   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   5051   CompressedTexImage2DBucket cmd;
   5052   cmd.Init(
   5053       GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, 4, 4, 0,
   5054       kBadBucketId);
   5055   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   5056   CompressedTexSubImage2DBucket cmd2;
   5057   cmd2.Init(
   5058       GL_TEXTURE_2D, 0, 0, 0, 4, 4, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT,
   5059       kBadBucketId);
   5060   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   5061 }
   5062 
   5063 namespace {
   5064 
   5065 struct S3TCTestData {
   5066   GLenum format;
   5067   size_t block_size;
   5068 };
   5069 
   5070 }  // anonymous namespace.
   5071 
   5072 TEST_F(GLES2DecoderManualInitTest, CompressedTexImage2DS3TC) {
   5073   InitDecoder(
   5074       "GL_EXT_texture_compression_s3tc",  // extensions
   5075       false,   // has alpha
   5076       false,   // has depth
   5077       false,   // has stencil
   5078       false,   // request alpha
   5079       false,   // request depth
   5080       false,   // request stencil
   5081       true);   // bind generates resource
   5082   const uint32 kBucketId = 123;
   5083   CommonDecoder::Bucket* bucket = decoder_->CreateBucket(kBucketId);
   5084   ASSERT_TRUE(bucket != NULL);
   5085 
   5086   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   5087 
   5088   static const S3TCTestData test_data[] = {
   5089     { GL_COMPRESSED_RGB_S3TC_DXT1_EXT, 8, },
   5090     { GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, 8, },
   5091     { GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, 16, },
   5092     { GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, 16, },
   5093   };
   5094 
   5095   for (size_t ii = 0; ii < arraysize(test_data); ++ii) {
   5096     const S3TCTestData& test = test_data[ii];
   5097     CompressedTexImage2DBucket cmd;
   5098     // test small width.
   5099     DoCompressedTexImage2D(
   5100         GL_TEXTURE_2D, 0, test.format, 2, 4, 0, test.block_size,
   5101         kBucketId);
   5102     EXPECT_EQ(GL_NO_ERROR, GetGLError());
   5103 
   5104     // test bad width.
   5105     cmd.Init(
   5106         GL_TEXTURE_2D, 0, test.format, 5, 4, 0,
   5107         kBucketId);
   5108     bucket->SetSize(test.block_size * 2);
   5109     EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   5110     EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   5111 
   5112     // test small height.
   5113     DoCompressedTexImage2D(
   5114         GL_TEXTURE_2D, 0, test.format, 4, 2, 0, test.block_size,
   5115         kBucketId);
   5116     EXPECT_EQ(GL_NO_ERROR, GetGLError());
   5117 
   5118     // test too bad height.
   5119     cmd.Init(
   5120         GL_TEXTURE_2D, 0, test.format, 4, 5, 0,
   5121         kBucketId);
   5122     bucket->SetSize(test.block_size * 2);
   5123     EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   5124     EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   5125 
   5126     // test small for level 0.
   5127     DoCompressedTexImage2D(
   5128         GL_TEXTURE_2D, 0, test.format, 1, 1, 0, test.block_size,
   5129         kBucketId);
   5130     EXPECT_EQ(GL_NO_ERROR, GetGLError());
   5131 
   5132     // test small for level 0.
   5133     DoCompressedTexImage2D(
   5134         GL_TEXTURE_2D, 0, test.format, 2, 2, 0, test.block_size,
   5135         kBucketId);
   5136     EXPECT_EQ(GL_NO_ERROR, GetGLError());
   5137 
   5138     // test size too large.
   5139     cmd.Init(
   5140         GL_TEXTURE_2D, 0, test.format, 4, 4, 0,
   5141         kBucketId);
   5142     bucket->SetSize(test.block_size * 2);
   5143     EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   5144     EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   5145 
   5146     // test size too small.
   5147     cmd.Init(
   5148         GL_TEXTURE_2D, 0, test.format, 4, 4, 0,
   5149         kBucketId);
   5150     bucket->SetSize(test.block_size / 2);
   5151     EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   5152     EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   5153 
   5154     // test with 3 mips.
   5155     DoCompressedTexImage2D(
   5156         GL_TEXTURE_2D, 0, test.format, 4, 4, 0, test.block_size, kBucketId);
   5157     DoCompressedTexImage2D(
   5158         GL_TEXTURE_2D, 1, test.format, 2, 2, 0, test.block_size, kBucketId);
   5159     DoCompressedTexImage2D(
   5160         GL_TEXTURE_2D, 2, test.format, 1, 1, 0, test.block_size, kBucketId);
   5161     EXPECT_EQ(GL_NO_ERROR, GetGLError());
   5162 
   5163     // Test a 16x16
   5164     DoCompressedTexImage2D(
   5165         GL_TEXTURE_2D, 0, test.format, 16, 16, 0, test.block_size * 4 * 4,
   5166         kBucketId);
   5167     EXPECT_EQ(GL_NO_ERROR, GetGLError());
   5168 
   5169     CompressedTexSubImage2DBucket sub_cmd;
   5170     bucket->SetSize(test.block_size);
   5171     // Test sub image bad xoffset
   5172     sub_cmd.Init(
   5173         GL_TEXTURE_2D, 0, 1, 0, 4, 4, test.format, kBucketId);
   5174     EXPECT_EQ(error::kNoError, ExecuteCmd(sub_cmd));
   5175     EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   5176 
   5177     // Test sub image bad yoffset
   5178     sub_cmd.Init(
   5179         GL_TEXTURE_2D, 0, 0, 2, 4, 4, test.format, kBucketId);
   5180     EXPECT_EQ(error::kNoError, ExecuteCmd(sub_cmd));
   5181     EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   5182 
   5183     // Test sub image bad width
   5184     bucket->SetSize(test.block_size * 2);
   5185     sub_cmd.Init(
   5186         GL_TEXTURE_2D, 0, 0, 0, 5, 4, test.format, kBucketId);
   5187     EXPECT_EQ(error::kNoError, ExecuteCmd(sub_cmd));
   5188     EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   5189 
   5190     // Test sub image bad height
   5191     sub_cmd.Init(
   5192         GL_TEXTURE_2D, 0, 0, 0, 4, 5, test.format, kBucketId);
   5193     EXPECT_EQ(error::kNoError, ExecuteCmd(sub_cmd));
   5194     EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   5195 
   5196     // Test sub image bad size
   5197     bucket->SetSize(test.block_size + 1);
   5198     sub_cmd.Init(
   5199         GL_TEXTURE_2D, 0, 0, 0, 4, 4, test.format, kBucketId);
   5200     EXPECT_EQ(error::kNoError, ExecuteCmd(sub_cmd));
   5201     EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   5202 
   5203     for (GLint yoffset = 0; yoffset <= 8; yoffset += 4) {
   5204       for (GLint xoffset = 0; xoffset <= 8; xoffset += 4) {
   5205         for (GLsizei height = 4; height <= 8; height +=4 ) {
   5206           for (GLsizei width = 4; width <= 8; width += 4) {
   5207             GLsizei size = test.block_size * (width / 4) * (height / 4);
   5208             bucket->SetSize(size);
   5209             EXPECT_CALL(*gl_, CompressedTexSubImage2D(
   5210                 GL_TEXTURE_2D, 0, xoffset, yoffset, width, height, test.format,
   5211                 size, _))
   5212                 .Times(1)
   5213                 .RetiresOnSaturation();
   5214             sub_cmd.Init(
   5215                 GL_TEXTURE_2D, 0, xoffset, yoffset, width, height, test.format,
   5216                 kBucketId);
   5217             EXPECT_EQ(error::kNoError, ExecuteCmd(sub_cmd));
   5218             EXPECT_EQ(GL_NO_ERROR, GetGLError());
   5219           }
   5220         }
   5221       }
   5222     }
   5223   }
   5224 }
   5225 
   5226 TEST_F(GLES2DecoderManualInitTest, CompressedTexImage2DETC1) {
   5227   InitDecoder(
   5228       "GL_OES_compressed_ETC1_RGB8_texture",  // extensions
   5229       false,   // has alpha
   5230       false,   // has depth
   5231       false,   // has stencil
   5232       false,   // request alpha
   5233       false,   // request depth
   5234       false,   // request stencil
   5235       true);   // bind generates resource
   5236   const uint32 kBucketId = 123;
   5237   CommonDecoder::Bucket* bucket = decoder_->CreateBucket(kBucketId);
   5238   ASSERT_TRUE(bucket != NULL);
   5239 
   5240   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   5241 
   5242   const GLenum kFormat = GL_ETC1_RGB8_OES;
   5243   const size_t kBlockSize = 8;
   5244 
   5245   CompressedTexImage2DBucket cmd;
   5246   // test small width.
   5247   DoCompressedTexImage2D(GL_TEXTURE_2D, 0, kFormat, 4, 8, 0, 16, kBucketId);
   5248   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   5249 
   5250   // test small height.
   5251   DoCompressedTexImage2D(GL_TEXTURE_2D, 0, kFormat, 8, 4, 0, 16, kBucketId);
   5252   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   5253 
   5254   // test size too large.
   5255   cmd.Init(GL_TEXTURE_2D, 0, kFormat, 4, 4, 0, kBucketId);
   5256   bucket->SetSize(kBlockSize * 2);
   5257   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   5258   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   5259 
   5260   // test size too small.
   5261   cmd.Init(GL_TEXTURE_2D, 0, kFormat, 4, 4, 0, kBucketId);
   5262   bucket->SetSize(kBlockSize / 2);
   5263   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   5264   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   5265 
   5266   // Test a 16x16
   5267   DoCompressedTexImage2D(
   5268       GL_TEXTURE_2D, 0, kFormat, 16, 16, 0, kBlockSize * 16, kBucketId);
   5269   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   5270 
   5271   // Test CompressedTexSubImage not allowed
   5272   CompressedTexSubImage2DBucket sub_cmd;
   5273   bucket->SetSize(kBlockSize);
   5274   sub_cmd.Init(GL_TEXTURE_2D, 0, 0, 0, 4, 4, kFormat, kBucketId);
   5275   EXPECT_EQ(error::kNoError, ExecuteCmd(sub_cmd));
   5276   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   5277 
   5278   // Test TexSubImage not allowed for ETC1 compressed texture
   5279   TextureRef* texture_ref = GetTexture(client_texture_id_);
   5280   ASSERT_TRUE(texture_ref != NULL);
   5281   Texture* texture = texture_ref->texture();
   5282   GLenum type, internal_format;
   5283   EXPECT_TRUE(texture->GetLevelType(GL_TEXTURE_2D, 0, &type, &internal_format));
   5284   EXPECT_EQ(kFormat, internal_format);
   5285   TexSubImage2D texsub_cmd;
   5286   texsub_cmd.Init(GL_TEXTURE_2D, 0, 0, 0, 4, 4, GL_RGBA, GL_UNSIGNED_BYTE,
   5287            kSharedMemoryId, kSharedMemoryOffset, GL_FALSE);
   5288   EXPECT_EQ(error::kNoError, ExecuteCmd(texsub_cmd));
   5289   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   5290 
   5291   // Test CopyTexSubImage not allowed for ETC1 compressed texture
   5292   CopyTexSubImage2D copy_cmd;
   5293   copy_cmd.Init(GL_TEXTURE_2D, 0, 0, 0, 0, 0, 4, 4);
   5294   EXPECT_EQ(error::kNoError, ExecuteCmd(copy_cmd));
   5295   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   5296 }
   5297 
   5298 TEST_F(GLES2DecoderManualInitTest, GetCompressedTextureFormatsETC1) {
   5299   InitDecoder(
   5300       "GL_OES_compressed_ETC1_RGB8_texture",  // extensions
   5301       false,   // has alpha
   5302       false,   // has depth
   5303       false,   // has stencil
   5304       false,   // request alpha
   5305       false,   // request depth
   5306       false,   // request stencil
   5307       true);   // bind generates resource
   5308 
   5309   EXPECT_CALL(*gl_, GetError())
   5310       .WillOnce(Return(GL_NO_ERROR))
   5311       .WillOnce(Return(GL_NO_ERROR))
   5312       .WillOnce(Return(GL_NO_ERROR))
   5313       .WillOnce(Return(GL_NO_ERROR))
   5314       .RetiresOnSaturation();
   5315 
   5316   typedef GetIntegerv::Result Result;
   5317   Result* result = static_cast<Result*>(shared_memory_address_);
   5318   GetIntegerv cmd;
   5319   result->size = 0;
   5320   EXPECT_CALL(*gl_, GetIntegerv(_, _))
   5321       .Times(0)
   5322       .RetiresOnSaturation();
   5323   cmd.Init(
   5324       GL_NUM_COMPRESSED_TEXTURE_FORMATS,
   5325       shared_memory_id_, shared_memory_offset_);
   5326   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   5327   EXPECT_EQ(1, result->GetNumResults());
   5328   GLint num_formats = result->GetData()[0];
   5329   EXPECT_EQ(1, num_formats);
   5330   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   5331 
   5332   result->size = 0;
   5333   cmd.Init(
   5334       GL_COMPRESSED_TEXTURE_FORMATS,
   5335       shared_memory_id_, shared_memory_offset_);
   5336   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   5337   EXPECT_EQ(num_formats, result->GetNumResults());
   5338 
   5339   EXPECT_TRUE(ValueInArray(
   5340       GL_ETC1_RGB8_OES,
   5341       result->GetData(), result->GetNumResults()));
   5342   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   5343 }
   5344 
   5345 TEST_F(GLES2DecoderWithShaderTest, GetProgramInfoCHROMIUMValidArgs) {
   5346   const uint32 kBucketId = 123;
   5347   GetProgramInfoCHROMIUM cmd;
   5348   cmd.Init(client_program_id_, kBucketId);
   5349   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   5350   CommonDecoder::Bucket* bucket = decoder_->GetBucket(kBucketId);
   5351   EXPECT_GT(bucket->size(), 0u);
   5352 }
   5353 
   5354 TEST_F(GLES2DecoderWithShaderTest, GetProgramInfoCHROMIUMInvalidArgs) {
   5355   const uint32 kBucketId = 123;
   5356   CommonDecoder::Bucket* bucket = decoder_->GetBucket(kBucketId);
   5357   EXPECT_TRUE(bucket == NULL);
   5358   GetProgramInfoCHROMIUM cmd;
   5359   cmd.Init(kInvalidClientId, kBucketId);
   5360   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   5361   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   5362   bucket = decoder_->GetBucket(kBucketId);
   5363   ASSERT_TRUE(bucket != NULL);
   5364   EXPECT_EQ(sizeof(ProgramInfoHeader), bucket->size());
   5365   ProgramInfoHeader* info = bucket->GetDataAs<ProgramInfoHeader*>(
   5366       0, sizeof(ProgramInfoHeader));
   5367   ASSERT_TRUE(info != 0);
   5368   EXPECT_EQ(0u, info->link_status);
   5369   EXPECT_EQ(0u, info->num_attribs);
   5370   EXPECT_EQ(0u, info->num_uniforms);
   5371 }
   5372 
   5373 TEST_F(GLES2DecoderManualInitTest, EGLImageExternalBindTexture) {
   5374   InitDecoder(
   5375       "GL_OES_EGL_image_external",  // extensions
   5376       false,   // has alpha
   5377       false,   // has depth
   5378       false,   // has stencil
   5379       false,   // request alpha
   5380       false,   // request depth
   5381       false,   // request stencil
   5382       true);   // bind generates resource
   5383   EXPECT_CALL(*gl_, BindTexture(GL_TEXTURE_EXTERNAL_OES, kNewServiceId));
   5384   EXPECT_CALL(*gl_, GenTextures(1, _))
   5385       .WillOnce(SetArgumentPointee<1>(kNewServiceId));
   5386   BindTexture cmd;
   5387   cmd.Init(GL_TEXTURE_EXTERNAL_OES, kNewClientId);
   5388   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   5389   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   5390   TextureRef* texture_ref = GetTexture(kNewClientId);
   5391   EXPECT_TRUE(texture_ref != NULL);
   5392   EXPECT_TRUE(texture_ref->texture()->target() == GL_TEXTURE_EXTERNAL_OES);
   5393 }
   5394 
   5395 TEST_F(GLES2DecoderManualInitTest, EGLImageExternalGetBinding) {
   5396   InitDecoder(
   5397       "GL_OES_EGL_image_external",  // extensions
   5398       false,   // has alpha
   5399       false,   // has depth
   5400       false,   // has stencil
   5401       false,   // request alpha
   5402       false,   // request depth
   5403       false,   // request stencil
   5404       true);   // bind generates resource
   5405   DoBindTexture(GL_TEXTURE_EXTERNAL_OES, client_texture_id_, kServiceTextureId);
   5406 
   5407   EXPECT_CALL(*gl_, GetError())
   5408       .WillOnce(Return(GL_NO_ERROR))
   5409       .WillOnce(Return(GL_NO_ERROR))
   5410       .RetiresOnSaturation();
   5411   typedef GetIntegerv::Result Result;
   5412   Result* result = static_cast<Result*>(shared_memory_address_);
   5413   EXPECT_CALL(*gl_, GetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES,
   5414                                 result->GetData()))
   5415       .Times(0);
   5416   result->size = 0;
   5417   GetIntegerv cmd;
   5418   cmd.Init(GL_TEXTURE_BINDING_EXTERNAL_OES,
   5419            shared_memory_id_,
   5420            shared_memory_offset_);
   5421   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   5422   EXPECT_EQ(decoder_->GetGLES2Util()->GLGetNumValuesReturned(
   5423       GL_TEXTURE_BINDING_EXTERNAL_OES), result->GetNumResults());
   5424   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   5425   EXPECT_EQ(client_texture_id_, (uint32)result->GetData()[0]);
   5426 }
   5427 
   5428 TEST_F(GLES2DecoderManualInitTest, EGLImageExternalTextureDefaults) {
   5429   InitDecoder(
   5430       "GL_OES_EGL_image_external",  // extensions
   5431       false,   // has alpha
   5432       false,   // has depth
   5433       false,   // has stencil
   5434       false,   // request alpha
   5435       false,   // request depth
   5436       false,   // request stencil
   5437       true);   // bind generates resource
   5438   DoBindTexture(GL_TEXTURE_EXTERNAL_OES, client_texture_id_, kServiceTextureId);
   5439 
   5440   TextureRef* texture_ref = GetTexture(client_texture_id_);
   5441   EXPECT_TRUE(texture_ref != NULL);
   5442   Texture* texture = texture_ref->texture();
   5443   EXPECT_TRUE(texture->target() == GL_TEXTURE_EXTERNAL_OES);
   5444   EXPECT_TRUE(texture->min_filter() == GL_LINEAR);
   5445   EXPECT_TRUE(texture->wrap_s() == GL_CLAMP_TO_EDGE);
   5446   EXPECT_TRUE(texture->wrap_t() == GL_CLAMP_TO_EDGE);
   5447 }
   5448 
   5449 TEST_F(GLES2DecoderManualInitTest, EGLImageExternalTextureParam) {
   5450   InitDecoder(
   5451       "GL_OES_EGL_image_external",  // extensions
   5452       false,   // has alpha
   5453       false,   // has depth
   5454       false,   // has stencil
   5455       false,   // request alpha
   5456       false,   // request depth
   5457       false,   // request stencil
   5458       true);   // bind generates resource
   5459 
   5460   DoBindTexture(GL_TEXTURE_EXTERNAL_OES, client_texture_id_, kServiceTextureId);
   5461 
   5462   EXPECT_CALL(*gl_, TexParameteri(GL_TEXTURE_EXTERNAL_OES,
   5463                                   GL_TEXTURE_MIN_FILTER,
   5464                                   GL_NEAREST));
   5465   EXPECT_CALL(*gl_, TexParameteri(GL_TEXTURE_EXTERNAL_OES,
   5466                                   GL_TEXTURE_MIN_FILTER,
   5467                                   GL_LINEAR));
   5468   EXPECT_CALL(*gl_, TexParameteri(GL_TEXTURE_EXTERNAL_OES,
   5469                                   GL_TEXTURE_WRAP_S,
   5470                                   GL_CLAMP_TO_EDGE));
   5471   EXPECT_CALL(*gl_, TexParameteri(GL_TEXTURE_EXTERNAL_OES,
   5472                                   GL_TEXTURE_WRAP_T,
   5473                                   GL_CLAMP_TO_EDGE));
   5474   TexParameteri cmd;
   5475   cmd.Init(GL_TEXTURE_EXTERNAL_OES,
   5476            GL_TEXTURE_MIN_FILTER,
   5477            GL_NEAREST);
   5478   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   5479   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   5480 
   5481   cmd.Init(GL_TEXTURE_EXTERNAL_OES,
   5482            GL_TEXTURE_MIN_FILTER,
   5483            GL_LINEAR);
   5484   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   5485   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   5486 
   5487   cmd.Init(GL_TEXTURE_EXTERNAL_OES,
   5488            GL_TEXTURE_WRAP_S,
   5489            GL_CLAMP_TO_EDGE);
   5490   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   5491   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   5492 
   5493   cmd.Init(GL_TEXTURE_EXTERNAL_OES,
   5494            GL_TEXTURE_WRAP_T,
   5495            GL_CLAMP_TO_EDGE);
   5496   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   5497   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   5498 
   5499   TextureRef* texture_ref = GetTexture(client_texture_id_);
   5500   EXPECT_TRUE(texture_ref != NULL);
   5501   Texture* texture = texture_ref->texture();
   5502   EXPECT_TRUE(texture->target() == GL_TEXTURE_EXTERNAL_OES);
   5503   EXPECT_TRUE(texture->min_filter() == GL_LINEAR);
   5504   EXPECT_TRUE(texture->wrap_s() == GL_CLAMP_TO_EDGE);
   5505   EXPECT_TRUE(texture->wrap_t() == GL_CLAMP_TO_EDGE);
   5506 }
   5507 
   5508 TEST_F(GLES2DecoderManualInitTest, EGLImageExternalTextureParamInvalid) {
   5509   InitDecoder(
   5510       "GL_OES_EGL_image_external",  // extensions
   5511       false,   // has alpha
   5512       false,   // has depth
   5513       false,   // has stencil
   5514       false,   // request alpha
   5515       false,   // request depth
   5516       false,   // request stencil
   5517       true);   // bind generates resource
   5518 
   5519   DoBindTexture(GL_TEXTURE_EXTERNAL_OES, client_texture_id_, kServiceTextureId);
   5520 
   5521   TexParameteri cmd;
   5522   cmd.Init(GL_TEXTURE_EXTERNAL_OES,
   5523            GL_TEXTURE_MIN_FILTER,
   5524            GL_NEAREST_MIPMAP_NEAREST);
   5525   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   5526   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
   5527 
   5528   cmd.Init(GL_TEXTURE_EXTERNAL_OES,
   5529            GL_TEXTURE_WRAP_S,
   5530            GL_REPEAT);
   5531   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   5532   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
   5533 
   5534   cmd.Init(GL_TEXTURE_EXTERNAL_OES,
   5535            GL_TEXTURE_WRAP_T,
   5536            GL_REPEAT);
   5537   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   5538   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
   5539 
   5540   TextureRef* texture_ref = GetTexture(client_texture_id_);
   5541   EXPECT_TRUE(texture_ref != NULL);
   5542   Texture* texture = texture_ref->texture();
   5543   EXPECT_TRUE(texture->target() == GL_TEXTURE_EXTERNAL_OES);
   5544   EXPECT_TRUE(texture->min_filter() == GL_LINEAR);
   5545   EXPECT_TRUE(texture->wrap_s() == GL_CLAMP_TO_EDGE);
   5546   EXPECT_TRUE(texture->wrap_t() == GL_CLAMP_TO_EDGE);
   5547 }
   5548 
   5549 TEST_F(GLES2DecoderManualInitTest, EGLImageExternalTexImage2DError) {
   5550   InitDecoder(
   5551       "GL_OES_EGL_image_external",  // extensions
   5552       false,   // has alpha
   5553       false,   // has depth
   5554       false,   // has stencil
   5555       false,   // request alpha
   5556       false,   // request depth
   5557       false,   // request stencil
   5558       true);   // bind generates resource
   5559 
   5560   GLenum target = GL_TEXTURE_EXTERNAL_OES;
   5561   GLint level = 0;
   5562   GLenum internal_format = GL_RGBA;
   5563   GLsizei width = 2;
   5564   GLsizei height = 4;
   5565   GLint border = 0;
   5566   GLenum format = GL_RGBA;
   5567   GLenum type = GL_UNSIGNED_BYTE;
   5568   DoBindTexture(GL_TEXTURE_EXTERNAL_OES, client_texture_id_, kServiceTextureId);
   5569   ASSERT_TRUE(GetTexture(client_texture_id_) != NULL);
   5570   TexImage2D cmd;
   5571   cmd.Init(target, level, internal_format, width, height, border, format,
   5572            type, kSharedMemoryId, kSharedMemoryOffset);
   5573   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   5574 
   5575   // TexImage2D is not allowed with GL_TEXTURE_EXTERNAL_OES targets.
   5576   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
   5577 }
   5578 
   5579 TEST_F(GLES2DecoderManualInitTest, BindGeneratesResourceFalse) {
   5580   InitDecoder(
   5581       "",      // extensions
   5582       false,   // has alpha
   5583       false,   // has depth
   5584       false,   // has stencil
   5585       false,   // request alpha
   5586       false,   // request depth
   5587       false,   // request stencil
   5588       false);  // bind generates resource
   5589 
   5590   BindTexture cmd1;
   5591   cmd1.Init(GL_TEXTURE_2D, kInvalidClientId);
   5592   EXPECT_NE(error::kNoError, ExecuteCmd(cmd1));
   5593 
   5594   BindBuffer cmd2;
   5595   cmd2.Init(GL_ARRAY_BUFFER, kInvalidClientId);
   5596   EXPECT_NE(error::kNoError, ExecuteCmd(cmd2));
   5597 
   5598   BindFramebuffer cmd3;
   5599   cmd3.Init(GL_FRAMEBUFFER, kInvalidClientId);
   5600   EXPECT_NE(error::kNoError, ExecuteCmd(cmd3));
   5601 
   5602   BindRenderbuffer cmd4;
   5603   cmd4.Init(GL_RENDERBUFFER, kInvalidClientId);
   5604   EXPECT_NE(error::kNoError, ExecuteCmd(cmd4));
   5605 }
   5606 
   5607 TEST_F(GLES2DecoderManualInitTest, CreateStreamTextureCHROMIUM) {
   5608   const GLuint kObjectId = 123;
   5609   InitDecoder(
   5610       "GL_CHROMIUM_stream_texture",  // extensions
   5611       false,   // has alpha
   5612       false,   // has depth
   5613       false,   // has stencil
   5614       false,   // request alpha
   5615       false,   // request depth
   5616       false,   // request stencil
   5617       true);   // bind generates resource
   5618 
   5619   EXPECT_CALL(*stream_texture_manager(), CreateStreamTexture(
   5620       kServiceTextureId, client_texture_id_))
   5621       .WillOnce(Return(kObjectId))
   5622       .RetiresOnSaturation();
   5623 
   5624   CreateStreamTextureCHROMIUM cmd;
   5625   CreateStreamTextureCHROMIUM::Result* result =
   5626       static_cast<CreateStreamTextureCHROMIUM::Result*>(shared_memory_address_);
   5627   cmd.Init(client_texture_id_, shared_memory_id_, shared_memory_offset_);
   5628   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   5629   EXPECT_EQ(kObjectId, *result);
   5630   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   5631   TextureRef* texture_ref = GetTexture(client_texture_id_);
   5632   EXPECT_TRUE(texture_ref != NULL);
   5633   EXPECT_TRUE(texture_ref->texture()->IsStreamTexture());
   5634   EXPECT_CALL(*stream_texture_manager(),
   5635               DestroyStreamTexture(kServiceTextureId))
   5636       .Times(1)
   5637       .RetiresOnSaturation();
   5638 }
   5639 
   5640 TEST_F(GLES2DecoderManualInitTest, CreateStreamTextureCHROMIUMBadId) {
   5641   InitDecoder(
   5642       "GL_CHROMIUM_stream_texture",  // extensions
   5643       false,   // has alpha
   5644       false,   // has depth
   5645       false,   // has stencil
   5646       false,   // request alpha
   5647       false,   // request depth
   5648       false,   // request stencil
   5649       true);   // bind generates resource
   5650 
   5651   CreateStreamTextureCHROMIUM cmd;
   5652   CreateStreamTextureCHROMIUM::Result* result =
   5653       static_cast<CreateStreamTextureCHROMIUM::Result*>(shared_memory_address_);
   5654   cmd.Init(kNewClientId, shared_memory_id_, shared_memory_offset_);
   5655   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   5656   EXPECT_EQ(static_cast<GLuint>(GL_ZERO), *result);
   5657   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   5658 }
   5659 
   5660 TEST_F(GLES2DecoderManualInitTest, CreateStreamTextureCHROMIUMAlreadyBound) {
   5661   InitDecoder(
   5662       "GL_CHROMIUM_stream_texture",  // extensions
   5663       false,   // has alpha
   5664       false,   // has depth
   5665       false,   // has stencil
   5666       false,   // request alpha
   5667       false,   // request depth
   5668       false,   // request stencil
   5669       true);   // bind generates resource
   5670   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   5671 
   5672   CreateStreamTextureCHROMIUM cmd;
   5673   CreateStreamTextureCHROMIUM::Result* result =
   5674       static_cast<CreateStreamTextureCHROMIUM::Result*>(shared_memory_address_);
   5675   cmd.Init(client_texture_id_, shared_memory_id_, shared_memory_offset_);
   5676   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   5677   EXPECT_EQ(static_cast<GLuint>(GL_ZERO), *result);
   5678   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   5679 }
   5680 
   5681 TEST_F(GLES2DecoderManualInitTest, CreateStreamTextureCHROMIUMAlreadySet) {
   5682   InitDecoder(
   5683       "GL_CHROMIUM_stream_texture",  // extensions
   5684       false,   // has alpha
   5685       false,   // has depth
   5686       false,   // has stencil
   5687       false,   // request alpha
   5688       false,   // request depth
   5689       false,   // request stencil
   5690       true);   // bind generates resource
   5691 
   5692   TextureRef* texture_ref = GetTexture(client_texture_id_);
   5693   group().texture_manager()->SetStreamTexture(texture_ref, true);
   5694 
   5695   CreateStreamTextureCHROMIUM cmd;
   5696   cmd.Init(client_texture_id_, shared_memory_id_, shared_memory_offset_);
   5697   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   5698   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   5699 
   5700   EXPECT_CALL(*stream_texture_manager(),
   5701               DestroyStreamTexture(kServiceTextureId))
   5702       .Times(1)
   5703       .RetiresOnSaturation();
   5704 }
   5705 
   5706 TEST_F(GLES2DecoderManualInitTest, DrawStreamTextureCHROMIUM) {
   5707   InitDecoder(
   5708       "GL_CHROMIUM_stream_texture GL_OES_EGL_image_external",  // extensions
   5709       true,                                                    // has alpha
   5710       true,                                                    // has depth
   5711       false,                                                   // has stencil
   5712       true,                                                    // request alpha
   5713       true,                                                    // request depth
   5714       false,  // request stencil
   5715       true);  // bind generates resource
   5716 
   5717   StrictMock<MockStreamTexture> stream_texture;
   5718 
   5719   TextureRef* texture_ref = GetTexture(client_texture_id_);
   5720   group().texture_manager()->SetStreamTexture(texture_ref, true);
   5721 
   5722   DoBindTexture(GL_TEXTURE_EXTERNAL_OES, client_texture_id_, kServiceTextureId);
   5723   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   5724 
   5725   SetupSamplerExternalProgram();
   5726   SetupIndexBuffer();
   5727   AddExpectationsForSimulatedAttrib0(kMaxValidIndex + 1, 0);
   5728   SetupExpectationsForApplyingDefaultDirtyState();
   5729   EXPECT_TRUE(group().texture_manager()->CanRender(texture_ref));
   5730 
   5731   InSequence s;
   5732   EXPECT_CALL(*stream_texture_manager(), LookupStreamTexture(kServiceTextureId))
   5733       .WillOnce(Return(&stream_texture))
   5734       .RetiresOnSaturation();
   5735   EXPECT_CALL(*gl_, ActiveTexture(GL_TEXTURE0))
   5736       .Times(1)
   5737       .RetiresOnSaturation();
   5738   EXPECT_CALL(stream_texture, Update())
   5739       .Times(1)
   5740       .RetiresOnSaturation();
   5741   EXPECT_CALL(*gl_, DrawElements(_, _, _, _))
   5742       .Times(1);
   5743   DrawElements cmd;
   5744   cmd.Init(GL_TRIANGLES, kValidIndexRangeCount, GL_UNSIGNED_SHORT,
   5745            kValidIndexRangeStart * 2);
   5746   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   5747   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   5748 
   5749   EXPECT_CALL(*stream_texture_manager(),
   5750               DestroyStreamTexture(kServiceTextureId))
   5751       .Times(1)
   5752       .RetiresOnSaturation();
   5753 }
   5754 
   5755 TEST_F(GLES2DecoderManualInitTest, BindStreamTextureCHROMIUMInvalid) {
   5756   InitDecoder(
   5757       "GL_CHROMIUM_stream_texture",  // extensions
   5758       false,   // has alpha
   5759       false,   // has depth
   5760       false,   // has stencil
   5761       false,   // request alpha
   5762       false,   // request depth
   5763       false,   // request stencil
   5764       true);   // bind generates resource
   5765 
   5766   TextureRef* texture_ref = GetTexture(client_texture_id_);
   5767   group().texture_manager()->SetStreamTexture(texture_ref, true);
   5768 
   5769   BindTexture cmd;
   5770   cmd.Init(GL_TEXTURE_2D, client_texture_id_);
   5771   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   5772   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   5773 
   5774   BindTexture cmd2;
   5775   cmd2.Init(GL_TEXTURE_CUBE_MAP, client_texture_id_);
   5776   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd2));
   5777   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   5778 
   5779   EXPECT_CALL(*stream_texture_manager(),
   5780               DestroyStreamTexture(kServiceTextureId))
   5781       .Times(1)
   5782       .RetiresOnSaturation();
   5783 }
   5784 
   5785 TEST_F(GLES2DecoderManualInitTest, DestroyStreamTextureCHROMIUM) {
   5786   InitDecoder(
   5787       "GL_CHROMIUM_stream_texture",  // extensions
   5788       false,   // has alpha
   5789       false,   // has depth
   5790       false,   // has stencil
   5791       false,   // request alpha
   5792       false,   // request depth
   5793       false,   // request stencil
   5794       true);   // bind generates resource
   5795 
   5796   TextureRef* texture_ref = GetTexture(client_texture_id_);
   5797   group().texture_manager()->SetStreamTexture(texture_ref, true);
   5798 
   5799   EXPECT_CALL(*stream_texture_manager(),
   5800               DestroyStreamTexture(kServiceTextureId))
   5801       .Times(1)
   5802       .RetiresOnSaturation();
   5803 
   5804   DestroyStreamTextureCHROMIUM cmd;
   5805   cmd.Init(client_texture_id_);
   5806   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   5807   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   5808   EXPECT_FALSE(texture_ref->texture()->IsStreamTexture());
   5809   EXPECT_EQ(0U, texture_ref->texture()->target());
   5810 }
   5811 
   5812 TEST_F(GLES2DecoderManualInitTest, DestroyStreamTextureCHROMIUMInvalid) {
   5813   InitDecoder(
   5814       "GL_CHROMIUM_stream_texture",  // extensions
   5815       false,   // has alpha
   5816       false,   // has depth
   5817       false,   // has stencil
   5818       false,   // request alpha
   5819       false,   // request depth
   5820       false,   // request stencil
   5821       true);   // bind generates resource
   5822 
   5823   DestroyStreamTextureCHROMIUM cmd;
   5824   cmd.Init(client_texture_id_);
   5825   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   5826   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   5827 }
   5828 
   5829 TEST_F(GLES2DecoderManualInitTest, DestroyStreamTextureCHROMIUMBadId) {
   5830   InitDecoder(
   5831       "GL_CHROMIUM_stream_texture",  // extensions
   5832       false,   // has alpha
   5833       false,   // has depth
   5834       false,   // has stencil
   5835       false,   // request alpha
   5836       false,   // request depth
   5837       false,   // request stencil
   5838       true);   // bind generates resource
   5839 
   5840   DestroyStreamTextureCHROMIUM cmd;
   5841   cmd.Init(GL_ZERO);
   5842   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   5843   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   5844 }
   5845 
   5846 TEST_F(GLES2DecoderManualInitTest, StreamTextureCHROMIUMNullMgr) {
   5847   InitDecoder(
   5848       "",  // extensions
   5849       false,   // has alpha
   5850       false,   // has depth
   5851       false,   // has stencil
   5852       false,   // request alpha
   5853       false,   // request depth
   5854       false,   // request stencil
   5855       true);   // bind generates resource
   5856 
   5857   CreateStreamTextureCHROMIUM cmd;
   5858   cmd.Init(client_texture_id_, shared_memory_id_, shared_memory_offset_);
   5859   EXPECT_EQ(error::kInvalidArguments, ExecuteCmd(cmd));
   5860   GetGLError(); // ignore internal error
   5861 
   5862   TextureRef* texture_ref = GetTexture(client_texture_id_);
   5863   group().texture_manager()->SetStreamTexture(texture_ref, true);
   5864 
   5865   DestroyStreamTextureCHROMIUM cmd2;
   5866   cmd2.Init(client_texture_id_);
   5867   EXPECT_EQ(error::kInvalidArguments, ExecuteCmd(cmd2));
   5868   GetGLError(); // ignore internal error
   5869 }
   5870 
   5871 TEST_F(GLES2DecoderManualInitTest, ReCreateStreamTextureCHROMIUM) {
   5872   const GLuint kObjectId = 123;
   5873   InitDecoder(
   5874       "GL_CHROMIUM_stream_texture GL_OES_EGL_image_external",  // extensions
   5875       false,   // has alpha
   5876       false,   // has depth
   5877       false,   // has stencil
   5878       false,   // request alpha
   5879       false,   // request depth
   5880       false,   // request stencil
   5881       true);   // bind generates resource
   5882 
   5883   EXPECT_CALL(*stream_texture_manager(),
   5884               DestroyStreamTexture(kServiceTextureId))
   5885       .Times(1)
   5886       .RetiresOnSaturation();
   5887   EXPECT_CALL(*stream_texture_manager(),
   5888               CreateStreamTexture(kServiceTextureId, client_texture_id_))
   5889       .WillOnce(Return(kObjectId))
   5890       .RetiresOnSaturation();
   5891 
   5892   TextureRef* texture_ref = GetTexture(client_texture_id_);
   5893   group().texture_manager()->SetStreamTexture(texture_ref, true);
   5894 
   5895   DoBindTexture(GL_TEXTURE_EXTERNAL_OES, client_texture_id_, kServiceTextureId);
   5896   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   5897 
   5898   DestroyStreamTextureCHROMIUM cmd;
   5899   cmd.Init(client_texture_id_);
   5900   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   5901   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   5902   EXPECT_FALSE(texture_ref->texture()->IsStreamTexture());
   5903 
   5904   CreateStreamTextureCHROMIUM cmd2;
   5905   cmd2.Init(client_texture_id_, shared_memory_id_, shared_memory_offset_);
   5906   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd2));
   5907   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   5908   EXPECT_TRUE(texture_ref->texture()->IsStreamTexture());
   5909 
   5910   EXPECT_CALL(*stream_texture_manager(),
   5911               DestroyStreamTexture(kServiceTextureId))
   5912       .Times(1)
   5913       .RetiresOnSaturation();
   5914 }
   5915 
   5916 TEST_F(GLES2DecoderManualInitTest, ProduceAndConsumeStreamTextureCHROMIUM) {
   5917   InitDecoder(
   5918       "GL_CHROMIUM_stream_texture GL_OES_EGL_image_external",  // extensions
   5919       false,   // has alpha
   5920       false,   // has depth
   5921       false,   // has stencil
   5922       false,   // request alpha
   5923       false,   // request depth
   5924       false,   // request stencil
   5925       true);   // bind generates resource
   5926 
   5927   TextureRef* texture_ref = GetTexture(client_texture_id_);
   5928   group().texture_manager()->SetStreamTexture(texture_ref, true);
   5929 
   5930   DoBindTexture(GL_TEXTURE_EXTERNAL_OES, client_texture_id_, kServiceTextureId);
   5931 
   5932   GLbyte mailbox[GL_MAILBOX_SIZE_CHROMIUM];
   5933   group().mailbox_manager()->GenerateMailboxName(
   5934       reinterpret_cast<MailboxName*>(mailbox));
   5935 
   5936   memcpy(shared_memory_address_, mailbox, sizeof(mailbox));
   5937 
   5938   EXPECT_EQ(kServiceTextureId, texture_ref->service_id());
   5939 
   5940   ProduceTextureCHROMIUM produce_cmd;
   5941   produce_cmd.Init(
   5942       GL_TEXTURE_EXTERNAL_OES, kSharedMemoryId, kSharedMemoryOffset);
   5943   EXPECT_EQ(error::kNoError, ExecuteCmd(produce_cmd));
   5944   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   5945 
   5946   // Create new texture for consume.
   5947   EXPECT_CALL(*gl_, GenTextures(_, _))
   5948       .WillOnce(SetArgumentPointee<1>(kNewServiceId))
   5949       .RetiresOnSaturation();
   5950   DoBindTexture(GL_TEXTURE_EXTERNAL_OES, kNewClientId, kNewServiceId);
   5951 
   5952   // Assigns and binds original service size texture ID.
   5953   EXPECT_CALL(*gl_, DeleteTextures(1, _))
   5954       .Times(1)
   5955       .RetiresOnSaturation();
   5956   EXPECT_CALL(*gl_, BindTexture(GL_TEXTURE_EXTERNAL_OES, kServiceTextureId))
   5957       .Times(1)
   5958       .RetiresOnSaturation();
   5959 
   5960   // Shared mem got clobbered from GetError() above.
   5961   memcpy(shared_memory_address_, mailbox, sizeof(mailbox));
   5962   ConsumeTextureCHROMIUM consume_cmd;
   5963   consume_cmd.Init(
   5964       GL_TEXTURE_EXTERNAL_OES, kSharedMemoryId, kSharedMemoryOffset);
   5965   EXPECT_EQ(error::kNoError, ExecuteCmd(consume_cmd));
   5966   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   5967 
   5968   // Service ID is restored.
   5969   EXPECT_EQ(kServiceTextureId, texture_ref->service_id());
   5970 
   5971   EXPECT_CALL(*stream_texture_manager(),
   5972               DestroyStreamTexture(kServiceTextureId))
   5973       .Times(1)
   5974       .RetiresOnSaturation();
   5975 }
   5976 
   5977 TEST_F(GLES2DecoderManualInitTest, ARBTextureRectangleBindTexture) {
   5978   InitDecoder(
   5979       "GL_ARB_texture_rectangle",  // extensions
   5980       false,   // has alpha
   5981       false,   // has depth
   5982       false,   // has stencil
   5983       false,   // request alpha
   5984       false,   // request depth
   5985       false,   // request stencil
   5986       true);   // bind generates resource
   5987   EXPECT_CALL(*gl_, BindTexture(GL_TEXTURE_RECTANGLE_ARB, kNewServiceId));
   5988   EXPECT_CALL(*gl_, GenTextures(1, _))
   5989      .WillOnce(SetArgumentPointee<1>(kNewServiceId));
   5990   BindTexture cmd;
   5991   cmd.Init(GL_TEXTURE_RECTANGLE_ARB, kNewClientId);
   5992   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   5993   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   5994   Texture* texture = GetTexture(kNewClientId)->texture();
   5995   EXPECT_TRUE(texture != NULL);
   5996   EXPECT_TRUE(texture->target() == GL_TEXTURE_RECTANGLE_ARB);
   5997 }
   5998 
   5999 TEST_F(GLES2DecoderManualInitTest, ARBTextureRectangleGetBinding) {
   6000   InitDecoder(
   6001       "GL_ARB_texture_rectangle",  // extensions
   6002       false,   // has alpha
   6003       false,   // has depth
   6004       false,   // has stencil
   6005       false,   // request alpha
   6006       false,   // request depth
   6007       false,   // request stencil
   6008       true);   // bind generates resource
   6009   DoBindTexture(
   6010       GL_TEXTURE_RECTANGLE_ARB, client_texture_id_, kServiceTextureId);
   6011 
   6012   EXPECT_CALL(*gl_, GetError())
   6013       .WillOnce(Return(GL_NO_ERROR))
   6014       .WillOnce(Return(GL_NO_ERROR))
   6015       .RetiresOnSaturation();
   6016   typedef GetIntegerv::Result Result;
   6017   Result* result = static_cast<Result*>(shared_memory_address_);
   6018   EXPECT_CALL(*gl_, GetIntegerv(GL_TEXTURE_BINDING_RECTANGLE_ARB,
   6019                                 result->GetData()))
   6020       .Times(0);
   6021   result->size = 0;
   6022   GetIntegerv cmd;
   6023   cmd.Init(GL_TEXTURE_BINDING_RECTANGLE_ARB,
   6024            shared_memory_id_,
   6025            shared_memory_offset_);
   6026   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6027   EXPECT_EQ(decoder_->GetGLES2Util()->GLGetNumValuesReturned(
   6028       GL_TEXTURE_BINDING_RECTANGLE_ARB), result->GetNumResults());
   6029   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   6030   EXPECT_EQ(client_texture_id_, (uint32)result->GetData()[0]);
   6031 }
   6032 
   6033 TEST_F(GLES2DecoderManualInitTest, ARBTextureRectangleTextureDefaults) {
   6034   InitDecoder(
   6035       "GL_ARB_texture_rectangle",  // extensions
   6036       false,   // has alpha
   6037       false,   // has depth
   6038       false,   // has stencil
   6039       false,   // request alpha
   6040       false,   // request depth
   6041       false,   // request stencil
   6042       true);   // bind generates resource
   6043   DoBindTexture(
   6044       GL_TEXTURE_RECTANGLE_ARB, client_texture_id_, kServiceTextureId);
   6045 
   6046   Texture* texture = GetTexture(client_texture_id_)->texture();
   6047   EXPECT_TRUE(texture != NULL);
   6048   EXPECT_TRUE(texture->target() == GL_TEXTURE_RECTANGLE_ARB);
   6049   EXPECT_TRUE(texture->min_filter() == GL_LINEAR);
   6050   EXPECT_TRUE(texture->wrap_s() == GL_CLAMP_TO_EDGE);
   6051   EXPECT_TRUE(texture->wrap_t() == GL_CLAMP_TO_EDGE);
   6052 }
   6053 
   6054 TEST_F(GLES2DecoderManualInitTest, ARBTextureRectangleTextureParam) {
   6055   InitDecoder(
   6056       "GL_ARB_texture_rectangle",  // extensions
   6057       false,   // has alpha
   6058       false,   // has depth
   6059       false,   // has stencil
   6060       false,   // request alpha
   6061       false,   // request depth
   6062       false,   // request stencil
   6063       true);   // bind generates resource
   6064 
   6065   DoBindTexture(
   6066       GL_TEXTURE_RECTANGLE_ARB, client_texture_id_, kServiceTextureId);
   6067 
   6068   EXPECT_CALL(*gl_, TexParameteri(GL_TEXTURE_RECTANGLE_ARB,
   6069                                   GL_TEXTURE_MIN_FILTER,
   6070                                   GL_NEAREST));
   6071   EXPECT_CALL(*gl_, TexParameteri(GL_TEXTURE_RECTANGLE_ARB,
   6072                                   GL_TEXTURE_MIN_FILTER,
   6073                                   GL_LINEAR));
   6074   EXPECT_CALL(*gl_, TexParameteri(GL_TEXTURE_RECTANGLE_ARB,
   6075                                   GL_TEXTURE_WRAP_S,
   6076                                   GL_CLAMP_TO_EDGE));
   6077   EXPECT_CALL(*gl_, TexParameteri(GL_TEXTURE_RECTANGLE_ARB,
   6078                                   GL_TEXTURE_WRAP_T,
   6079                                   GL_CLAMP_TO_EDGE));
   6080   TexParameteri cmd;
   6081   cmd.Init(GL_TEXTURE_RECTANGLE_ARB,
   6082            GL_TEXTURE_MIN_FILTER,
   6083            GL_NEAREST);
   6084   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6085   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   6086 
   6087   cmd.Init(GL_TEXTURE_RECTANGLE_ARB,
   6088            GL_TEXTURE_MIN_FILTER,
   6089            GL_LINEAR);
   6090   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6091   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   6092 
   6093   cmd.Init(GL_TEXTURE_RECTANGLE_ARB,
   6094            GL_TEXTURE_WRAP_S,
   6095            GL_CLAMP_TO_EDGE);
   6096   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6097   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   6098 
   6099   cmd.Init(GL_TEXTURE_RECTANGLE_ARB,
   6100            GL_TEXTURE_WRAP_T,
   6101            GL_CLAMP_TO_EDGE);
   6102   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6103   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   6104 
   6105   Texture* texture = GetTexture(client_texture_id_)->texture();
   6106   EXPECT_TRUE(texture != NULL);
   6107   EXPECT_TRUE(texture->target() == GL_TEXTURE_RECTANGLE_ARB);
   6108   EXPECT_TRUE(texture->min_filter() == GL_LINEAR);
   6109   EXPECT_TRUE(texture->wrap_s() == GL_CLAMP_TO_EDGE);
   6110   EXPECT_TRUE(texture->wrap_t() == GL_CLAMP_TO_EDGE);
   6111 }
   6112 
   6113 TEST_F(GLES2DecoderManualInitTest, ARBTextureRectangleTextureParamInvalid) {
   6114   InitDecoder(
   6115       "GL_ARB_texture_rectangle",  // extensions
   6116       false,   // has alpha
   6117       false,   // has depth
   6118       false,   // has stencil
   6119       false,   // request alpha
   6120       false,   // request depth
   6121       false,   // request stencil
   6122       true);   // bind generates resource
   6123 
   6124   DoBindTexture(
   6125       GL_TEXTURE_RECTANGLE_ARB, client_texture_id_, kServiceTextureId);
   6126 
   6127   TexParameteri cmd;
   6128   cmd.Init(GL_TEXTURE_RECTANGLE_ARB,
   6129            GL_TEXTURE_MIN_FILTER,
   6130            GL_NEAREST_MIPMAP_NEAREST);
   6131   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6132   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
   6133 
   6134   cmd.Init(GL_TEXTURE_RECTANGLE_ARB,
   6135            GL_TEXTURE_WRAP_S,
   6136            GL_REPEAT);
   6137   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6138   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
   6139 
   6140   cmd.Init(GL_TEXTURE_RECTANGLE_ARB,
   6141            GL_TEXTURE_WRAP_T,
   6142            GL_REPEAT);
   6143   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6144   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
   6145 
   6146   Texture* texture = GetTexture(client_texture_id_)->texture();
   6147   EXPECT_TRUE(texture != NULL);
   6148   EXPECT_TRUE(texture->target() == GL_TEXTURE_RECTANGLE_ARB);
   6149   EXPECT_TRUE(texture->min_filter() == GL_LINEAR);
   6150   EXPECT_TRUE(texture->wrap_s() == GL_CLAMP_TO_EDGE);
   6151   EXPECT_TRUE(texture->wrap_t() == GL_CLAMP_TO_EDGE);
   6152 }
   6153 
   6154 TEST_F(GLES2DecoderManualInitTest, ARBTextureRectangleTexImage2DError) {
   6155   InitDecoder(
   6156       "GL_ARB_texture_rectangle",  // extensions
   6157       false,   // has alpha
   6158       false,   // has depth
   6159       false,   // has stencil
   6160       false,   // request alpha
   6161       false,   // request depth
   6162       false,   // request stencil
   6163       true);   // bind generates resource
   6164 
   6165   GLenum target = GL_TEXTURE_RECTANGLE_ARB;
   6166   GLint level = 0;
   6167   GLenum internal_format = GL_RGBA;
   6168   GLsizei width = 2;
   6169   GLsizei height = 4;
   6170   GLint border = 0;
   6171   GLenum format = GL_RGBA;
   6172   GLenum type = GL_UNSIGNED_BYTE;
   6173   DoBindTexture(
   6174       GL_TEXTURE_RECTANGLE_ARB, client_texture_id_, kServiceTextureId);
   6175   ASSERT_TRUE(GetTexture(client_texture_id_) != NULL);
   6176   TexImage2D cmd;
   6177   cmd.Init(target, level, internal_format, width, height, border, format,
   6178            type, kSharedMemoryId, kSharedMemoryOffset);
   6179   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6180 
   6181   // TexImage2D is not allowed with GL_TEXTURE_RECTANGLE_ARB targets.
   6182   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
   6183 }
   6184 
   6185 TEST_F(GLES2DecoderTest, EnableFeatureCHROMIUMBadBucket) {
   6186   const uint32 kBadBucketId = 123;
   6187   EnableFeatureCHROMIUM cmd;
   6188   cmd.Init(kBadBucketId, shared_memory_id_, shared_memory_offset_);
   6189   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   6190 }
   6191 
   6192 TEST_F(GLES2DecoderTest, RequestExtensionCHROMIUMBadBucket) {
   6193   const uint32 kBadBucketId = 123;
   6194   RequestExtensionCHROMIUM cmd;
   6195   cmd.Init(kBadBucketId);
   6196   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   6197 }
   6198 
   6199 TEST_F(GLES2DecoderTest, TexSubImage2DClearsAfterTexImage2DNULL) {
   6200   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   6201   DoTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   6202                0, 0);
   6203   SetupClearTextureExpections(
   6204       kServiceTextureId, kServiceTextureId, GL_TEXTURE_2D, GL_TEXTURE_2D,
   6205       0, GL_RGBA, GL_UNSIGNED_BYTE, 2, 2);
   6206   EXPECT_CALL(*gl_, TexSubImage2D(
   6207       GL_TEXTURE_2D, 0, 1, 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,
   6208       shared_memory_address_))
   6209       .Times(1)
   6210       .RetiresOnSaturation();
   6211   TexSubImage2D cmd;
   6212   cmd.Init(
   6213       GL_TEXTURE_2D, 0, 1, 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,
   6214       kSharedMemoryId, kSharedMemoryOffset, GL_FALSE);
   6215   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6216   // Test if we call it again it does not clear.
   6217   EXPECT_CALL(*gl_, TexSubImage2D(
   6218       GL_TEXTURE_2D, 0, 1, 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,
   6219       shared_memory_address_))
   6220       .Times(1)
   6221       .RetiresOnSaturation();
   6222   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6223 }
   6224 
   6225 TEST_F(GLES2DecoderTest, TexSubImage2DDoesNotClearAfterTexImage2DNULLThenData) {
   6226   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   6227   DoTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   6228                0, 0);
   6229   DoTexImage2D(
   6230       GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   6231       kSharedMemoryId, kSharedMemoryOffset);
   6232   EXPECT_CALL(*gl_, TexSubImage2D(
   6233       GL_TEXTURE_2D, 0, 1, 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,
   6234       shared_memory_address_))
   6235       .Times(1)
   6236       .RetiresOnSaturation();
   6237   TexSubImage2D cmd;
   6238   cmd.Init(
   6239       GL_TEXTURE_2D, 0, 1, 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,
   6240       kSharedMemoryId, kSharedMemoryOffset, GL_FALSE);
   6241   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6242   // Test if we call it again it does not clear.
   6243   EXPECT_CALL(*gl_, TexSubImage2D(
   6244       GL_TEXTURE_2D, 0, 1, 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,
   6245       shared_memory_address_))
   6246       .Times(1)
   6247       .RetiresOnSaturation();
   6248   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6249 }
   6250 
   6251 TEST_F(
   6252     GLES2DecoderManualInitTest,
   6253     TexSubImage2DDoesNotClearAfterTexImage2DNULLThenDataWithTexImage2DIsFaster) {
   6254   CommandLine command_line(0, NULL);
   6255   command_line.AppendSwitchASCII(
   6256       switches::kGpuDriverBugWorkarounds,
   6257       base::IntToString(gpu::TEXSUBIMAGE2D_FASTER_THAN_TEXIMAGE2D));
   6258   InitDecoderWithCommandLine(
   6259       "",     // extensions
   6260       false,  // has alpha
   6261       false,  // has depth
   6262       false,  // has stencil
   6263       false,  // request alpha
   6264       false,  // request depth
   6265       false,  // request stencil
   6266       true,   // bind generates resource
   6267       &command_line);
   6268   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   6269   DoTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   6270                0, 0);
   6271 
   6272   {
   6273     // Uses texSubimage internally because the above workaround is active and
   6274     // the update is for the full size of the texture.
   6275     EXPECT_CALL(*gl_,
   6276                 TexSubImage2D(
   6277                     GL_TEXTURE_2D, 0, 0, 0, 2, 2, GL_RGBA, GL_UNSIGNED_BYTE, _))
   6278         .Times(1)
   6279         .RetiresOnSaturation();
   6280     cmds::TexImage2D cmd;
   6281     cmd.Init(GL_TEXTURE_2D,
   6282              0,
   6283              GL_RGBA,
   6284              2,
   6285              2,
   6286              0,
   6287              GL_RGBA,
   6288              GL_UNSIGNED_BYTE,
   6289              kSharedMemoryId,
   6290              kSharedMemoryOffset);
   6291     EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6292   }
   6293 
   6294   EXPECT_CALL(*gl_, TexSubImage2D(
   6295       GL_TEXTURE_2D, 0, 1, 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,
   6296       shared_memory_address_))
   6297       .Times(1)
   6298       .RetiresOnSaturation();
   6299   TexSubImage2D cmd;
   6300   cmd.Init(
   6301       GL_TEXTURE_2D, 0, 1, 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,
   6302       kSharedMemoryId, kSharedMemoryOffset, GL_FALSE);
   6303   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6304   // Test if we call it again it does not clear.
   6305   EXPECT_CALL(*gl_, TexSubImage2D(
   6306       GL_TEXTURE_2D, 0, 1, 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,
   6307       shared_memory_address_))
   6308       .Times(1)
   6309       .RetiresOnSaturation();
   6310   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6311 }
   6312 
   6313 TEST_F(GLES2DecoderTest, TexSubImage2DClearsAfterTexImage2DWithDataThenNULL) {
   6314   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   6315   // Put in data (so it should be marked as cleared)
   6316   DoTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   6317                kSharedMemoryId, kSharedMemoryOffset);
   6318   // Put in no data.
   6319   TexImage2D tex_cmd;
   6320   tex_cmd.Init(
   6321       GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0, 0);
   6322   // It won't actually call TexImage2D, just mark it as uncleared.
   6323   EXPECT_EQ(error::kNoError, ExecuteCmd(tex_cmd));
   6324   // Next call to TexSubImage2d should clear.
   6325   SetupClearTextureExpections(
   6326       kServiceTextureId, kServiceTextureId, GL_TEXTURE_2D, GL_TEXTURE_2D,
   6327       0, GL_RGBA, GL_UNSIGNED_BYTE, 2, 2);
   6328   EXPECT_CALL(*gl_, TexSubImage2D(
   6329       GL_TEXTURE_2D, 0, 1, 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,
   6330       shared_memory_address_))
   6331       .Times(1)
   6332       .RetiresOnSaturation();
   6333   TexSubImage2D cmd;
   6334   cmd.Init(
   6335       GL_TEXTURE_2D, 0, 1, 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,
   6336       kSharedMemoryId, kSharedMemoryOffset, GL_FALSE);
   6337   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6338 }
   6339 
   6340 TEST_F(GLES2DecoderWithShaderTest, DrawArraysClearsAfterTexImage2DNULL) {
   6341   SetupAllNeededVertexBuffers();
   6342   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   6343   // Create an uncleared texture with 2 levels.
   6344   DoTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   6345                0, 0);
   6346   DoTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   6347                0, 0);
   6348   // Expect 2 levels will be cleared.
   6349   SetupClearTextureExpections(
   6350       kServiceTextureId, kServiceTextureId, GL_TEXTURE_2D, GL_TEXTURE_2D,
   6351       0, GL_RGBA, GL_UNSIGNED_BYTE, 2, 2);
   6352   SetupClearTextureExpections(
   6353       kServiceTextureId, kServiceTextureId, GL_TEXTURE_2D, GL_TEXTURE_2D,
   6354       1, GL_RGBA, GL_UNSIGNED_BYTE, 1, 1);
   6355   SetupExpectationsForApplyingDefaultDirtyState();
   6356   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   6357       .Times(1)
   6358       .RetiresOnSaturation();
   6359   DrawArrays cmd;
   6360   cmd.Init(GL_TRIANGLES, 0, kNumVertices);
   6361   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6362   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   6363 
   6364   // But not again
   6365   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   6366       .Times(1)
   6367       .RetiresOnSaturation();
   6368   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6369   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   6370 }
   6371 
   6372 TEST_F(GLES2DecoderWithShaderTest, DrawElementsClearsAfterTexImage2DNULL) {
   6373   SetupAllNeededVertexBuffers();
   6374   SetupIndexBuffer();
   6375   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   6376   // Create an uncleared texture with 2 levels.
   6377   DoTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   6378                0, 0);
   6379   DoTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   6380                0, 0);
   6381   // Expect 2 levels will be cleared.
   6382   SetupClearTextureExpections(
   6383       kServiceTextureId, kServiceTextureId, GL_TEXTURE_2D, GL_TEXTURE_2D,
   6384       0, GL_RGBA, GL_UNSIGNED_BYTE, 2, 2);
   6385   SetupClearTextureExpections(
   6386       kServiceTextureId, kServiceTextureId, GL_TEXTURE_2D, GL_TEXTURE_2D,
   6387       1, GL_RGBA, GL_UNSIGNED_BYTE, 1, 1);
   6388   SetupExpectationsForApplyingDefaultDirtyState();
   6389 
   6390   EXPECT_CALL(*gl_, DrawElements(GL_TRIANGLES, kValidIndexRangeCount,
   6391                                  GL_UNSIGNED_SHORT,
   6392                                  BufferOffset(kValidIndexRangeStart * 2)))
   6393       .Times(1)
   6394       .RetiresOnSaturation();
   6395   DrawElements cmd;
   6396   cmd.Init(GL_TRIANGLES, kValidIndexRangeCount, GL_UNSIGNED_SHORT,
   6397            kValidIndexRangeStart * 2);
   6398   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6399   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   6400 
   6401   // But not again
   6402   EXPECT_CALL(*gl_, DrawElements(GL_TRIANGLES, kValidIndexRangeCount,
   6403                                  GL_UNSIGNED_SHORT,
   6404                                  BufferOffset(kValidIndexRangeStart * 2)))
   6405       .Times(1)
   6406       .RetiresOnSaturation();
   6407   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6408   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   6409 }
   6410 
   6411 TEST_F(GLES2DecoderWithShaderTest, DrawClearsAfterTexImage2DNULLInFBO) {
   6412   const GLuint kFBOClientTextureId = 4100;
   6413   const GLuint kFBOServiceTextureId = 4101;
   6414 
   6415   SetupAllNeededVertexBuffers();
   6416   // Register a texture id.
   6417   EXPECT_CALL(*gl_, GenTextures(_, _))
   6418       .WillOnce(SetArgumentPointee<1>(kFBOServiceTextureId))
   6419       .RetiresOnSaturation();
   6420   GenHelper<GenTexturesImmediate>(kFBOClientTextureId);
   6421 
   6422   // Setup "render to" texture.
   6423   DoBindTexture(GL_TEXTURE_2D, kFBOClientTextureId, kFBOServiceTextureId);
   6424   DoTexImage2D(
   6425       GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0, 0);
   6426   DoBindFramebuffer(
   6427       GL_FRAMEBUFFER, client_framebuffer_id_, kServiceFramebufferId);
   6428   DoFramebufferTexture2D(
   6429       GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
   6430       kFBOClientTextureId, kFBOServiceTextureId, 0, GL_NO_ERROR);
   6431 
   6432   // Setup "render from" texture.
   6433   SetupTexture();
   6434 
   6435   SetupExpectationsForFramebufferClearing(
   6436       GL_FRAMEBUFFER,         // target
   6437       GL_COLOR_BUFFER_BIT,    // clear bits
   6438       0, 0, 0, 0,             // color
   6439       0,                      // stencil
   6440       1.0f,                   // depth
   6441       false);                 // scissor test
   6442 
   6443   SetupExpectationsForApplyingDirtyState(
   6444       false,   // Framebuffer is RGB
   6445       false,   // Framebuffer has depth
   6446       false,   // Framebuffer has stencil
   6447       0x1111,  // color bits
   6448       false,   // depth mask
   6449       false,   // depth enabled
   6450       0,       // front stencil mask
   6451       0,       // back stencil mask
   6452       false,   // stencil enabled
   6453       false,   // cull_face_enabled
   6454       false,   // scissor_test_enabled
   6455       false);  // blend_enabled
   6456 
   6457   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   6458       .Times(1)
   6459       .RetiresOnSaturation();
   6460   DrawArrays cmd;
   6461   cmd.Init(GL_TRIANGLES, 0, kNumVertices);
   6462   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6463   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   6464 
   6465   // But not again.
   6466   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   6467       .Times(1)
   6468       .RetiresOnSaturation();
   6469   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6470   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   6471 }
   6472 
   6473 TEST_F(GLES2DecoderWithShaderTest, DrawWitFBOThatCantClearDoesNotDraw) {
   6474   const GLuint kFBOClientTextureId = 4100;
   6475   const GLuint kFBOServiceTextureId = 4101;
   6476 
   6477   // Register a texture id.
   6478   EXPECT_CALL(*gl_, GenTextures(_, _))
   6479       .WillOnce(SetArgumentPointee<1>(kFBOServiceTextureId))
   6480       .RetiresOnSaturation();
   6481   GenHelper<GenTexturesImmediate>(kFBOClientTextureId);
   6482 
   6483   // Setup "render to" texture.
   6484   DoBindTexture(GL_TEXTURE_2D, kFBOClientTextureId, kFBOServiceTextureId);
   6485   DoTexImage2D(
   6486       GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0, 0);
   6487   DoBindFramebuffer(
   6488       GL_FRAMEBUFFER, client_framebuffer_id_, kServiceFramebufferId);
   6489   DoFramebufferTexture2D(
   6490       GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
   6491       kFBOClientTextureId, kFBOServiceTextureId, 0, GL_NO_ERROR);
   6492 
   6493   // Setup "render from" texture.
   6494   SetupTexture();
   6495 
   6496   EXPECT_CALL(*gl_, CheckFramebufferStatusEXT(GL_FRAMEBUFFER))
   6497       .WillOnce(Return(GL_FRAMEBUFFER_UNSUPPORTED))
   6498       .RetiresOnSaturation();
   6499   EXPECT_CALL(*gl_, DrawArrays(_, _, _))
   6500       .Times(0)
   6501       .RetiresOnSaturation();
   6502   DrawArrays cmd;
   6503   cmd.Init(GL_TRIANGLES, 0, kNumVertices);
   6504   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6505   EXPECT_EQ(GL_INVALID_FRAMEBUFFER_OPERATION, GetGLError());
   6506 }
   6507 
   6508 TEST_F(GLES2DecoderTest, CopyTexImage2DMarksTextureAsCleared) {
   6509   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   6510 
   6511   TextureManager* manager = group().texture_manager();
   6512   TextureRef* texture_ref = manager->GetTexture(client_texture_id_);
   6513   ASSERT_TRUE(texture_ref != NULL);
   6514   Texture* texture = texture_ref->texture();
   6515 
   6516   EXPECT_CALL(*gl_, GetError())
   6517       .WillOnce(Return(GL_NO_ERROR))
   6518       .RetiresOnSaturation();
   6519   EXPECT_CALL(*gl_, CopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, 1, 1, 0))
   6520       .Times(1)
   6521       .RetiresOnSaturation();
   6522   EXPECT_CALL(*gl_, GetError())
   6523       .WillOnce(Return(GL_NO_ERROR))
   6524       .RetiresOnSaturation();
   6525   CopyTexImage2D cmd;
   6526   cmd.Init(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, 1, 1, 0);
   6527   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6528 
   6529   EXPECT_TRUE(texture->SafeToRenderFrom());
   6530 }
   6531 
   6532 TEST_F(GLES2DecoderTest, CopyTexSubImage2DClearsUnclearedTexture) {
   6533   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   6534   DoTexImage2D(
   6535       GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0, 0);
   6536 
   6537   SetupClearTextureExpections(
   6538       kServiceTextureId, kServiceTextureId, GL_TEXTURE_2D, GL_TEXTURE_2D,
   6539       0, GL_RGBA, GL_UNSIGNED_BYTE, 2, 2);
   6540   EXPECT_CALL(*gl_, CopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, 1, 1))
   6541       .Times(1)
   6542       .RetiresOnSaturation();
   6543   CopyTexSubImage2D cmd;
   6544   cmd.Init(GL_TEXTURE_2D, 0, 0, 0, 0, 0, 1, 1);
   6545   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6546 }
   6547 
   6548 TEST_F(GLES2DecoderManualInitTest, CompressedImage2DMarksTextureAsCleared) {
   6549   InitDecoder(
   6550       "GL_EXT_texture_compression_s3tc",  // extensions
   6551       false,   // has alpha
   6552       false,   // has depth
   6553       false,   // has stencil
   6554       false,   // request alpha
   6555       false,   // request depth
   6556       false,   // request stencil
   6557       true);   // bind generates resource
   6558 
   6559   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   6560   EXPECT_CALL(*gl_, GetError())
   6561       .WillOnce(Return(GL_NO_ERROR))
   6562       .RetiresOnSaturation();
   6563   EXPECT_CALL(*gl_, CompressedTexImage2D(
   6564       GL_TEXTURE_2D, 0, GL_COMPRESSED_RGB_S3TC_DXT1_EXT, 4, 4, 0, 8, _))
   6565       .Times(1)
   6566       .RetiresOnSaturation();
   6567   EXPECT_CALL(*gl_, GetError())
   6568       .WillOnce(Return(GL_NO_ERROR))
   6569       .RetiresOnSaturation();
   6570   CompressedTexImage2D cmd;
   6571   cmd.Init(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGB_S3TC_DXT1_EXT, 4, 4, 0,
   6572            8, kSharedMemoryId, kSharedMemoryOffset);
   6573   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6574   TextureManager* manager = group().texture_manager();
   6575   TextureRef* texture_ref = manager->GetTexture(client_texture_id_);
   6576   EXPECT_TRUE(texture_ref->texture()->SafeToRenderFrom());
   6577 }
   6578 
   6579 TEST_F(GLES2DecoderWithShaderTest, UnClearedAttachmentsGetClearedOnClear) {
   6580   const GLuint kFBOClientTextureId = 4100;
   6581   const GLuint kFBOServiceTextureId = 4101;
   6582 
   6583   // Register a texture id.
   6584   EXPECT_CALL(*gl_, GenTextures(_, _))
   6585       .WillOnce(SetArgumentPointee<1>(kFBOServiceTextureId))
   6586       .RetiresOnSaturation();
   6587   GenHelper<GenTexturesImmediate>(kFBOClientTextureId);
   6588 
   6589   // Setup "render to" texture.
   6590   DoBindTexture(GL_TEXTURE_2D, kFBOClientTextureId, kFBOServiceTextureId);
   6591   DoTexImage2D(
   6592       GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0, 0);
   6593   DoBindFramebuffer(
   6594       GL_FRAMEBUFFER, client_framebuffer_id_, kServiceFramebufferId);
   6595   DoFramebufferTexture2D(
   6596       GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
   6597       kFBOClientTextureId, kFBOServiceTextureId, 0, GL_NO_ERROR);
   6598 
   6599   // Setup "render from" texture.
   6600   SetupTexture();
   6601 
   6602   SetupExpectationsForFramebufferClearing(
   6603       GL_FRAMEBUFFER,         // target
   6604       GL_COLOR_BUFFER_BIT,    // clear bits
   6605       0, 0, 0, 0,             // color
   6606       0,                      // stencil
   6607       1.0f,                   // depth
   6608       false);                 // scissor test
   6609   SetupExpectationsForApplyingDirtyState(
   6610       false,   // Framebuffer is RGB
   6611       false,   // Framebuffer has depth
   6612       false,   // Framebuffer has stencil
   6613       0x1111,  // color bits
   6614       false,   // depth mask
   6615       false,   // depth enabled
   6616       0,       // front stencil mask
   6617       0,       // back stencil mask
   6618       false,   // stencil enabled
   6619       false,   // cull_face_enabled
   6620       false,   // scissor_test_enabled
   6621       false);  // blend_enabled
   6622 
   6623   EXPECT_CALL(*gl_, Clear(GL_COLOR_BUFFER_BIT))
   6624       .Times(1)
   6625       .RetiresOnSaturation();
   6626 
   6627   Clear cmd;
   6628   cmd.Init(GL_COLOR_BUFFER_BIT);
   6629   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6630   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   6631 }
   6632 
   6633 TEST_F(GLES2DecoderWithShaderTest, UnClearedAttachmentsGetClearedOnReadPixels) {
   6634   const GLuint kFBOClientTextureId = 4100;
   6635   const GLuint kFBOServiceTextureId = 4101;
   6636 
   6637   // Register a texture id.
   6638   EXPECT_CALL(*gl_, GenTextures(_, _))
   6639       .WillOnce(SetArgumentPointee<1>(kFBOServiceTextureId))
   6640       .RetiresOnSaturation();
   6641   GenHelper<GenTexturesImmediate>(kFBOClientTextureId);
   6642 
   6643   // Setup "render to" texture.
   6644   DoBindTexture(GL_TEXTURE_2D, kFBOClientTextureId, kFBOServiceTextureId);
   6645   DoTexImage2D(
   6646       GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0, 0);
   6647   DoBindFramebuffer(
   6648       GL_FRAMEBUFFER, client_framebuffer_id_, kServiceFramebufferId);
   6649   DoFramebufferTexture2D(
   6650       GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
   6651       kFBOClientTextureId, kFBOServiceTextureId, 0, GL_NO_ERROR);
   6652 
   6653   // Setup "render from" texture.
   6654   SetupTexture();
   6655 
   6656   SetupExpectationsForFramebufferClearing(
   6657       GL_FRAMEBUFFER,         // target
   6658       GL_COLOR_BUFFER_BIT,    // clear bits
   6659       0, 0, 0, 0,             // color
   6660       0,                      // stencil
   6661       1.0f,                   // depth
   6662       false);                 // scissor test
   6663 
   6664   EXPECT_CALL(*gl_, GetError())
   6665      .WillOnce(Return(GL_NO_ERROR))
   6666      .WillOnce(Return(GL_NO_ERROR))
   6667      .RetiresOnSaturation();
   6668   EXPECT_CALL(*gl_, ReadPixels(0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, _))
   6669       .Times(1)
   6670       .RetiresOnSaturation();
   6671   typedef ReadPixels::Result Result;
   6672   Result* result = GetSharedMemoryAs<Result*>();
   6673   uint32 result_shm_id = kSharedMemoryId;
   6674   uint32 result_shm_offset = kSharedMemoryOffset;
   6675   uint32 pixels_shm_id = kSharedMemoryId;
   6676   uint32 pixels_shm_offset = kSharedMemoryOffset + sizeof(*result);
   6677   ReadPixels cmd;
   6678   cmd.Init(0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,
   6679            pixels_shm_id, pixels_shm_offset,
   6680            result_shm_id, result_shm_offset,
   6681            false);
   6682   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6683   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   6684 }
   6685 
   6686 TEST_F(GLES2DecoderManualInitTest,
   6687        UnClearedAttachmentsGetClearedOnReadPixelsAndDrawBufferGetsRestored) {
   6688   InitDecoder(
   6689       "GL_EXT_framebuffer_multisample",  // extensions
   6690       false,   // has alpha
   6691       false,   // has depth
   6692       false,   // has stencil
   6693       false,   // request alpha
   6694       false,   // request depth
   6695       false,   // request stencil
   6696       true);   // bind generates resource
   6697   const GLuint kFBOClientTextureId = 4100;
   6698   const GLuint kFBOServiceTextureId = 4101;
   6699 
   6700   // Register a texture id.
   6701   EXPECT_CALL(*gl_, GenTextures(_, _))
   6702       .WillOnce(SetArgumentPointee<1>(kFBOServiceTextureId))
   6703       .RetiresOnSaturation();
   6704   GenHelper<GenTexturesImmediate>(kFBOClientTextureId);
   6705 
   6706   // Setup "render from" texture.
   6707   DoBindTexture(GL_TEXTURE_2D, kFBOClientTextureId, kFBOServiceTextureId);
   6708   DoTexImage2D(
   6709       GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0, 0);
   6710   DoBindFramebuffer(
   6711       GL_READ_FRAMEBUFFER, client_framebuffer_id_, kServiceFramebufferId);
   6712   DoFramebufferTexture2D(
   6713       GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
   6714       kFBOClientTextureId, kFBOServiceTextureId, 0, GL_NO_ERROR);
   6715 
   6716   SetupExpectationsForFramebufferClearingMulti(
   6717       kServiceFramebufferId,  // read framebuffer service id
   6718       0,                      // backbuffer service id
   6719       GL_READ_FRAMEBUFFER,    // target
   6720       GL_COLOR_BUFFER_BIT,    // clear bits
   6721       0, 0, 0, 0,             // color
   6722       0,                      // stencil
   6723       1.0f,                   // depth
   6724       false);                 // scissor test
   6725 
   6726   EXPECT_CALL(*gl_, GetError())
   6727      .WillOnce(Return(GL_NO_ERROR))
   6728      .WillOnce(Return(GL_NO_ERROR))
   6729      .RetiresOnSaturation();
   6730   EXPECT_CALL(*gl_, ReadPixels(0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, _))
   6731       .Times(1)
   6732       .RetiresOnSaturation();
   6733   typedef ReadPixels::Result Result;
   6734   uint32 result_shm_id = kSharedMemoryId;
   6735   uint32 result_shm_offset = kSharedMemoryOffset;
   6736   uint32 pixels_shm_id = kSharedMemoryId;
   6737   uint32 pixels_shm_offset = kSharedMemoryOffset + sizeof(Result);
   6738   ReadPixels cmd;
   6739   cmd.Init(0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,
   6740            pixels_shm_id, pixels_shm_offset,
   6741            result_shm_id, result_shm_offset,
   6742            false);
   6743   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6744   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   6745 }
   6746 
   6747 TEST_F(GLES2DecoderWithShaderTest, DrawClearsAfterRenderbufferStorageInFBO) {
   6748   SetupTexture();
   6749   DoBindRenderbuffer(GL_RENDERBUFFER, client_renderbuffer_id_,
   6750                     kServiceRenderbufferId);
   6751   DoBindFramebuffer(GL_FRAMEBUFFER, client_framebuffer_id_,
   6752                     kServiceFramebufferId);
   6753   DoRenderbufferStorage(
   6754       GL_RENDERBUFFER, GL_RGBA4, GL_RGBA, 100, 50, GL_NO_ERROR);
   6755   DoFramebufferRenderbuffer(
   6756       GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER,
   6757       client_renderbuffer_id_, kServiceRenderbufferId, GL_NO_ERROR);
   6758 
   6759   SetupExpectationsForFramebufferClearing(
   6760       GL_FRAMEBUFFER,         // target
   6761       GL_COLOR_BUFFER_BIT,    // clear bits
   6762       0, 0, 0, 0,             // color
   6763       0,                      // stencil
   6764       1.0f,                   // depth
   6765       false);                 // scissor test
   6766 
   6767   AddExpectationsForSimulatedAttrib0(kNumVertices, 0);
   6768   SetupExpectationsForApplyingDirtyState(
   6769       false,   // Framebuffer is RGB
   6770       false,   // Framebuffer has depth
   6771       false,   // Framebuffer has stencil
   6772       0x1111,  // color bits
   6773       false,   // depth mask
   6774       false,   // depth enabled
   6775       0,       // front stencil mask
   6776       0,       // back stencil mask
   6777       false,   // stencil enabled
   6778       false,   // cull_face_enabled
   6779       false,   // scissor_test_enabled
   6780       false);  // blend_enabled
   6781 
   6782   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   6783       .Times(1)
   6784       .RetiresOnSaturation();
   6785   DrawArrays cmd;
   6786   cmd.Init(GL_TRIANGLES, 0, kNumVertices);
   6787   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6788   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   6789 }
   6790 
   6791 TEST_F(GLES2DecoderTest, DrawArraysClearsAfterTexImage2DNULLCubemap) {
   6792   static const GLenum faces[] = {
   6793     GL_TEXTURE_CUBE_MAP_POSITIVE_X,
   6794     GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
   6795     GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
   6796     GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
   6797     GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
   6798     GL_TEXTURE_CUBE_MAP_NEGATIVE_Z,
   6799   };
   6800   SetupCubemapProgram();
   6801   DoBindTexture(GL_TEXTURE_CUBE_MAP, client_texture_id_, kServiceTextureId);
   6802   // Fill out all the faces for 2 levels, leave 2 uncleared.
   6803   for (int ii = 0; ii < 6; ++ii) {
   6804     GLenum face = faces[ii];
   6805     int32 shm_id =
   6806         (face == GL_TEXTURE_CUBE_MAP_NEGATIVE_Y) ? 0 : kSharedMemoryId;
   6807     uint32 shm_offset =
   6808         (face == GL_TEXTURE_CUBE_MAP_NEGATIVE_Y) ? 0 : kSharedMemoryOffset;
   6809     DoTexImage2D(face, 0, GL_RGBA, 2, 2, 0, GL_RGBA,
   6810                  GL_UNSIGNED_BYTE, shm_id, shm_offset);
   6811     DoTexImage2D(face, 1, GL_RGBA, 1, 1, 0, GL_RGBA,
   6812                  GL_UNSIGNED_BYTE, shm_id, shm_offset);
   6813   }
   6814   // Expect 2 levels will be cleared.
   6815   SetupClearTextureExpections(
   6816       kServiceTextureId, kServiceTextureId, GL_TEXTURE_CUBE_MAP,
   6817       GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_RGBA, GL_UNSIGNED_BYTE, 2, 2);
   6818   SetupClearTextureExpections(
   6819       kServiceTextureId, kServiceTextureId, GL_TEXTURE_CUBE_MAP,
   6820       GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, 1);
   6821   AddExpectationsForSimulatedAttrib0(kNumVertices, 0);
   6822   SetupExpectationsForApplyingDefaultDirtyState();
   6823   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   6824       .Times(1)
   6825       .RetiresOnSaturation();
   6826   DrawArrays cmd;
   6827   cmd.Init(GL_TRIANGLES, 0, kNumVertices);
   6828   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6829 }
   6830 
   6831 TEST_F(GLES2DecoderTest, TextureUsageAngleExtNotEnabledByDefault) {
   6832   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   6833 
   6834   TexParameteri cmd;
   6835   cmd.Init(GL_TEXTURE_2D,
   6836            GL_TEXTURE_USAGE_ANGLE,
   6837            GL_FRAMEBUFFER_ATTACHMENT_ANGLE);
   6838   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6839   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
   6840 }
   6841 
   6842 TEST_F(GLES2DecoderWithShaderTest,
   6843        DrawClearsAfterRenderbuffersWithMultipleAttachments) {
   6844   const GLuint kFBOClientTextureId = 4100;
   6845   const GLuint kFBOServiceTextureId = 4101;
   6846 
   6847   // Register a texture id.
   6848   EXPECT_CALL(*gl_, GenTextures(_, _))
   6849       .WillOnce(SetArgumentPointee<1>(kFBOServiceTextureId))
   6850       .RetiresOnSaturation();
   6851   GenHelper<GenTexturesImmediate>(kFBOClientTextureId);
   6852 
   6853   // Setup "render to" texture.
   6854   DoBindTexture(GL_TEXTURE_2D, kFBOClientTextureId, kFBOServiceTextureId);
   6855   DoTexImage2D(
   6856       GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0, 0);
   6857   DoBindFramebuffer(
   6858       GL_FRAMEBUFFER, client_framebuffer_id_, kServiceFramebufferId);
   6859   DoFramebufferTexture2D(
   6860       GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
   6861       kFBOClientTextureId, kFBOServiceTextureId, 0, GL_NO_ERROR);
   6862 
   6863   DoBindRenderbuffer(GL_RENDERBUFFER, client_renderbuffer_id_,
   6864                     kServiceRenderbufferId);
   6865   DoBindFramebuffer(GL_FRAMEBUFFER, client_framebuffer_id_,
   6866                     kServiceFramebufferId);
   6867   DoRenderbufferStorage(
   6868       GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT,
   6869       1, 1, GL_NO_ERROR);
   6870   DoFramebufferRenderbuffer(
   6871       GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER,
   6872       client_renderbuffer_id_, kServiceRenderbufferId, GL_NO_ERROR);
   6873 
   6874   SetupTexture();
   6875   SetupExpectationsForFramebufferClearing(
   6876       GL_FRAMEBUFFER,         // target
   6877       GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT,    // clear bits
   6878       0, 0, 0, 0,             // color
   6879       0,                      // stencil
   6880       1.0f,                   // depth
   6881       false);                 // scissor test
   6882 
   6883   AddExpectationsForSimulatedAttrib0(kNumVertices, 0);
   6884   SetupExpectationsForApplyingDirtyState(
   6885       false,   // Framebuffer is RGB
   6886       true,    // Framebuffer has depth
   6887       false,   // Framebuffer has stencil
   6888       0x1111,  // color bits
   6889       true,    // depth mask
   6890       false,   // depth enabled
   6891       0,       // front stencil mask
   6892       0,       // back stencil mask
   6893       false,   // stencil enabled
   6894       false,   // cull_face_enabled
   6895       false,   // scissor_test_enabled
   6896       false);  // blend_enabled
   6897 
   6898   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   6899       .Times(1)
   6900       .RetiresOnSaturation();
   6901   DrawArrays cmd;
   6902   cmd.Init(GL_TRIANGLES, 0, kNumVertices);
   6903   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6904   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   6905 }
   6906 
   6907 TEST_F(GLES2DecoderWithShaderTest, CopyTexImageWithInCompleteFBOFails) {
   6908   GLenum target = GL_TEXTURE_2D;
   6909   GLint level = 0;
   6910   GLenum internal_format = GL_RGBA;
   6911   GLsizei width = 2;
   6912   GLsizei height = 4;
   6913   GLint border = 0;
   6914   SetupTexture();
   6915   DoBindRenderbuffer(GL_RENDERBUFFER, client_renderbuffer_id_,
   6916                     kServiceRenderbufferId);
   6917   DoBindFramebuffer(GL_FRAMEBUFFER, client_framebuffer_id_,
   6918                     kServiceFramebufferId);
   6919   DoRenderbufferStorage(
   6920       GL_RENDERBUFFER, GL_RGBA4, GL_RGBA, 0, 0, GL_NO_ERROR);
   6921   DoFramebufferRenderbuffer(
   6922       GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER,
   6923       client_renderbuffer_id_, kServiceRenderbufferId, GL_NO_ERROR);
   6924 
   6925   EXPECT_CALL(*gl_, CopyTexImage2D(_, _, _, _, _, _, _, _))
   6926       .Times(0)
   6927       .RetiresOnSaturation();
   6928   CopyTexImage2D cmd;
   6929   cmd.Init(target, level, internal_format, 0, 0, width, height, border);
   6930   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6931   EXPECT_EQ(GL_INVALID_FRAMEBUFFER_OPERATION, GetGLError());
   6932 }
   6933 
   6934 void GLES2DecoderWithShaderTest::CheckRenderbufferChangesMarkFBOAsNotComplete(
   6935     bool bound_fbo) {
   6936   FramebufferManager* framebuffer_manager = group().framebuffer_manager();
   6937   SetupTexture();
   6938   DoBindRenderbuffer(GL_RENDERBUFFER, client_renderbuffer_id_,
   6939                     kServiceRenderbufferId);
   6940   DoBindFramebuffer(GL_FRAMEBUFFER, client_framebuffer_id_,
   6941                     kServiceFramebufferId);
   6942   DoRenderbufferStorage(
   6943       GL_RENDERBUFFER, GL_RGBA4, GL_RGBA, 1, 1, GL_NO_ERROR);
   6944   DoFramebufferRenderbuffer(
   6945       GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER,
   6946       client_renderbuffer_id_, kServiceRenderbufferId, GL_NO_ERROR);
   6947 
   6948 
   6949   if (!bound_fbo) {
   6950     DoBindFramebuffer(GL_FRAMEBUFFER, 0, 0);
   6951   }
   6952 
   6953   Framebuffer* framebuffer =
   6954       framebuffer_manager->GetFramebuffer(client_framebuffer_id_);
   6955   ASSERT_TRUE(framebuffer != NULL);
   6956   framebuffer_manager->MarkAsComplete(framebuffer);
   6957   EXPECT_TRUE(framebuffer_manager->IsComplete(framebuffer));
   6958 
   6959   // Test that renderbufferStorage marks fbo as not complete.
   6960   DoRenderbufferStorage(
   6961       GL_RENDERBUFFER, GL_RGBA4, GL_RGBA, 1, 1, GL_NO_ERROR);
   6962   EXPECT_FALSE(framebuffer_manager->IsComplete(framebuffer));
   6963   framebuffer_manager->MarkAsComplete(framebuffer);
   6964   EXPECT_TRUE(framebuffer_manager->IsComplete(framebuffer));
   6965 
   6966   // Test deleting renderbuffer marks fbo as not complete.
   6967   DoDeleteRenderbuffer(client_renderbuffer_id_, kServiceRenderbufferId);
   6968   if (bound_fbo) {
   6969     EXPECT_FALSE(framebuffer_manager->IsComplete(framebuffer));
   6970   } else {
   6971     EXPECT_TRUE(framebuffer_manager->IsComplete(framebuffer));
   6972   }
   6973 }
   6974 
   6975 TEST_F(GLES2DecoderWithShaderTest,
   6976        RenderbufferChangesMarkFBOAsNotCompleteBoundFBO) {
   6977   CheckRenderbufferChangesMarkFBOAsNotComplete(true);
   6978 }
   6979 
   6980 TEST_F(GLES2DecoderWithShaderTest,
   6981        RenderbufferChangesMarkFBOAsNotCompleteUnboundFBO) {
   6982   CheckRenderbufferChangesMarkFBOAsNotComplete(false);
   6983 }
   6984 
   6985 void GLES2DecoderWithShaderTest::CheckTextureChangesMarkFBOAsNotComplete(
   6986     bool bound_fbo) {
   6987   FramebufferManager* framebuffer_manager = group().framebuffer_manager();
   6988   const GLuint kFBOClientTextureId = 4100;
   6989   const GLuint kFBOServiceTextureId = 4101;
   6990 
   6991   // Register a texture id.
   6992   EXPECT_CALL(*gl_, GenTextures(_, _))
   6993       .WillOnce(SetArgumentPointee<1>(kFBOServiceTextureId))
   6994       .RetiresOnSaturation();
   6995   GenHelper<GenTexturesImmediate>(kFBOClientTextureId);
   6996 
   6997   SetupTexture();
   6998 
   6999   // Setup "render to" texture.
   7000   DoBindTexture(GL_TEXTURE_2D, kFBOClientTextureId, kFBOServiceTextureId);
   7001   DoTexImage2D(
   7002       GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0, 0);
   7003   DoBindFramebuffer(
   7004       GL_FRAMEBUFFER, client_framebuffer_id_, kServiceFramebufferId);
   7005   DoFramebufferTexture2D(
   7006       GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
   7007       kFBOClientTextureId, kFBOServiceTextureId, 0, GL_NO_ERROR);
   7008 
   7009   DoBindRenderbuffer(GL_RENDERBUFFER, client_renderbuffer_id_,
   7010                     kServiceRenderbufferId);
   7011   DoBindFramebuffer(GL_FRAMEBUFFER, client_framebuffer_id_,
   7012                     kServiceFramebufferId);
   7013   DoRenderbufferStorage(
   7014       GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT,
   7015       1, 1, GL_NO_ERROR);
   7016   DoFramebufferRenderbuffer(
   7017       GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER,
   7018       client_renderbuffer_id_, kServiceRenderbufferId, GL_NO_ERROR);
   7019 
   7020   if (!bound_fbo) {
   7021     DoBindFramebuffer(GL_FRAMEBUFFER, 0, 0);
   7022   }
   7023 
   7024   Framebuffer* framebuffer =
   7025       framebuffer_manager->GetFramebuffer(client_framebuffer_id_);
   7026   ASSERT_TRUE(framebuffer != NULL);
   7027   framebuffer_manager->MarkAsComplete(framebuffer);
   7028   EXPECT_TRUE(framebuffer_manager->IsComplete(framebuffer));
   7029 
   7030   // Test TexImage2D marks fbo as not complete.
   7031   DoTexImage2D(
   7032       GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, 0, 0);
   7033   EXPECT_FALSE(framebuffer_manager->IsComplete(framebuffer));
   7034   framebuffer_manager->MarkAsComplete(framebuffer);
   7035   EXPECT_TRUE(framebuffer_manager->IsComplete(framebuffer));
   7036 
   7037   // Test CopyImage2D marks fbo as not complete.
   7038   EXPECT_CALL(*gl_, GetError())
   7039       .WillOnce(Return(GL_NO_ERROR))
   7040       .RetiresOnSaturation();
   7041   EXPECT_CALL(*gl_, CopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, 1, 1, 0))
   7042       .Times(1)
   7043       .RetiresOnSaturation();
   7044   EXPECT_CALL(*gl_, GetError())
   7045       .WillOnce(Return(GL_NO_ERROR))
   7046       .RetiresOnSaturation();
   7047   CopyTexImage2D cmd;
   7048   cmd.Init(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, 1, 1, 0);
   7049   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   7050   EXPECT_FALSE(framebuffer_manager->IsComplete(framebuffer));
   7051 
   7052   // Test deleting texture marks fbo as not complete.
   7053   framebuffer_manager->MarkAsComplete(framebuffer);
   7054   EXPECT_TRUE(framebuffer_manager->IsComplete(framebuffer));
   7055   DoDeleteTexture(kFBOClientTextureId, kFBOServiceTextureId);
   7056 
   7057   if (bound_fbo) {
   7058     EXPECT_FALSE(framebuffer_manager->IsComplete(framebuffer));
   7059   } else {
   7060     EXPECT_TRUE(framebuffer_manager->IsComplete(framebuffer));
   7061   }
   7062 }
   7063 
   7064 TEST_F(GLES2DecoderWithShaderTest, TextureChangesMarkFBOAsNotCompleteBoundFBO) {
   7065   CheckTextureChangesMarkFBOAsNotComplete(true);
   7066 }
   7067 
   7068 TEST_F(GLES2DecoderWithShaderTest,
   7069        TextureChangesMarkFBOAsNotCompleteUnboundFBO) {
   7070   CheckTextureChangesMarkFBOAsNotComplete(false);
   7071 }
   7072 
   7073 TEST_F(GLES2DecoderWithShaderTest,
   7074        DrawingWithFBOTwiceChecksForFBOCompleteOnce) {
   7075   const GLuint kFBOClientTextureId = 4100;
   7076   const GLuint kFBOServiceTextureId = 4101;
   7077 
   7078   SetupAllNeededVertexBuffers();
   7079 
   7080   // Register a texture id.
   7081   EXPECT_CALL(*gl_, GenTextures(_, _))
   7082       .WillOnce(SetArgumentPointee<1>(kFBOServiceTextureId))
   7083       .RetiresOnSaturation();
   7084   GenHelper<GenTexturesImmediate>(kFBOClientTextureId);
   7085 
   7086   // Setup "render to" texture that is cleared.
   7087   DoBindTexture(GL_TEXTURE_2D, kFBOClientTextureId, kFBOServiceTextureId);
   7088   DoTexImage2D(
   7089       GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   7090       kSharedMemoryId, kSharedMemoryOffset);
   7091   DoBindFramebuffer(
   7092       GL_FRAMEBUFFER, client_framebuffer_id_, kServiceFramebufferId);
   7093   DoFramebufferTexture2D(
   7094       GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
   7095       kFBOClientTextureId, kFBOServiceTextureId, 0, GL_NO_ERROR);
   7096 
   7097   // Setup "render from" texture.
   7098   SetupTexture();
   7099 
   7100   // Make sure we check for framebuffer complete.
   7101   EXPECT_CALL(*gl_, CheckFramebufferStatusEXT(GL_FRAMEBUFFER))
   7102       .WillOnce(Return(GL_FRAMEBUFFER_COMPLETE))
   7103       .RetiresOnSaturation();
   7104 
   7105   SetupExpectationsForApplyingDirtyState(
   7106       false,   // Framebuffer is RGB
   7107       false,   // Framebuffer has depth
   7108       false,   // Framebuffer has stencil
   7109       0x1111,  // color bits
   7110       false,   // depth mask
   7111       false,   // depth enabled
   7112       0,       // front stencil mask
   7113       0,       // back stencil mask
   7114       false,   // stencil enabled
   7115       false,   // cull_face_enabled
   7116       false,   // scissor_test_enabled
   7117       false);  // blend_enabled
   7118 
   7119   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   7120       .Times(1)
   7121       .RetiresOnSaturation();
   7122   DrawArrays cmd;
   7123   cmd.Init(GL_TRIANGLES, 0, kNumVertices);
   7124   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   7125   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   7126 
   7127   // But not again.
   7128   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   7129       .Times(1)
   7130       .RetiresOnSaturation();
   7131   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   7132   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   7133 }
   7134 
   7135 TEST_F(GLES2DecoderTest, BeginQueryEXTDisabled) {
   7136   // Test something fails if off.
   7137 }
   7138 
   7139 TEST_F(GLES2DecoderManualInitTest, BeginEndQueryEXT) {
   7140   InitDecoder(
   7141       "GL_EXT_occlusion_query_boolean",      // extensions
   7142       true,    // has alpha
   7143       false,   // has depth
   7144       false,   // has stencil
   7145       true,    // request alpha
   7146       false,   // request depth
   7147       false,   // request stencil
   7148       true);   // bind generates resource
   7149 
   7150   // Test end fails if no begin.
   7151   EndQueryEXT end_cmd;
   7152   end_cmd.Init(GL_ANY_SAMPLES_PASSED_EXT, 1);
   7153   EXPECT_EQ(error::kNoError, ExecuteCmd(end_cmd));
   7154   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   7155 
   7156   BeginQueryEXT begin_cmd;
   7157 
   7158   // Test id = 0 fails.
   7159   begin_cmd.Init(
   7160       GL_ANY_SAMPLES_PASSED_EXT, 0, kSharedMemoryId, kSharedMemoryOffset);
   7161   EXPECT_EQ(error::kNoError, ExecuteCmd(begin_cmd));
   7162   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   7163 
   7164   GenHelper<GenQueriesEXTImmediate>(kNewClientId);
   7165 
   7166   // Test valid parameters work.
   7167   EXPECT_CALL(*gl_, GenQueriesARB(1, _))
   7168      .WillOnce(SetArgumentPointee<1>(kNewServiceId))
   7169      .RetiresOnSaturation();
   7170   EXPECT_CALL(*gl_, BeginQueryARB(GL_ANY_SAMPLES_PASSED_EXT, kNewServiceId))
   7171       .Times(1)
   7172       .RetiresOnSaturation();
   7173   begin_cmd.Init(
   7174       GL_ANY_SAMPLES_PASSED_EXT, kNewClientId,
   7175       kSharedMemoryId, kSharedMemoryOffset);
   7176   EXPECT_EQ(error::kNoError, ExecuteCmd(begin_cmd));
   7177   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   7178 
   7179   QueryManager* query_manager = decoder_->GetQueryManager();
   7180   ASSERT_TRUE(query_manager != NULL);
   7181   QueryManager::Query* query = query_manager->GetQuery(kNewClientId);
   7182   ASSERT_TRUE(query != NULL);
   7183   EXPECT_FALSE(query->pending());
   7184 
   7185   // Test trying begin again fails
   7186   EXPECT_EQ(error::kNoError, ExecuteCmd(begin_cmd));
   7187   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   7188 
   7189   // Test end fails with different target
   7190   end_cmd.Init(GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT, 1);
   7191   EXPECT_EQ(error::kNoError, ExecuteCmd(end_cmd));
   7192   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   7193 
   7194   // Test end succeeds
   7195   EXPECT_CALL(*gl_, EndQueryARB(GL_ANY_SAMPLES_PASSED_EXT))
   7196       .Times(1)
   7197       .RetiresOnSaturation();
   7198   end_cmd.Init(GL_ANY_SAMPLES_PASSED_EXT, 1);
   7199   EXPECT_EQ(error::kNoError, ExecuteCmd(end_cmd));
   7200   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   7201   EXPECT_TRUE(query->pending());
   7202 
   7203   EXPECT_CALL(*gl_, DeleteQueriesARB(1, _))
   7204       .Times(1)
   7205       .RetiresOnSaturation();
   7206 }
   7207 
   7208 static void CheckBeginEndQueryBadMemoryFails(
   7209     GLES2DecoderTestBase* test,
   7210     GLuint client_id,
   7211     GLuint service_id,
   7212     int32 shm_id,
   7213     uint32 shm_offset) {
   7214   ::testing::StrictMock< ::gfx::MockGLInterface>* gl = test->GetGLMock();
   7215 
   7216   BeginQueryEXT begin_cmd;
   7217 
   7218   test->GenHelper<GenQueriesEXTImmediate>(client_id);
   7219 
   7220   EXPECT_CALL(*gl, GenQueriesARB(1, _))
   7221      .WillOnce(SetArgumentPointee<1>(service_id))
   7222      .RetiresOnSaturation();
   7223   EXPECT_CALL(*gl, BeginQueryARB(GL_ANY_SAMPLES_PASSED_EXT, service_id))
   7224       .Times(1)
   7225       .RetiresOnSaturation();
   7226 
   7227   // Test bad shared memory fails
   7228   begin_cmd.Init(GL_ANY_SAMPLES_PASSED_EXT, client_id, shm_id, shm_offset);
   7229   error::Error error1 = test->ExecuteCmd(begin_cmd);
   7230 
   7231   EXPECT_CALL(*gl, EndQueryARB(GL_ANY_SAMPLES_PASSED_EXT))
   7232       .Times(1)
   7233       .RetiresOnSaturation();
   7234 
   7235   EndQueryEXT end_cmd;
   7236   end_cmd.Init(GL_ANY_SAMPLES_PASSED_EXT, 1);
   7237   error::Error error2 = test->ExecuteCmd(end_cmd);
   7238 
   7239   EXPECT_CALL(*gl,
   7240       GetQueryObjectuivARB(service_id, GL_QUERY_RESULT_AVAILABLE_EXT, _))
   7241       .WillOnce(SetArgumentPointee<2>(1))
   7242       .RetiresOnSaturation();
   7243   EXPECT_CALL(*gl,
   7244       GetQueryObjectuivARB(service_id, GL_QUERY_RESULT_EXT, _))
   7245       .WillOnce(SetArgumentPointee<2>(1))
   7246       .RetiresOnSaturation();
   7247 
   7248   QueryManager* query_manager = test->GetDecoder()->GetQueryManager();
   7249   ASSERT_TRUE(query_manager != NULL);
   7250   bool process_success = query_manager->ProcessPendingQueries();
   7251 
   7252   EXPECT_TRUE(error1 != error::kNoError ||
   7253               error2 != error::kNoError ||
   7254               !process_success);
   7255 
   7256   EXPECT_CALL(*gl, DeleteQueriesARB(1, _))
   7257       .Times(1)
   7258       .RetiresOnSaturation();
   7259 }
   7260 
   7261 TEST_F(GLES2DecoderManualInitTest, BeginEndQueryEXTBadMemoryIdFails) {
   7262   InitDecoder(
   7263       "GL_EXT_occlusion_query_boolean",      // extensions
   7264       true,    // has alpha
   7265       false,   // has depth
   7266       false,   // has stencil
   7267       true,    // request alpha
   7268       false,   // request depth
   7269       false,   // request stencil
   7270       true);   // bind generates resource
   7271 
   7272   CheckBeginEndQueryBadMemoryFails(
   7273       this, kNewClientId, kNewServiceId,
   7274       kInvalidSharedMemoryId, kSharedMemoryOffset);
   7275 }
   7276 
   7277 TEST_F(GLES2DecoderManualInitTest, BeginEndQueryEXTBadMemoryOffsetFails) {
   7278   InitDecoder(
   7279       "GL_EXT_occlusion_query_boolean",      // extensions
   7280       true,    // has alpha
   7281       false,   // has depth
   7282       false,   // has stencil
   7283       true,    // request alpha
   7284       false,   // request depth
   7285       false,   // request stencil
   7286       true);   // bind generates resource
   7287 
   7288   CheckBeginEndQueryBadMemoryFails(
   7289       this, kNewClientId, kNewServiceId,
   7290       kSharedMemoryId, kInvalidSharedMemoryOffset);
   7291 }
   7292 
   7293 TEST_F(GLES2DecoderTest, BeginEndQueryEXTCommandsIssuedCHROMIUM) {
   7294   BeginQueryEXT begin_cmd;
   7295 
   7296   GenHelper<GenQueriesEXTImmediate>(kNewClientId);
   7297 
   7298   // Test valid parameters work.
   7299   begin_cmd.Init(
   7300       GL_COMMANDS_ISSUED_CHROMIUM, kNewClientId,
   7301       kSharedMemoryId, kSharedMemoryOffset);
   7302   EXPECT_EQ(error::kNoError, ExecuteCmd(begin_cmd));
   7303   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   7304 
   7305   QueryManager* query_manager = decoder_->GetQueryManager();
   7306   ASSERT_TRUE(query_manager != NULL);
   7307   QueryManager::Query* query = query_manager->GetQuery(kNewClientId);
   7308   ASSERT_TRUE(query != NULL);
   7309   EXPECT_FALSE(query->pending());
   7310 
   7311   // Test end succeeds
   7312   EndQueryEXT end_cmd;
   7313   end_cmd.Init(GL_COMMANDS_ISSUED_CHROMIUM, 1);
   7314   EXPECT_EQ(error::kNoError, ExecuteCmd(end_cmd));
   7315   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   7316   EXPECT_FALSE(query->pending());
   7317 }
   7318 
   7319 TEST_F(GLES2DecoderTest, BeginEndQueryEXTGetErrorQueryCHROMIUM) {
   7320   BeginQueryEXT begin_cmd;
   7321 
   7322   GenHelper<GenQueriesEXTImmediate>(kNewClientId);
   7323 
   7324   // Test valid parameters work.
   7325   begin_cmd.Init(
   7326       GL_GET_ERROR_QUERY_CHROMIUM, kNewClientId,
   7327       kSharedMemoryId, kSharedMemoryOffset);
   7328   EXPECT_EQ(error::kNoError, ExecuteCmd(begin_cmd));
   7329   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   7330 
   7331   QueryManager* query_manager = decoder_->GetQueryManager();
   7332   ASSERT_TRUE(query_manager != NULL);
   7333   QueryManager::Query* query = query_manager->GetQuery(kNewClientId);
   7334   ASSERT_TRUE(query != NULL);
   7335   EXPECT_FALSE(query->pending());
   7336 
   7337   // Test end succeeds
   7338   QuerySync* sync = static_cast<QuerySync*>(shared_memory_address_);
   7339 
   7340   EXPECT_CALL(*gl_, GetError())
   7341       .WillOnce(Return(GL_INVALID_VALUE))
   7342       .RetiresOnSaturation();
   7343 
   7344   EndQueryEXT end_cmd;
   7345   end_cmd.Init(GL_GET_ERROR_QUERY_CHROMIUM, 1);
   7346   EXPECT_EQ(error::kNoError, ExecuteCmd(end_cmd));
   7347   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   7348   EXPECT_FALSE(query->pending());
   7349   EXPECT_EQ(static_cast<GLenum>(GL_INVALID_VALUE),
   7350             static_cast<GLenum>(sync->result));
   7351 }
   7352 
   7353 TEST_F(GLES2DecoderTest, ProduceAndConsumeTextureCHROMIUM) {
   7354   Mailbox mailbox;
   7355   group().mailbox_manager()->GenerateMailbox(&mailbox);
   7356 
   7357   memcpy(shared_memory_address_, mailbox.name, sizeof(mailbox.name));
   7358 
   7359   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   7360   DoTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 3, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   7361                0, 0);
   7362   DoTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA, 2, 4, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   7363                0, 0);
   7364   TextureRef* texture_ref = group().texture_manager()->GetTexture(
   7365       client_texture_id_);
   7366   ASSERT_TRUE(texture_ref != NULL);
   7367   Texture* texture = texture_ref->texture();
   7368   EXPECT_EQ(kServiceTextureId, texture->service_id());
   7369 
   7370   ProduceTextureCHROMIUM produce_cmd;
   7371   produce_cmd.Init(GL_TEXTURE_2D, kSharedMemoryId, kSharedMemoryOffset);
   7372   EXPECT_EQ(error::kNoError, ExecuteCmd(produce_cmd));
   7373   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   7374 
   7375   // Texture didn't change.
   7376   GLsizei width;
   7377   GLsizei height;
   7378   GLenum type;
   7379   GLenum internal_format;
   7380 
   7381   EXPECT_TRUE(texture->GetLevelSize(GL_TEXTURE_2D, 0, &width, &height));
   7382   EXPECT_EQ(3, width);
   7383   EXPECT_EQ(1, height);
   7384   EXPECT_TRUE(texture->GetLevelType(GL_TEXTURE_2D, 0, &type, &internal_format));
   7385   EXPECT_EQ(static_cast<GLenum>(GL_RGBA), internal_format);
   7386   EXPECT_EQ(static_cast<GLenum>(GL_UNSIGNED_BYTE), type);
   7387 
   7388   EXPECT_TRUE(texture->GetLevelSize(GL_TEXTURE_2D, 1, &width, &height));
   7389   EXPECT_EQ(2, width);
   7390   EXPECT_EQ(4, height);
   7391   EXPECT_TRUE(texture->GetLevelType(GL_TEXTURE_2D, 1, &type, &internal_format));
   7392   EXPECT_EQ(static_cast<GLenum>(GL_RGBA), internal_format);
   7393   EXPECT_EQ(static_cast<GLenum>(GL_UNSIGNED_BYTE), type);
   7394 
   7395   // Service ID has not changed.
   7396   EXPECT_EQ(kServiceTextureId, texture->service_id());
   7397 
   7398   // Create new texture for consume.
   7399   EXPECT_CALL(*gl_, GenTextures(_, _))
   7400       .WillOnce(SetArgumentPointee<1>(kNewServiceId))
   7401       .RetiresOnSaturation();
   7402   DoBindTexture(GL_TEXTURE_2D, kNewClientId, kNewServiceId);
   7403 
   7404   // Assigns and binds original service size texture ID.
   7405   EXPECT_CALL(*gl_, DeleteTextures(1, _))
   7406       .Times(1)
   7407       .RetiresOnSaturation();
   7408   EXPECT_CALL(*gl_, BindTexture(GL_TEXTURE_2D, kServiceTextureId))
   7409       .Times(1)
   7410       .RetiresOnSaturation();
   7411 
   7412   memcpy(shared_memory_address_, mailbox.name, sizeof(mailbox.name));
   7413   ConsumeTextureCHROMIUM consume_cmd;
   7414   consume_cmd.Init(GL_TEXTURE_2D, kSharedMemoryId, kSharedMemoryOffset);
   7415   EXPECT_EQ(error::kNoError, ExecuteCmd(consume_cmd));
   7416   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   7417 
   7418   // Texture is redefined.
   7419   EXPECT_TRUE(texture->GetLevelSize(GL_TEXTURE_2D, 0, &width, &height));
   7420   EXPECT_EQ(3, width);
   7421   EXPECT_EQ(1, height);
   7422   EXPECT_TRUE(texture->GetLevelType(GL_TEXTURE_2D, 0, &type, &internal_format));
   7423   EXPECT_EQ(static_cast<GLenum>(GL_RGBA), internal_format);
   7424   EXPECT_EQ(static_cast<GLenum>(GL_UNSIGNED_BYTE), type);
   7425 
   7426   EXPECT_TRUE(texture->GetLevelSize(GL_TEXTURE_2D, 1, &width, &height));
   7427   EXPECT_EQ(2, width);
   7428   EXPECT_EQ(4, height);
   7429   EXPECT_TRUE(texture->GetLevelType(GL_TEXTURE_2D, 1, &type, &internal_format));
   7430   EXPECT_EQ(static_cast<GLenum>(GL_RGBA), internal_format);
   7431   EXPECT_EQ(static_cast<GLenum>(GL_UNSIGNED_BYTE), type);
   7432 
   7433   // Service ID is restored.
   7434   EXPECT_EQ(kServiceTextureId, texture->service_id());
   7435 }
   7436 
   7437 
   7438 TEST_F(GLES2DecoderTest, CanChangeSurface) {
   7439   scoped_refptr<GLSurfaceMock> other_surface(new GLSurfaceMock);
   7440   EXPECT_CALL(*other_surface.get(), GetBackingFrameBufferObject()).
   7441       WillOnce(Return(7));
   7442   EXPECT_CALL(*gl_, BindFramebufferEXT(GL_FRAMEBUFFER_EXT, 7));
   7443 
   7444   decoder_->SetSurface(other_surface);
   7445 }
   7446 
   7447 TEST_F(GLES2DecoderTest, IsEnabledReturnsCachedValue) {
   7448   // NOTE: There are no expectations because no GL functions should be
   7449   // called for DEPTH_TEST or STENCIL_TEST
   7450   static const GLenum kStates[] = {
   7451     GL_DEPTH_TEST,
   7452     GL_STENCIL_TEST,
   7453   };
   7454   for (size_t ii = 0; ii < arraysize(kStates); ++ii) {
   7455     Enable enable_cmd;
   7456     GLenum state = kStates[ii];
   7457     enable_cmd.Init(state);
   7458     EXPECT_EQ(error::kNoError, ExecuteCmd(enable_cmd));
   7459     IsEnabled::Result* result =
   7460         static_cast<IsEnabled::Result*>(shared_memory_address_);
   7461     IsEnabled is_enabled_cmd;
   7462     is_enabled_cmd.Init(state, shared_memory_id_, shared_memory_offset_);
   7463     EXPECT_EQ(error::kNoError, ExecuteCmd(is_enabled_cmd));
   7464     EXPECT_NE(0u, *result);
   7465     Disable disable_cmd;
   7466     disable_cmd.Init(state);
   7467     EXPECT_EQ(error::kNoError, ExecuteCmd(disable_cmd));
   7468     EXPECT_EQ(error::kNoError, ExecuteCmd(is_enabled_cmd));
   7469     EXPECT_EQ(0u, *result);
   7470   }
   7471 }
   7472 
   7473 TEST_F(GLES2DecoderManualInitTest, DepthTextureBadArgs) {
   7474   InitDecoder(
   7475       "GL_ANGLE_depth_texture",      // extensions
   7476       false,   // has alpha
   7477       true,    // has depth
   7478       true,    // has stencil
   7479       false,   // request alpha
   7480       true,    // request depth
   7481       true,    // request stencil
   7482       true);   // bind generates resource
   7483 
   7484   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   7485   // Check trying to upload data fails.
   7486   TexImage2D tex_cmd;
   7487   tex_cmd.Init(
   7488       GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT,
   7489       1, 1, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT,
   7490       kSharedMemoryId, kSharedMemoryOffset);
   7491   EXPECT_EQ(error::kNoError, ExecuteCmd(tex_cmd));
   7492   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   7493   // Try level > 0.
   7494   tex_cmd.Init(
   7495       GL_TEXTURE_2D, 1, GL_DEPTH_COMPONENT,
   7496       1, 1, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, 0, 0);
   7497   EXPECT_EQ(error::kNoError, ExecuteCmd(tex_cmd));
   7498   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   7499   // Make a 1 pixel depth texture.
   7500   DoTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT,
   7501                1, 1, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, 0, 0);
   7502   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   7503 
   7504   // Check that trying to update it fails.
   7505   TexSubImage2D tex_sub_cmd;
   7506   tex_sub_cmd.Init(
   7507       GL_TEXTURE_2D, 0, 0, 0, 1, 1, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT,
   7508       kSharedMemoryId, kSharedMemoryOffset, GL_FALSE);
   7509   EXPECT_EQ(error::kNoError, ExecuteCmd(tex_sub_cmd));
   7510   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   7511 
   7512   // Check that trying to CopyTexImage2D fails
   7513   CopyTexImage2D copy_tex_cmd;
   7514   copy_tex_cmd.Init(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 0, 0, 1, 1, 0);
   7515   EXPECT_EQ(error::kNoError, ExecuteCmd(copy_tex_cmd));
   7516   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   7517 
   7518   // Check that trying to CopyTexSubImage2D fails
   7519   CopyTexSubImage2D copy_sub_cmd;
   7520   copy_sub_cmd.Init(GL_TEXTURE_2D, 0, 0, 0, 0, 0, 1, 1);
   7521   EXPECT_EQ(error::kNoError, ExecuteCmd(copy_sub_cmd));
   7522   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   7523 }
   7524 
   7525 TEST_F(GLES2DecoderManualInitTest, GenerateMipmapDepthTexture) {
   7526   InitDecoder(
   7527       "GL_ANGLE_depth_texture",      // extensions
   7528       false,   // has alpha
   7529       true,    // has depth
   7530       true,    // has stencil
   7531       false,   // request alpha
   7532       true,    // request depth
   7533       true,    // request stencil
   7534       true);   // bind generates resource
   7535   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   7536   DoTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT,
   7537                2, 2, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT,
   7538                0, 0);
   7539   GenerateMipmap cmd;
   7540   cmd.Init(GL_TEXTURE_2D);
   7541   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   7542   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   7543 }
   7544 
   7545 TEST_F(GLES2DecoderManualInitTest, DrawClearsDepthTexture) {
   7546   InitDecoder(
   7547       "GL_ANGLE_depth_texture",      // extensions
   7548       true,    // has alpha
   7549       true,    // has depth
   7550       false,   // has stencil
   7551       true,    // request alpha
   7552       true,    // request depth
   7553       false,   // request stencil
   7554       true);   // bind generates resource
   7555 
   7556   SetupDefaultProgram();
   7557   SetupAllNeededVertexBuffers();
   7558   const GLenum attachment = GL_DEPTH_ATTACHMENT;
   7559   const GLenum target = GL_TEXTURE_2D;
   7560   const GLint level = 0;
   7561   DoBindTexture(target, client_texture_id_, kServiceTextureId);
   7562 
   7563   // Create a depth texture.
   7564   DoTexImage2D(target, level, GL_DEPTH_COMPONENT, 1, 1, 0,
   7565                GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, 0, 0);
   7566 
   7567   EXPECT_CALL(*gl_, GenFramebuffersEXT(1, _))
   7568       .Times(1)
   7569       .RetiresOnSaturation();
   7570   EXPECT_CALL(*gl_, BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, _))
   7571       .Times(1)
   7572       .RetiresOnSaturation();
   7573 
   7574   EXPECT_CALL(*gl_, FramebufferTexture2DEXT(
   7575       GL_DRAW_FRAMEBUFFER_EXT, attachment, target, kServiceTextureId, level))
   7576       .Times(1)
   7577       .RetiresOnSaturation();
   7578   EXPECT_CALL(*gl_, CheckFramebufferStatusEXT(GL_DRAW_FRAMEBUFFER_EXT))
   7579       .WillOnce(Return(GL_FRAMEBUFFER_COMPLETE))
   7580       .RetiresOnSaturation();
   7581 
   7582   EXPECT_CALL(*gl_, ClearStencil(0))
   7583       .Times(1)
   7584       .RetiresOnSaturation();
   7585   EXPECT_CALL(*gl_, StencilMask(-1))
   7586       .Times(1)
   7587       .RetiresOnSaturation();
   7588   EXPECT_CALL(*gl_, ClearDepth(1.0f))
   7589       .Times(1)
   7590       .RetiresOnSaturation();
   7591   EXPECT_CALL(*gl_, DepthMask(true))
   7592       .Times(1)
   7593       .RetiresOnSaturation();
   7594   EXPECT_CALL(*gl_, Disable(GL_SCISSOR_TEST))
   7595       .Times(1)
   7596       .RetiresOnSaturation();
   7597 
   7598   EXPECT_CALL(*gl_, Clear(GL_DEPTH_BUFFER_BIT))
   7599       .Times(1)
   7600       .RetiresOnSaturation();
   7601 
   7602   SetupExpectationsForRestoreClearState(
   7603       0.0f, 0.0f, 0.0f, 0.0f, 0, 1.0f, false);
   7604 
   7605   EXPECT_CALL(*gl_, DeleteFramebuffersEXT(1, _))
   7606       .Times(1)
   7607       .RetiresOnSaturation();
   7608   EXPECT_CALL(*gl_, BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, 0))
   7609       .Times(1)
   7610       .RetiresOnSaturation();
   7611 
   7612   SetupExpectationsForApplyingDefaultDirtyState();
   7613   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   7614       .Times(1)
   7615       .RetiresOnSaturation();
   7616   DrawArrays cmd;
   7617   cmd.Init(GL_TRIANGLES, 0, kNumVertices);
   7618   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   7619   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   7620 }
   7621 
   7622 TEST_F(GLES2DecoderWithShaderTest, BindUniformLocationCHROMIUM) {
   7623   const GLint kLocation = 2;
   7624   const char* kName = "testing";
   7625   const uint32 kNameSize = strlen(kName);
   7626   const char* kBadName1 = "gl_testing";
   7627   const uint32 kBadName1Size = strlen(kBadName1);
   7628   const char* kBadName2 = "testing[1]";
   7629   const uint32 kBadName2Size = strlen(kBadName2);
   7630   memcpy(shared_memory_address_, kName, kNameSize);
   7631   BindUniformLocationCHROMIUM cmd;
   7632   cmd.Init(client_program_id_, kLocation, kSharedMemoryId, kSharedMemoryOffset,
   7633            kNameSize);
   7634   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   7635   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   7636   // check negative location
   7637   memcpy(shared_memory_address_, kName, kNameSize);
   7638   cmd.Init(client_program_id_, -1, kSharedMemoryId, kSharedMemoryOffset,
   7639            kNameSize);
   7640   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   7641   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   7642   // check highest location
   7643   memcpy(shared_memory_address_, kName, kNameSize);
   7644   GLint kMaxLocation =
   7645       (kMaxFragmentUniformVectors + kMaxVertexUniformVectors) * 4 - 1;
   7646   cmd.Init(client_program_id_, kMaxLocation, kSharedMemoryId,
   7647            kSharedMemoryOffset, kNameSize);
   7648   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   7649   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   7650   // check too high location
   7651   memcpy(shared_memory_address_, kName, kNameSize);
   7652   cmd.Init(client_program_id_, kMaxLocation + 1, kSharedMemoryId,
   7653            kSharedMemoryOffset, kNameSize);
   7654   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   7655   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   7656   // check bad name "gl_..."
   7657   memcpy(shared_memory_address_, kBadName1, kBadName1Size);
   7658   cmd.Init(client_program_id_, kLocation, kSharedMemoryId, kSharedMemoryOffset,
   7659            kBadName1Size);
   7660   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   7661   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   7662   // check bad name "name[1]" non zero
   7663   memcpy(shared_memory_address_, kBadName2, kBadName2Size);
   7664   cmd.Init(client_program_id_, kLocation, kSharedMemoryId, kSharedMemoryOffset,
   7665            kBadName2Size);
   7666   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   7667   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   7668 }
   7669 
   7670 class GLES2DecoderVertexArraysOESTest : public GLES2DecoderWithShaderTest {
   7671  public:
   7672   GLES2DecoderVertexArraysOESTest() { }
   7673 
   7674   bool vertex_array_deleted_manually_;
   7675 
   7676   virtual void SetUp() {
   7677     InitDecoder(
   7678         "GL_OES_vertex_array_object",  // extensions
   7679         false,  // has alpha
   7680         false,  // has depth
   7681         false,  // has stencil
   7682         false,  // request alpha
   7683         false,  // request depth
   7684         false,  // request stencil
   7685         true);  // bind generates resource
   7686     SetupDefaultProgram();
   7687 
   7688     AddExpectationsForGenVertexArraysOES();
   7689     GenHelper<GenVertexArraysOESImmediate>(client_vertexarray_id_);
   7690 
   7691     vertex_array_deleted_manually_ = false;
   7692   }
   7693 
   7694   virtual void TearDown() {
   7695     // This should only be set if the test handled deletion of the vertex array
   7696     // itself. Necessary because vertex_array_objects are not sharable, and thus
   7697     // not managed in the ContextGroup, meaning they will be destroyed during
   7698     // test tear down
   7699     if (!vertex_array_deleted_manually_) {
   7700       AddExpectationsForDeleteVertexArraysOES();
   7701     }
   7702 
   7703     GLES2DecoderWithShaderTest::TearDown();
   7704   }
   7705 
   7706   void GenVertexArraysOESValidArgs() {
   7707     AddExpectationsForGenVertexArraysOES();
   7708     GetSharedMemoryAs<GLuint*>()[0] = kNewClientId;
   7709     GenVertexArraysOES cmd;
   7710     cmd.Init(1, shared_memory_id_, shared_memory_offset_);
   7711     EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   7712     EXPECT_EQ(GL_NO_ERROR, GetGLError());
   7713     EXPECT_TRUE(GetVertexArrayInfo(kNewClientId) != NULL);
   7714     AddExpectationsForDeleteVertexArraysOES();
   7715   }
   7716 
   7717   void GenVertexArraysOESInvalidArgs() {
   7718     EXPECT_CALL(*gl_, GenVertexArraysOES(_, _)).Times(0);
   7719     GetSharedMemoryAs<GLuint*>()[0] = client_vertexarray_id_;
   7720     GenVertexArraysOES cmd;
   7721     cmd.Init(1, shared_memory_id_, shared_memory_offset_);
   7722     EXPECT_EQ(error::kInvalidArguments, ExecuteCmd(cmd));
   7723   }
   7724 
   7725   void GenVertexArraysOESImmediateValidArgs() {
   7726     AddExpectationsForGenVertexArraysOES();
   7727     GenVertexArraysOESImmediate* cmd =
   7728         GetImmediateAs<GenVertexArraysOESImmediate>();
   7729     GLuint temp = kNewClientId;
   7730     cmd->Init(1, &temp);
   7731     EXPECT_EQ(error::kNoError,
   7732               ExecuteImmediateCmd(*cmd, sizeof(temp)));
   7733     EXPECT_EQ(GL_NO_ERROR, GetGLError());
   7734     EXPECT_TRUE(GetVertexArrayInfo(kNewClientId) != NULL);
   7735     AddExpectationsForDeleteVertexArraysOES();
   7736   }
   7737 
   7738   void GenVertexArraysOESImmediateInvalidArgs() {
   7739     EXPECT_CALL(*gl_, GenVertexArraysOES(_, _)).Times(0);
   7740     GenVertexArraysOESImmediate* cmd =
   7741         GetImmediateAs<GenVertexArraysOESImmediate>();
   7742     cmd->Init(1, &client_vertexarray_id_);
   7743     EXPECT_EQ(error::kInvalidArguments,
   7744               ExecuteImmediateCmd(*cmd, sizeof(&client_vertexarray_id_)));
   7745   }
   7746 
   7747   void DeleteVertexArraysOESValidArgs() {
   7748     AddExpectationsForDeleteVertexArraysOES();
   7749     GetSharedMemoryAs<GLuint*>()[0] = client_vertexarray_id_;
   7750     DeleteVertexArraysOES cmd;
   7751     cmd.Init(1, shared_memory_id_, shared_memory_offset_);
   7752     EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   7753     EXPECT_EQ(GL_NO_ERROR, GetGLError());
   7754     EXPECT_TRUE(
   7755         GetVertexArrayInfo(client_vertexarray_id_) == NULL);
   7756     vertex_array_deleted_manually_ = true;
   7757   }
   7758 
   7759   void DeleteVertexArraysOESInvalidArgs() {
   7760     GetSharedMemoryAs<GLuint*>()[0] = kInvalidClientId;
   7761     DeleteVertexArraysOES cmd;
   7762     cmd.Init(1, shared_memory_id_, shared_memory_offset_);
   7763     EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   7764   }
   7765 
   7766   void DeleteVertexArraysOESImmediateValidArgs() {
   7767     AddExpectationsForDeleteVertexArraysOES();
   7768     DeleteVertexArraysOESImmediate& cmd =
   7769         *GetImmediateAs<DeleteVertexArraysOESImmediate>();
   7770     cmd.Init(1, &client_vertexarray_id_);
   7771     EXPECT_EQ(error::kNoError,
   7772               ExecuteImmediateCmd(cmd, sizeof(client_vertexarray_id_)));
   7773     EXPECT_EQ(GL_NO_ERROR, GetGLError());
   7774     EXPECT_TRUE(
   7775         GetVertexArrayInfo(client_vertexarray_id_) == NULL);
   7776     vertex_array_deleted_manually_ = true;
   7777   }
   7778 
   7779   void DeleteVertexArraysOESImmediateInvalidArgs() {
   7780     DeleteVertexArraysOESImmediate& cmd =
   7781         *GetImmediateAs<DeleteVertexArraysOESImmediate>();
   7782     GLuint temp = kInvalidClientId;
   7783     cmd.Init(1, &temp);
   7784     EXPECT_EQ(error::kNoError,
   7785               ExecuteImmediateCmd(cmd, sizeof(temp)));
   7786   }
   7787 
   7788   void IsVertexArrayOESValidArgs() {
   7789     IsVertexArrayOES cmd;
   7790     cmd.Init(client_vertexarray_id_, shared_memory_id_, shared_memory_offset_);
   7791     EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   7792     EXPECT_EQ(GL_NO_ERROR, GetGLError());
   7793   }
   7794 
   7795   void IsVertexArrayOESInvalidArgsBadSharedMemoryId() {
   7796     IsVertexArrayOES cmd;
   7797     cmd.Init(
   7798         client_vertexarray_id_, kInvalidSharedMemoryId, shared_memory_offset_);
   7799     EXPECT_EQ(error::kOutOfBounds, ExecuteCmd(cmd));
   7800     cmd.Init(
   7801         client_vertexarray_id_, shared_memory_id_, kInvalidSharedMemoryOffset);
   7802     EXPECT_EQ(error::kOutOfBounds, ExecuteCmd(cmd));
   7803   }
   7804 
   7805   void BindVertexArrayOESValidArgs() {
   7806     AddExpectationsForBindVertexArrayOES();
   7807     BindVertexArrayOES cmd;
   7808     cmd.Init(client_vertexarray_id_);
   7809     EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   7810     EXPECT_EQ(GL_NO_ERROR, GetGLError());
   7811   }
   7812 
   7813   void BindVertexArrayOESValidArgsNewId() {
   7814     BindVertexArrayOES cmd;
   7815     cmd.Init(kNewClientId);
   7816     EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   7817     EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   7818   }
   7819 };
   7820 
   7821 class GLES2DecoderEmulatedVertexArraysOESTest
   7822     : public GLES2DecoderVertexArraysOESTest {
   7823  public:
   7824   GLES2DecoderEmulatedVertexArraysOESTest() { }
   7825 
   7826   virtual void SetUp() {
   7827     InitDecoder(
   7828         "",     // extensions
   7829         false,  // has alpha
   7830         false,  // has depth
   7831         false,  // has stencil
   7832         false,  // request alpha
   7833         false,  // request depth
   7834         false,  // request stencil
   7835         true);  // bind generates resource
   7836     SetupDefaultProgram();
   7837 
   7838     AddExpectationsForGenVertexArraysOES();
   7839     GenHelper<GenVertexArraysOESImmediate>(client_vertexarray_id_);
   7840 
   7841     vertex_array_deleted_manually_ = false;
   7842   }
   7843 };
   7844 
   7845 // Test vertex array objects with native support
   7846 TEST_F(GLES2DecoderVertexArraysOESTest, GenVertexArraysOESValidArgs) {
   7847   GenVertexArraysOESValidArgs();
   7848 }
   7849 TEST_F(GLES2DecoderEmulatedVertexArraysOESTest, GenVertexArraysOESValidArgs) {
   7850   GenVertexArraysOESValidArgs();
   7851 }
   7852 
   7853 TEST_F(GLES2DecoderVertexArraysOESTest, GenVertexArraysOESInvalidArgs) {
   7854   GenVertexArraysOESInvalidArgs();
   7855 }
   7856 TEST_F(GLES2DecoderEmulatedVertexArraysOESTest, ) {
   7857   GenVertexArraysOESInvalidArgs();
   7858 }
   7859 
   7860 TEST_F(GLES2DecoderVertexArraysOESTest, GenVertexArraysOESImmediateValidArgs) {
   7861   GenVertexArraysOESImmediateValidArgs();
   7862 }
   7863 TEST_F(GLES2DecoderEmulatedVertexArraysOESTest,
   7864     GenVertexArraysOESImmediateValidArgs) {
   7865   GenVertexArraysOESImmediateValidArgs();
   7866 }
   7867 
   7868 TEST_F(GLES2DecoderVertexArraysOESTest,
   7869     GenVertexArraysOESImmediateInvalidArgs) {
   7870   GenVertexArraysOESImmediateInvalidArgs();
   7871 }
   7872 TEST_F(GLES2DecoderEmulatedVertexArraysOESTest,
   7873     GenVertexArraysOESImmediateInvalidArgs) {
   7874   GenVertexArraysOESImmediateInvalidArgs();
   7875 }
   7876 
   7877 TEST_F(GLES2DecoderVertexArraysOESTest, DeleteVertexArraysOESValidArgs) {
   7878   DeleteVertexArraysOESValidArgs();
   7879 }
   7880 TEST_F(GLES2DecoderEmulatedVertexArraysOESTest,
   7881     DeleteVertexArraysOESValidArgs) {
   7882   DeleteVertexArraysOESValidArgs();
   7883 }
   7884 
   7885 TEST_F(GLES2DecoderVertexArraysOESTest, DeleteVertexArraysOESInvalidArgs) {
   7886   DeleteVertexArraysOESInvalidArgs();
   7887 }
   7888 TEST_F(GLES2DecoderEmulatedVertexArraysOESTest,
   7889     DeleteVertexArraysOESInvalidArgs) {
   7890   DeleteVertexArraysOESInvalidArgs();
   7891 }
   7892 
   7893 TEST_F(GLES2DecoderVertexArraysOESTest,
   7894     DeleteVertexArraysOESImmediateValidArgs) {
   7895   DeleteVertexArraysOESImmediateValidArgs();
   7896 }
   7897 TEST_F(GLES2DecoderEmulatedVertexArraysOESTest,
   7898     DeleteVertexArraysOESImmediateValidArgs) {
   7899   DeleteVertexArraysOESImmediateValidArgs();
   7900 }
   7901 
   7902 TEST_F(GLES2DecoderVertexArraysOESTest,
   7903     DeleteVertexArraysOESImmediateInvalidArgs) {
   7904   DeleteVertexArraysOESImmediateInvalidArgs();
   7905 }
   7906 TEST_F(GLES2DecoderEmulatedVertexArraysOESTest,
   7907     DeleteVertexArraysOESImmediateInvalidArgs) {
   7908   DeleteVertexArraysOESImmediateInvalidArgs();
   7909 }
   7910 
   7911 TEST_F(GLES2DecoderVertexArraysOESTest, IsVertexArrayOESValidArgs) {
   7912   IsVertexArrayOESValidArgs();
   7913 }
   7914 TEST_F(GLES2DecoderEmulatedVertexArraysOESTest, IsVertexArrayOESValidArgs) {
   7915   IsVertexArrayOESValidArgs();
   7916 }
   7917 
   7918 TEST_F(GLES2DecoderVertexArraysOESTest,
   7919     IsVertexArrayOESInvalidArgsBadSharedMemoryId) {
   7920   IsVertexArrayOESInvalidArgsBadSharedMemoryId();
   7921 }
   7922 TEST_F(GLES2DecoderEmulatedVertexArraysOESTest,
   7923     IsVertexArrayOESInvalidArgsBadSharedMemoryId) {
   7924   IsVertexArrayOESInvalidArgsBadSharedMemoryId();
   7925 }
   7926 
   7927 TEST_F(GLES2DecoderVertexArraysOESTest, BindVertexArrayOESValidArgs) {
   7928   BindVertexArrayOESValidArgs();
   7929 }
   7930 TEST_F(GLES2DecoderEmulatedVertexArraysOESTest, BindVertexArrayOESValidArgs) {
   7931   BindVertexArrayOESValidArgs();
   7932 }
   7933 
   7934 TEST_F(GLES2DecoderVertexArraysOESTest, BindVertexArrayOESValidArgsNewId) {
   7935   BindVertexArrayOESValidArgsNewId();
   7936 }
   7937 TEST_F(GLES2DecoderEmulatedVertexArraysOESTest,
   7938     BindVertexArrayOESValidArgsNewId) {
   7939   BindVertexArrayOESValidArgsNewId();
   7940 }
   7941 
   7942 TEST_F(GLES2DecoderTest, BindTexImage2DCHROMIUM) {
   7943   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   7944   DoTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 3, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   7945                0, 0);
   7946   TextureRef* texture_ref = group().texture_manager()->GetTexture(
   7947       client_texture_id_);
   7948   ASSERT_TRUE(texture_ref != NULL);
   7949   Texture* texture = texture_ref->texture();
   7950   EXPECT_EQ(kServiceTextureId, texture->service_id());
   7951 
   7952   group().image_manager()->AddImage(gfx::GLImage::CreateGLImage(0).get(), 1);
   7953   EXPECT_FALSE(group().image_manager()->LookupImage(1) == NULL);
   7954 
   7955   GLsizei width;
   7956   GLsizei height;
   7957   GLenum type;
   7958   GLenum internal_format;
   7959 
   7960   EXPECT_TRUE(texture->GetLevelSize(GL_TEXTURE_2D, 0, &width, &height));
   7961   EXPECT_EQ(3, width);
   7962   EXPECT_EQ(1, height);
   7963   EXPECT_TRUE(texture->GetLevelType(GL_TEXTURE_2D, 0, &type, &internal_format));
   7964   EXPECT_EQ(static_cast<GLenum>(GL_RGBA), internal_format);
   7965   EXPECT_EQ(static_cast<GLenum>(GL_UNSIGNED_BYTE), type);
   7966   EXPECT_TRUE(texture->GetLevelImage(GL_TEXTURE_2D, 0) == NULL);
   7967 
   7968   // Bind image to texture.
   7969   // ScopedGLErrorSuppressor calls GetError on its constructor and destructor.
   7970   EXPECT_CALL(*gl_, GetError())
   7971       .WillOnce(Return(GL_NO_ERROR))
   7972       .WillOnce(Return(GL_NO_ERROR))
   7973       .RetiresOnSaturation();
   7974   BindTexImage2DCHROMIUM bind_tex_image_2d_cmd;
   7975   bind_tex_image_2d_cmd.Init(GL_TEXTURE_2D, 1);
   7976   EXPECT_EQ(error::kNoError, ExecuteCmd(bind_tex_image_2d_cmd));
   7977   EXPECT_TRUE(texture->GetLevelSize(GL_TEXTURE_2D, 0, &width, &height));
   7978   // Image should now be set.
   7979   EXPECT_FALSE(texture->GetLevelImage(GL_TEXTURE_2D, 0) == NULL);
   7980 
   7981   // Define new texture image.
   7982   DoTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 3, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   7983                0, 0);
   7984   EXPECT_TRUE(texture->GetLevelSize(GL_TEXTURE_2D, 0, &width, &height));
   7985   // Image should no longer be set.
   7986   EXPECT_TRUE(texture->GetLevelImage(GL_TEXTURE_2D, 0) == NULL);
   7987 }
   7988 
   7989 TEST_F(GLES2DecoderTest, BindTexImage2DCHROMIUMCubeMapNotAllowed) {
   7990   group().image_manager()->AddImage(gfx::GLImage::CreateGLImage(0).get(), 1);
   7991   DoBindTexture(GL_TEXTURE_CUBE_MAP, client_texture_id_, kServiceTextureId);
   7992 
   7993   BindTexImage2DCHROMIUM bind_tex_image_2d_cmd;
   7994   bind_tex_image_2d_cmd.Init(GL_TEXTURE_CUBE_MAP, 1);
   7995   EXPECT_EQ(error::kNoError, ExecuteCmd(bind_tex_image_2d_cmd));
   7996   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
   7997 }
   7998 
   7999 TEST_F(GLES2DecoderTest, BindTexImage2DCHROMIUMCubeMapNotAllowed) {
   8000   group().image_manager()->AddImage(gfx::GLImage::CreateGLImage(0).get(), 1);
   8001   DoBindTexture(GL_TEXTURE_CUBE_MAP, client_texture_id_, kServiceTextureId);
   8002 
   8003   BindTexImage2DCHROMIUM bind_tex_image_2d_cmd;
   8004   bind_tex_image_2d_cmd.Init(GL_TEXTURE_CUBE_MAP, 1);
   8005   EXPECT_EQ(error::kNoError, ExecuteCmd(bind_tex_image_2d_cmd));
   8006   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
   8007 }
   8008 
   8009 TEST_F(GLES2DecoderTest, OrphanGLImageWithTexImage2D) {
   8010   group().image_manager()->AddImage(gfx::GLImage::CreateGLImage(0).get(), 1);
   8011   DoBindTexture(GL_TEXTURE_CUBE_MAP, client_texture_id_, kServiceTextureId);
   8012 
   8013   BindTexImage2DCHROMIUM bind_tex_image_2d_cmd;
   8014   bind_tex_image_2d_cmd.Init(GL_TEXTURE_CUBE_MAP, 1);
   8015   EXPECT_EQ(error::kNoError, ExecuteCmd(bind_tex_image_2d_cmd));
   8016   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
   8017 
   8018   DoTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 3, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   8019                0, 0);
   8020   TextureRef* texture_ref = group().texture_manager()->GetTexture(
   8021       client_texture_id_);
   8022   ASSERT_TRUE(texture_ref != NULL);
   8023   Texture* texture = texture_ref->texture();
   8024   EXPECT_TRUE(texture->GetLevelImage(GL_TEXTURE_2D, 0) == NULL);
   8025 }
   8026 
   8027 TEST_F(GLES2DecoderTest, ReleaseTexImage2DCHROMIUM) {
   8028   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   8029   DoTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 3, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   8030                0, 0);
   8031   TextureRef* texture_ref = group().texture_manager()->GetTexture(
   8032       client_texture_id_);
   8033   ASSERT_TRUE(texture_ref != NULL);
   8034   Texture* texture = texture_ref->texture();
   8035   EXPECT_EQ(kServiceTextureId, texture->service_id());
   8036 
   8037   group().image_manager()->AddImage(gfx::GLImage::CreateGLImage(0).get(), 1);
   8038   EXPECT_FALSE(group().image_manager()->LookupImage(1) == NULL);
   8039 
   8040   GLsizei width;
   8041   GLsizei height;
   8042   GLenum type;
   8043   GLenum internal_format;
   8044 
   8045   EXPECT_TRUE(texture->GetLevelSize(GL_TEXTURE_2D, 0, &width, &height));
   8046   EXPECT_EQ(3, width);
   8047   EXPECT_EQ(1, height);
   8048   EXPECT_TRUE(texture->GetLevelType(GL_TEXTURE_2D, 0, &type, &internal_format));
   8049   EXPECT_EQ(static_cast<GLenum>(GL_RGBA), internal_format);
   8050   EXPECT_EQ(static_cast<GLenum>(GL_UNSIGNED_BYTE), type);
   8051   EXPECT_TRUE(texture->GetLevelImage(GL_TEXTURE_2D, 0) == NULL);
   8052 
   8053   // Bind image to texture.
   8054   // ScopedGLErrorSuppressor calls GetError on its constructor and destructor.
   8055   EXPECT_CALL(*gl_, GetError())
   8056       .WillOnce(Return(GL_NO_ERROR))
   8057       .WillOnce(Return(GL_NO_ERROR))
   8058       .RetiresOnSaturation();
   8059   BindTexImage2DCHROMIUM bind_tex_image_2d_cmd;
   8060   bind_tex_image_2d_cmd.Init(GL_TEXTURE_2D, 1);
   8061   EXPECT_EQ(error::kNoError, ExecuteCmd(bind_tex_image_2d_cmd));
   8062   EXPECT_TRUE(texture->GetLevelSize(GL_TEXTURE_2D, 0, &width, &height));
   8063   // Image should now be set.
   8064   EXPECT_FALSE(texture->GetLevelImage(GL_TEXTURE_2D, 0) == NULL);
   8065 
   8066   // Release image from texture.
   8067   // ScopedGLErrorSuppressor calls GetError on its constructor and destructor.
   8068   EXPECT_CALL(*gl_, GetError())
   8069       .WillOnce(Return(GL_NO_ERROR))
   8070       .WillOnce(Return(GL_NO_ERROR))
   8071       .RetiresOnSaturation();
   8072   ReleaseTexImage2DCHROMIUM release_tex_image_2d_cmd;
   8073   release_tex_image_2d_cmd.Init(GL_TEXTURE_2D, 1);
   8074   EXPECT_EQ(error::kNoError, ExecuteCmd(release_tex_image_2d_cmd));
   8075   EXPECT_TRUE(texture->GetLevelSize(GL_TEXTURE_2D, 0, &width, &height));
   8076   // Image should no longer be set.
   8077   EXPECT_TRUE(texture->GetLevelImage(GL_TEXTURE_2D, 0) == NULL);
   8078 }
   8079 
   8080 class MockGLImage : public gfx::GLImage {
   8081  public:
   8082   MockGLImage() {}
   8083 
   8084   // Overridden from gfx::GLImage:
   8085   MOCK_METHOD0(Destroy, void());
   8086   MOCK_METHOD0(GetSize, gfx::Size());
   8087   MOCK_METHOD1(BindTexImage, bool(unsigned));
   8088   MOCK_METHOD1(ReleaseTexImage, void(unsigned));
   8089   MOCK_METHOD0(WillUseTexImage, void());
   8090   MOCK_METHOD0(DidUseTexImage, void());
   8091 
   8092  protected:
   8093   virtual ~MockGLImage() {}
   8094 };
   8095 
   8096 TEST_F(GLES2DecoderWithShaderTest, UseTexImage) {
   8097   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   8098   DoTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   8099                kSharedMemoryId, kSharedMemoryOffset);
   8100 
   8101   TextureRef* texture_ref = group().texture_manager()->GetTexture(
   8102       client_texture_id_);
   8103   ASSERT_TRUE(texture_ref != NULL);
   8104   Texture* texture = texture_ref->texture();
   8105   EXPECT_EQ(kServiceTextureId, texture->service_id());
   8106 
   8107   const int32 kImageId = 1;
   8108   scoped_refptr<MockGLImage> image(new MockGLImage);
   8109   group().image_manager()->AddImage(image.get(), kImageId);
   8110 
   8111   // Bind image to texture.
   8112   EXPECT_CALL(*image, BindTexImage(GL_TEXTURE_2D))
   8113       .Times(1)
   8114       .WillOnce(Return(true))
   8115       .RetiresOnSaturation();
   8116   EXPECT_CALL(*image, GetSize())
   8117       .Times(1)
   8118       .WillOnce(Return(gfx::Size(1, 1)))
   8119       .RetiresOnSaturation();
   8120   // ScopedGLErrorSuppressor calls GetError on its constructor and destructor.
   8121   EXPECT_CALL(*gl_, GetError())
   8122       .WillOnce(Return(GL_NO_ERROR))
   8123       .WillOnce(Return(GL_NO_ERROR))
   8124       .RetiresOnSaturation();
   8125   BindTexImage2DCHROMIUM bind_tex_image_2d_cmd;
   8126   bind_tex_image_2d_cmd.Init(GL_TEXTURE_2D, kImageId);
   8127   EXPECT_EQ(error::kNoError, ExecuteCmd(bind_tex_image_2d_cmd));
   8128 
   8129   AddExpectationsForSimulatedAttrib0(kNumVertices, 0);
   8130   SetupExpectationsForApplyingDefaultDirtyState();
   8131 
   8132   // ScopedGLErrorSuppressor calls GetError on its constructor and destructor.
   8133   EXPECT_CALL(*gl_, GetError())
   8134       .WillOnce(Return(GL_NO_ERROR))
   8135       .WillOnce(Return(GL_NO_ERROR))
   8136       .WillOnce(Return(GL_NO_ERROR))
   8137       .WillOnce(Return(GL_NO_ERROR))
   8138       .RetiresOnSaturation();
   8139   EXPECT_CALL(*gl_, ActiveTexture(GL_TEXTURE0))
   8140       .Times(3)
   8141       .RetiresOnSaturation();
   8142   EXPECT_CALL(*image, WillUseTexImage())
   8143       .Times(1)
   8144       .RetiresOnSaturation();
   8145   EXPECT_CALL(*image, DidUseTexImage())
   8146       .Times(1)
   8147       .RetiresOnSaturation();
   8148   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   8149       .Times(1)
   8150       .RetiresOnSaturation();
   8151   DrawArrays cmd;
   8152   cmd.Init(GL_TRIANGLES, 0, kNumVertices);
   8153   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   8154   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   8155 
   8156   DoBindFramebuffer(GL_FRAMEBUFFER, client_framebuffer_id_,
   8157                     kServiceFramebufferId);
   8158   // ScopedGLErrorSuppressor calls GetError on its constructor and destructor.
   8159   EXPECT_CALL(*gl_, GetError())
   8160       .WillOnce(Return(GL_NO_ERROR))
   8161       .WillOnce(Return(GL_NO_ERROR))
   8162       .RetiresOnSaturation();
   8163   EXPECT_CALL(*gl_, ActiveTexture(GL_TEXTURE0))
   8164       .Times(1)
   8165       .RetiresOnSaturation();
   8166   EXPECT_CALL(*gl_, BindTexture(GL_TEXTURE_2D, kServiceTextureId))
   8167       .Times(2)
   8168       .RetiresOnSaturation();
   8169   // Image will be 'in use' as long as bound to a framebuffer.
   8170   EXPECT_CALL(*image, WillUseTexImage())
   8171       .Times(1)
   8172       .RetiresOnSaturation();
   8173   EXPECT_CALL(*gl_, FramebufferTexture2DEXT(
   8174       GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
   8175       kServiceTextureId, 0))
   8176       .Times(1)
   8177       .RetiresOnSaturation();
   8178   EXPECT_CALL(*gl_, GetError())
   8179       .WillOnce(Return(GL_NO_ERROR))
   8180       .WillOnce(Return(GL_NO_ERROR))
   8181       .RetiresOnSaturation();
   8182   FramebufferTexture2D fbtex_cmd;
   8183   fbtex_cmd.Init(
   8184       GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, client_texture_id_,
   8185       0);
   8186   EXPECT_EQ(error::kNoError, ExecuteCmd(fbtex_cmd));
   8187   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   8188 
   8189   // ScopedGLErrorSuppressor calls GetError on its constructor and destructor.
   8190   EXPECT_CALL(*gl_, GetError())
   8191       .WillOnce(Return(GL_NO_ERROR))
   8192       .WillOnce(Return(GL_NO_ERROR))
   8193       .RetiresOnSaturation();
   8194   EXPECT_CALL(*gl_, FramebufferRenderbufferEXT(
   8195       GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER,
   8196       kServiceRenderbufferId))
   8197       .Times(1)
   8198       .RetiresOnSaturation();
   8199   EXPECT_CALL(*gl_, ActiveTexture(GL_TEXTURE0))
   8200       .Times(1)
   8201       .RetiresOnSaturation();
   8202   EXPECT_CALL(*gl_, BindTexture(GL_TEXTURE_2D, kServiceTextureId))
   8203       .Times(2)
   8204       .RetiresOnSaturation();
   8205   // Image should no longer be 'in use' after being unbound from framebuffer.
   8206   EXPECT_CALL(*image, DidUseTexImage())
   8207       .Times(1)
   8208       .RetiresOnSaturation();
   8209   EXPECT_CALL(*gl_, GetError())
   8210       .WillOnce(Return(GL_NO_ERROR))
   8211       .WillOnce(Return(GL_NO_ERROR))
   8212       .RetiresOnSaturation();
   8213   FramebufferRenderbuffer fbrb_cmd;
   8214   fbrb_cmd.Init(
   8215       GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER,
   8216       client_renderbuffer_id_);
   8217   EXPECT_EQ(error::kNoError, ExecuteCmd(fbrb_cmd));
   8218 }
   8219 
   8220 TEST_F(GLES2DecoderManualInitTest, GpuMemoryManagerCHROMIUM) {
   8221   InitDecoder(
   8222       "GL_ARB_texture_rectangle",  // extensions
   8223       false,   // has alpha
   8224       false,   // has depth
   8225       false,   // has stencil
   8226       false,   // request alpha
   8227       false,   // request depth
   8228       false,   // request stencil
   8229       true);   // bind generates resource
   8230 
   8231   Texture* texture = GetTexture(client_texture_id_)->texture();
   8232   EXPECT_TRUE(texture != NULL);
   8233   EXPECT_TRUE(texture->pool() == GL_TEXTURE_POOL_UNMANAGED_CHROMIUM);
   8234 
   8235   DoBindTexture(
   8236       GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   8237 
   8238   TexParameteri cmd;
   8239   cmd.Init(GL_TEXTURE_2D,
   8240            GL_TEXTURE_POOL_CHROMIUM,
   8241            GL_TEXTURE_POOL_UNMANAGED_CHROMIUM);
   8242   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   8243   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   8244 
   8245   cmd.Init(GL_TEXTURE_2D,
   8246            GL_TEXTURE_POOL_CHROMIUM,
   8247            GL_TEXTURE_POOL_MANAGED_CHROMIUM);
   8248   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   8249   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   8250 
   8251   EXPECT_TRUE(texture->pool() == GL_TEXTURE_POOL_MANAGED_CHROMIUM);
   8252 
   8253   cmd.Init(GL_TEXTURE_2D,
   8254            GL_TEXTURE_POOL_CHROMIUM,
   8255            GL_NONE);
   8256   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   8257   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
   8258 }
   8259 
   8260 TEST_F(GLES2DecoderManualInitTest, AsyncPixelTransfers) {
   8261   InitDecoder(
   8262       "GL_CHROMIUM_async_pixel_transfers",  // extensions
   8263       false, false, false,  // has alpha/depth/stencil
   8264       false, false, false,  // request alpha/depth/stencil
   8265       true);   // bind generates resource
   8266 
   8267   // Set up the texture.
   8268   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   8269   TextureRef* texture_ref = GetTexture(client_texture_id_);
   8270   Texture* texture = texture_ref->texture();
   8271 
   8272   // Set a mock Async delegate
   8273   StrictMock<gpu::MockAsyncPixelTransferManager>* manager =
   8274       new StrictMock<gpu::MockAsyncPixelTransferManager>;
   8275   manager->Initialize(group().texture_manager());
   8276   decoder_->SetAsyncPixelTransferManagerForTest(manager);
   8277   StrictMock<gpu::MockAsyncPixelTransferDelegate>* delegate = NULL;
   8278 
   8279   // Tex(Sub)Image2D upload commands.
   8280   AsyncTexImage2DCHROMIUM teximage_cmd;
   8281   teximage_cmd.Init(GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 0, GL_RGBA,
   8282                     GL_UNSIGNED_BYTE, kSharedMemoryId, kSharedMemoryOffset);
   8283   AsyncTexSubImage2DCHROMIUM texsubimage_cmd;
   8284   texsubimage_cmd.Init(GL_TEXTURE_2D, 0, 0, 0, 8, 8, GL_RGBA,
   8285                       GL_UNSIGNED_BYTE, kSharedMemoryId, kSharedMemoryOffset);
   8286   WaitAsyncTexImage2DCHROMIUM wait_cmd;
   8287   wait_cmd.Init(GL_TEXTURE_2D);
   8288 
   8289   // No transfer state exists initially.
   8290   EXPECT_FALSE(
   8291       decoder_->GetAsyncPixelTransferManager()->GetPixelTransferDelegate(
   8292           texture_ref));
   8293 
   8294   base::Closure bind_callback;
   8295 
   8296   // AsyncTexImage2D
   8297   {
   8298     // Create transfer state since it doesn't exist.
   8299     EXPECT_CALL(*manager, CreatePixelTransferDelegateImpl(texture_ref, _))
   8300         .WillOnce(Return(
   8301             delegate = new StrictMock<gpu::MockAsyncPixelTransferDelegate>))
   8302         .RetiresOnSaturation();
   8303     EXPECT_CALL(*delegate, AsyncTexImage2D(_, _, _))
   8304         .WillOnce(SaveArg<2>(&bind_callback))
   8305         .RetiresOnSaturation();
   8306     // Command succeeds.
   8307     EXPECT_EQ(error::kNoError, ExecuteCmd(teximage_cmd));
   8308     EXPECT_EQ(GL_NO_ERROR, GetGLError());
   8309     EXPECT_EQ(
   8310         delegate,
   8311         decoder_->GetAsyncPixelTransferManager()->GetPixelTransferDelegate(
   8312             texture_ref));
   8313     EXPECT_TRUE(texture->IsImmutable());
   8314     // The texture is safe but the level has not been defined yet.
   8315     EXPECT_TRUE(texture->SafeToRenderFrom());
   8316     GLsizei width, height;
   8317     EXPECT_FALSE(texture->GetLevelSize(GL_TEXTURE_2D, 0, &width, &height));
   8318   }
   8319   {
   8320     // Async redefinitions are not allowed!
   8321     // Command fails.
   8322     EXPECT_EQ(error::kNoError, ExecuteCmd(teximage_cmd));
   8323     EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   8324     EXPECT_EQ(
   8325         delegate,
   8326         decoder_->GetAsyncPixelTransferManager()->GetPixelTransferDelegate(
   8327             texture_ref));
   8328     EXPECT_TRUE(texture->IsImmutable());
   8329     EXPECT_TRUE(texture->SafeToRenderFrom());
   8330   }
   8331 
   8332   // Binding/defining of the async transfer
   8333   {
   8334     // TODO(epenner): We should check that the manager gets the
   8335     // BindCompletedAsyncTransfers() call, which is required to
   8336     // guarantee the delegate calls the bind callback.
   8337 
   8338     // Simulate the bind callback from the delegate.
   8339     bind_callback.Run();
   8340 
   8341     // After the bind callback is run, the texture is safe,
   8342     // and has the right size etc.
   8343     EXPECT_TRUE(texture->SafeToRenderFrom());
   8344     GLsizei width, height;
   8345     EXPECT_TRUE(texture->GetLevelSize(GL_TEXTURE_2D, 0, &width, &height));
   8346     EXPECT_EQ(width, 8);
   8347     EXPECT_EQ(height, 8);
   8348   }
   8349 
   8350   // AsyncTexSubImage2D
   8351   decoder_->GetAsyncPixelTransferManager()
   8352       ->ClearPixelTransferDelegateForTest(texture_ref);
   8353   texture->SetImmutable(false);
   8354   {
   8355     // Create transfer state since it doesn't exist.
   8356     EXPECT_CALL(*manager, CreatePixelTransferDelegateImpl(texture_ref, _))
   8357         .WillOnce(Return(
   8358             delegate = new StrictMock<gpu::MockAsyncPixelTransferDelegate>))
   8359         .RetiresOnSaturation();
   8360     EXPECT_CALL(*delegate, AsyncTexSubImage2D(_, _))
   8361         .RetiresOnSaturation();
   8362     // Command succeeds.
   8363     EXPECT_EQ(error::kNoError, ExecuteCmd(texsubimage_cmd));
   8364     EXPECT_EQ(GL_NO_ERROR, GetGLError());
   8365     EXPECT_EQ(
   8366         delegate,
   8367         decoder_->GetAsyncPixelTransferManager()->GetPixelTransferDelegate(
   8368             texture_ref));
   8369     EXPECT_TRUE(texture->IsImmutable());
   8370     EXPECT_TRUE(texture->SafeToRenderFrom());
   8371   }
   8372   {
   8373     // No transfer is in progress.
   8374     EXPECT_CALL(*delegate, TransferIsInProgress())
   8375         .WillOnce(Return(false))  // texSubImage validation
   8376         .WillOnce(Return(false))  // async validation
   8377         .RetiresOnSaturation();
   8378     EXPECT_CALL(*delegate, AsyncTexSubImage2D(_, _))
   8379         .RetiresOnSaturation();
   8380     // Command succeeds.
   8381     EXPECT_EQ(error::kNoError, ExecuteCmd(texsubimage_cmd));
   8382     EXPECT_EQ(GL_NO_ERROR, GetGLError());
   8383     EXPECT_EQ(
   8384         delegate,
   8385         decoder_->GetAsyncPixelTransferManager()->GetPixelTransferDelegate(
   8386             texture_ref));
   8387     EXPECT_TRUE(texture->IsImmutable());
   8388     EXPECT_TRUE(texture->SafeToRenderFrom());
   8389   }
   8390   {
   8391     // A transfer is still in progress!
   8392     EXPECT_CALL(*delegate, TransferIsInProgress())
   8393         .WillOnce(Return(true))
   8394         .RetiresOnSaturation();
   8395     // No async call, command fails.
   8396     EXPECT_EQ(error::kNoError, ExecuteCmd(texsubimage_cmd));
   8397     EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   8398     EXPECT_EQ(
   8399         delegate,
   8400         decoder_->GetAsyncPixelTransferManager()->GetPixelTransferDelegate(
   8401             texture_ref));
   8402     EXPECT_TRUE(texture->IsImmutable());
   8403     EXPECT_TRUE(texture->SafeToRenderFrom());
   8404   }
   8405 
   8406   // Delete delegate on DeleteTexture.
   8407   {
   8408     EXPECT_CALL(*delegate, Destroy()).RetiresOnSaturation();
   8409     DoDeleteTexture(client_texture_id_, kServiceTextureId);
   8410     EXPECT_FALSE(
   8411         decoder_->GetAsyncPixelTransferManager()->GetPixelTransferDelegate(
   8412             texture_ref));
   8413     delegate = NULL;
   8414   }
   8415 
   8416   // WaitAsyncTexImage2D
   8417   {
   8418     // Get a fresh texture since the existing texture cannot be respecified
   8419     // asynchronously and AsyncTexSubImage2D does not involved binding.
   8420     EXPECT_CALL(*gl_, GenTextures(1, _))
   8421         .WillOnce(SetArgumentPointee<1>(kServiceTextureId));
   8422     DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   8423     texture_ref = GetTexture(client_texture_id_);
   8424     texture = texture_ref->texture();
   8425     texture->SetImmutable(false);
   8426     // Create transfer state since it doesn't exist.
   8427     EXPECT_CALL(*manager, CreatePixelTransferDelegateImpl(texture_ref, _))
   8428         .WillOnce(Return(
   8429             delegate = new StrictMock<gpu::MockAsyncPixelTransferDelegate>))
   8430         .RetiresOnSaturation();
   8431     EXPECT_CALL(*delegate, AsyncTexImage2D(_, _, _))
   8432         .RetiresOnSaturation();
   8433     // Start async transfer.
   8434     EXPECT_EQ(error::kNoError, ExecuteCmd(teximage_cmd));
   8435     EXPECT_EQ(GL_NO_ERROR, GetGLError());
   8436     EXPECT_EQ(
   8437         delegate,
   8438         decoder_->GetAsyncPixelTransferManager()->GetPixelTransferDelegate(
   8439             texture_ref));
   8440 
   8441     EXPECT_TRUE(texture->IsImmutable());
   8442     // Wait for completion.
   8443     EXPECT_CALL(*delegate, WaitForTransferCompletion());
   8444     EXPECT_CALL(*manager, BindCompletedAsyncTransfers());
   8445     EXPECT_EQ(error::kNoError, ExecuteCmd(wait_cmd));
   8446     EXPECT_EQ(GL_NO_ERROR, GetGLError());
   8447   }
   8448 }
   8449 
   8450 TEST_F(GLES2DecoderManualInitTest, AsyncPixelTransferManager) {
   8451   InitDecoder(
   8452       "GL_CHROMIUM_async_pixel_transfers",  // extensions
   8453       false, false, false,  // has alpha/depth/stencil
   8454       false, false, false,  // request alpha/depth/stencil
   8455       true);   // bind generates resource
   8456 
   8457   // Set up the texture.
   8458   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   8459   TextureRef* texture_ref = GetTexture(client_texture_id_);
   8460 
   8461   // Set a mock Async delegate.
   8462   StrictMock<gpu::MockAsyncPixelTransferManager>* manager =
   8463       new StrictMock<gpu::MockAsyncPixelTransferManager>;
   8464   manager->Initialize(group().texture_manager());
   8465   decoder_->SetAsyncPixelTransferManagerForTest(manager);
   8466   StrictMock<gpu::MockAsyncPixelTransferDelegate>* delegate = NULL;
   8467 
   8468   AsyncTexImage2DCHROMIUM teximage_cmd;
   8469   teximage_cmd.Init(GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 0, GL_RGBA,
   8470                     GL_UNSIGNED_BYTE, kSharedMemoryId, kSharedMemoryOffset);
   8471 
   8472   // No transfer delegate exists initially.
   8473   EXPECT_FALSE(
   8474       decoder_->GetAsyncPixelTransferManager()->GetPixelTransferDelegate(
   8475           texture_ref));
   8476 
   8477   // Create delegate on AsyncTexImage2D.
   8478   {
   8479     EXPECT_CALL(*manager, CreatePixelTransferDelegateImpl(texture_ref, _))
   8480         .WillOnce(Return(
   8481              delegate = new StrictMock<gpu::MockAsyncPixelTransferDelegate>))
   8482         .RetiresOnSaturation();
   8483     EXPECT_CALL(*delegate, AsyncTexImage2D(_, _, _)).RetiresOnSaturation();
   8484 
   8485     // Command succeeds.
   8486     EXPECT_EQ(error::kNoError, ExecuteCmd(teximage_cmd));
   8487     EXPECT_EQ(GL_NO_ERROR, GetGLError());
   8488   }
   8489 
   8490   // Delegate is cached.
   8491   EXPECT_EQ(delegate,
   8492             decoder_->GetAsyncPixelTransferManager()->GetPixelTransferDelegate(
   8493                 texture_ref));
   8494 
   8495   // Delete delegate on manager teardown.
   8496   {
   8497     EXPECT_CALL(*delegate, Destroy()).RetiresOnSaturation();
   8498     decoder_->ResetAsyncPixelTransferManagerForTest();
   8499 
   8500     // Texture ref still valid.
   8501     EXPECT_EQ(texture_ref, GetTexture(client_texture_id_));
   8502   }
   8503 }
   8504 
   8505 namespace {
   8506 
   8507 class SizeOnlyMemoryTracker : public MemoryTracker {
   8508  public:
   8509   SizeOnlyMemoryTracker() {
   8510     // These are the default textures. 1 for TEXTURE_2D and 6 faces for
   8511     // TEXTURE_CUBE_MAP.
   8512     const size_t kInitialUnmanagedPoolSize = 7 * 4;
   8513     const size_t kInitialManagedPoolSize = 0;
   8514     pool_infos_[MemoryTracker::kUnmanaged].initial_size =
   8515         kInitialUnmanagedPoolSize;
   8516     pool_infos_[MemoryTracker::kManaged].initial_size =
   8517         kInitialManagedPoolSize;
   8518   }
   8519 
   8520   // Ensure a certain amount of GPU memory is free. Returns true on success.
   8521   MOCK_METHOD1(EnsureGPUMemoryAvailable, bool(size_t size_needed));
   8522 
   8523   virtual void TrackMemoryAllocatedChange(
   8524       size_t old_size, size_t new_size, Pool pool) {
   8525     PoolInfo& info = pool_infos_[pool];
   8526     info.size += new_size - old_size;
   8527   }
   8528 
   8529   size_t GetPoolSize(Pool pool) {
   8530     const PoolInfo& info = pool_infos_[pool];
   8531     return info.size - info.initial_size;
   8532   }
   8533 
   8534  private:
   8535   virtual ~SizeOnlyMemoryTracker() {
   8536   }
   8537   struct PoolInfo {
   8538     PoolInfo()
   8539         : initial_size(0),
   8540           size(0) {
   8541     }
   8542     size_t initial_size;
   8543     size_t size;
   8544   };
   8545   std::map<Pool, PoolInfo> pool_infos_;
   8546 };
   8547 
   8548 }  // anonymous namespace.
   8549 
   8550 TEST_F(GLES2DecoderManualInitTest, MemoryTrackerInitialSize) {
   8551   scoped_refptr<SizeOnlyMemoryTracker> memory_tracker =
   8552       new SizeOnlyMemoryTracker();
   8553   set_memory_tracker(memory_tracker.get());
   8554   InitDecoder(
   8555       "",      // extensions
   8556       false,   // has alpha
   8557       false,   // has depth
   8558       false,   // has stencil
   8559       false,   // request alpha
   8560       false,   // request depth
   8561       false,   // request stencil
   8562       true);   // bind generates resource
   8563   // Expect that initial size - size is 0.
   8564   EXPECT_EQ(0u, memory_tracker->GetPoolSize(MemoryTracker::kUnmanaged));
   8565   EXPECT_EQ(0u, memory_tracker->GetPoolSize(MemoryTracker::kManaged));
   8566 }
   8567 
   8568 TEST_F(GLES2DecoderManualInitTest, MemoryTrackerTexImage2D) {
   8569   scoped_refptr<SizeOnlyMemoryTracker> memory_tracker =
   8570       new SizeOnlyMemoryTracker();
   8571   set_memory_tracker(memory_tracker.get());
   8572   InitDecoder(
   8573       "",      // extensions
   8574       false,   // has alpha
   8575       false,   // has depth
   8576       false,   // has stencil
   8577       false,   // request alpha
   8578       false,   // request depth
   8579       false,   // request stencil
   8580       true);   // bind generates resource
   8581   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   8582   EXPECT_CALL(*memory_tracker.get(), EnsureGPUMemoryAvailable(128))
   8583       .WillOnce(Return(true)).RetiresOnSaturation();
   8584   DoTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 8, 4, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   8585                kSharedMemoryId, kSharedMemoryOffset);
   8586   EXPECT_EQ(128u, memory_tracker->GetPoolSize(MemoryTracker::kUnmanaged));
   8587   EXPECT_CALL(*memory_tracker.get(), EnsureGPUMemoryAvailable(64))
   8588       .WillOnce(Return(true)).RetiresOnSaturation();
   8589   DoTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 4, 4, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   8590                kSharedMemoryId, kSharedMemoryOffset);
   8591   EXPECT_EQ(64u, memory_tracker->GetPoolSize(MemoryTracker::kUnmanaged));
   8592   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   8593   // Check we get out of memory and no call to glTexImage2D if Ensure fails.
   8594   EXPECT_CALL(*memory_tracker.get(), EnsureGPUMemoryAvailable(64))
   8595       .WillOnce(Return(false)).RetiresOnSaturation();
   8596   TexImage2D cmd;
   8597   cmd.Init(GL_TEXTURE_2D, 0, GL_RGBA, 4, 4, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   8598            kSharedMemoryId, kSharedMemoryOffset);
   8599   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   8600   EXPECT_EQ(GL_OUT_OF_MEMORY, GetGLError());
   8601   EXPECT_EQ(64u, memory_tracker->GetPoolSize(MemoryTracker::kUnmanaged));
   8602 }
   8603 
   8604 TEST_F(GLES2DecoderManualInitTest, MemoryTrackerTexStorage2DEXT) {
   8605   scoped_refptr<SizeOnlyMemoryTracker> memory_tracker =
   8606       new SizeOnlyMemoryTracker();
   8607   set_memory_tracker(memory_tracker.get());
   8608   InitDecoder(
   8609       "",      // extensions
   8610       false,   // has alpha
   8611       false,   // has depth
   8612       false,   // has stencil
   8613       false,   // request alpha
   8614       false,   // request depth
   8615       false,   // request stencil
   8616       true);   // bind generates resource
   8617   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   8618   // Check we get out of memory and no call to glTexStorage2DEXT
   8619   // if Ensure fails.
   8620   EXPECT_CALL(*memory_tracker.get(), EnsureGPUMemoryAvailable(128))
   8621       .WillOnce(Return(false)).RetiresOnSaturation();
   8622   TexStorage2DEXT cmd;
   8623   cmd.Init(GL_TEXTURE_2D, 1, GL_RGBA8, 8, 4);
   8624   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   8625   EXPECT_EQ(0u, memory_tracker->GetPoolSize(MemoryTracker::kUnmanaged));
   8626   EXPECT_EQ(GL_OUT_OF_MEMORY, GetGLError());
   8627 }
   8628 
   8629 TEST_F(GLES2DecoderManualInitTest, MemoryTrackerCopyTexImage2D) {
   8630   GLenum target = GL_TEXTURE_2D;
   8631   GLint level = 0;
   8632   GLenum internal_format = GL_RGBA;
   8633   GLsizei width = 4;
   8634   GLsizei height = 8;
   8635   GLint border = 0;
   8636   scoped_refptr<SizeOnlyMemoryTracker> memory_tracker =
   8637       new SizeOnlyMemoryTracker();
   8638   set_memory_tracker(memory_tracker.get());
   8639   InitDecoder(
   8640       "",      // extensions
   8641       true,    // has alpha
   8642       false,   // has depth
   8643       false,   // has stencil
   8644       true,    // request alpha
   8645       false,   // request depth
   8646       false,   // request stencil
   8647       true);   // bind generates resource
   8648   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   8649   EXPECT_CALL(*memory_tracker.get(), EnsureGPUMemoryAvailable(128))
   8650       .WillOnce(Return(true)).RetiresOnSaturation();
   8651   EXPECT_CALL(*gl_, GetError())
   8652       .WillOnce(Return(GL_NO_ERROR))
   8653       .WillOnce(Return(GL_NO_ERROR))
   8654       .RetiresOnSaturation();
   8655   EXPECT_CALL(*gl_, CopyTexImage2D(
   8656       target, level, internal_format, 0, 0, width, height, border))
   8657       .Times(1)
   8658       .RetiresOnSaturation();
   8659   CopyTexImage2D cmd;
   8660   cmd.Init(target, level, internal_format, 0, 0, width, height, border);
   8661   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   8662   EXPECT_EQ(128u, memory_tracker->GetPoolSize(MemoryTracker::kUnmanaged));
   8663   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   8664   // Check we get out of memory and no call to glCopyTexImage2D if Ensure fails.
   8665   EXPECT_CALL(*memory_tracker.get(), EnsureGPUMemoryAvailable(128))
   8666       .WillOnce(Return(false)).RetiresOnSaturation();
   8667   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   8668   EXPECT_EQ(GL_OUT_OF_MEMORY, GetGLError());
   8669   EXPECT_EQ(128u, memory_tracker->GetPoolSize(MemoryTracker::kUnmanaged));
   8670 }
   8671 
   8672 TEST_F(GLES2DecoderManualInitTest, MemoryTrackerRenderbufferStorage) {
   8673   scoped_refptr<SizeOnlyMemoryTracker> memory_tracker =
   8674       new SizeOnlyMemoryTracker();
   8675   set_memory_tracker(memory_tracker.get());
   8676   InitDecoder(
   8677       "",      // extensions
   8678       false,   // has alpha
   8679       false,   // has depth
   8680       false,   // has stencil
   8681       false,   // request alpha
   8682       false,   // request depth
   8683       false,   // request stencil
   8684       true);   // bind generates resource
   8685   DoBindRenderbuffer(GL_RENDERBUFFER, client_renderbuffer_id_,
   8686                     kServiceRenderbufferId);
   8687   EXPECT_CALL(*gl_, GetError())
   8688       .WillOnce(Return(GL_NO_ERROR))
   8689       .WillOnce(Return(GL_NO_ERROR))
   8690       .RetiresOnSaturation();
   8691   EXPECT_CALL(*memory_tracker.get(), EnsureGPUMemoryAvailable(128))
   8692       .WillOnce(Return(true)).RetiresOnSaturation();
   8693   EXPECT_CALL(*gl_, RenderbufferStorageEXT(
   8694       GL_RENDERBUFFER, GL_RGBA, 8, 4))
   8695       .Times(1)
   8696       .RetiresOnSaturation();
   8697   RenderbufferStorage cmd;
   8698   cmd.Init(GL_RENDERBUFFER, GL_RGBA4, 8, 4);
   8699   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   8700   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   8701   EXPECT_EQ(128u, memory_tracker->GetPoolSize(MemoryTracker::kUnmanaged));
   8702   // Check we get out of memory and no call to glRenderbufferStorage if Ensure
   8703   // fails.
   8704   EXPECT_CALL(*memory_tracker.get(), EnsureGPUMemoryAvailable(128))
   8705       .WillOnce(Return(false)).RetiresOnSaturation();
   8706   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   8707   EXPECT_EQ(GL_OUT_OF_MEMORY, GetGLError());
   8708   EXPECT_EQ(128u, memory_tracker->GetPoolSize(MemoryTracker::kUnmanaged));
   8709 }
   8710 
   8711 TEST_F(GLES2DecoderManualInitTest, MemoryTrackerBufferData) {
   8712   scoped_refptr<SizeOnlyMemoryTracker> memory_tracker =
   8713       new SizeOnlyMemoryTracker();
   8714   set_memory_tracker(memory_tracker.get());
   8715   InitDecoder(
   8716       "",      // extensions
   8717       false,   // has alpha
   8718       false,   // has depth
   8719       false,   // has stencil
   8720       false,   // request alpha
   8721       false,   // request depth
   8722       false,   // request stencil
   8723       true);   // bind generates resource
   8724   DoBindBuffer(GL_ARRAY_BUFFER, client_buffer_id_,
   8725                kServiceBufferId);
   8726   EXPECT_CALL(*gl_, GetError())
   8727       .WillOnce(Return(GL_NO_ERROR))
   8728       .WillOnce(Return(GL_NO_ERROR))
   8729       .RetiresOnSaturation();
   8730   EXPECT_CALL(*memory_tracker.get(), EnsureGPUMemoryAvailable(128))
   8731       .WillOnce(Return(true)).RetiresOnSaturation();
   8732   EXPECT_CALL(*gl_, BufferData(GL_ARRAY_BUFFER, 128, _, GL_STREAM_DRAW))
   8733       .Times(1)
   8734       .RetiresOnSaturation();
   8735   BufferData cmd;
   8736   cmd.Init(GL_ARRAY_BUFFER, 128, 0, 0, GL_STREAM_DRAW);
   8737   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   8738   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   8739   EXPECT_EQ(128u, memory_tracker->GetPoolSize(MemoryTracker::kManaged));
   8740   // Check we get out of memory and no call to glBufferData if Ensure
   8741   // fails.
   8742   EXPECT_CALL(*memory_tracker.get(), EnsureGPUMemoryAvailable(128))
   8743       .WillOnce(Return(false)).RetiresOnSaturation();
   8744   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   8745   EXPECT_EQ(GL_OUT_OF_MEMORY, GetGLError());
   8746   EXPECT_EQ(128u, memory_tracker->GetPoolSize(MemoryTracker::kManaged));
   8747 }
   8748 
   8749 TEST_F(GLES2DecoderTest, DrawBuffersEXTImmediateSuccceeds) {
   8750   const GLsizei count = 1;
   8751   const GLenum bufs[] = { GL_COLOR_ATTACHMENT0 };
   8752   DrawBuffersEXTImmediate& cmd =
   8753       *GetImmediateAs<DrawBuffersEXTImmediate>();
   8754   cmd.Init(count, bufs);
   8755 
   8756   DoBindFramebuffer(GL_FRAMEBUFFER, client_framebuffer_id_,
   8757                     kServiceFramebufferId);
   8758   EXPECT_CALL(*gl_, DrawBuffersARB(count, _))
   8759       .Times(1)
   8760       .RetiresOnSaturation();
   8761   EXPECT_EQ(error::kNoError,
   8762             ExecuteImmediateCmd(cmd, sizeof(bufs)));
   8763   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   8764 }
   8765 
   8766 TEST_F(GLES2DecoderTest, DrawBuffersEXTImmediateFails) {
   8767   const GLsizei count = 1;
   8768   const GLenum bufs[] = { GL_COLOR_ATTACHMENT1_EXT };
   8769   DrawBuffersEXTImmediate& cmd =
   8770       *GetImmediateAs<DrawBuffersEXTImmediate>();
   8771   cmd.Init(count, bufs);
   8772 
   8773   DoBindFramebuffer(GL_FRAMEBUFFER, client_framebuffer_id_,
   8774                     kServiceFramebufferId);
   8775   EXPECT_EQ(error::kNoError,
   8776             ExecuteImmediateCmd(cmd, sizeof(bufs)));
   8777   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   8778 }
   8779 
   8780 TEST_F(GLES2DecoderTest, DrawBuffersEXTImmediateBackbuffer) {
   8781   const GLsizei count = 1;
   8782   const GLenum bufs[] = { GL_BACK };
   8783   DrawBuffersEXTImmediate& cmd =
   8784       *GetImmediateAs<DrawBuffersEXTImmediate>();
   8785   cmd.Init(count, bufs);
   8786 
   8787   DoBindFramebuffer(GL_FRAMEBUFFER, client_framebuffer_id_,
   8788                     kServiceFramebufferId);
   8789   EXPECT_EQ(error::kNoError,
   8790             ExecuteImmediateCmd(cmd, sizeof(bufs)));
   8791   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   8792 
   8793   DoBindFramebuffer(GL_FRAMEBUFFER, 0, 0);  // unbind
   8794 
   8795   EXPECT_CALL(*gl_, DrawBuffersARB(count, _))
   8796       .Times(1)
   8797       .RetiresOnSaturation();
   8798 
   8799   EXPECT_EQ(error::kNoError,
   8800             ExecuteImmediateCmd(cmd, sizeof(bufs)));
   8801   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   8802 }
   8803 
   8804 TEST_F(GLES2DecoderManualInitTest, DiscardFramebufferEXT) {
   8805   InitDecoder("GL_EXT_discard_framebuffer",  // extensions
   8806               false,                         // has alpha
   8807               false,                         // has depth
   8808               false,                         // has stencil
   8809               false,                         // request alpha
   8810               false,                         // request depth
   8811               false,                         // request stencil
   8812               false);                        // bind generates resource
   8813 
   8814   const GLenum target = GL_FRAMEBUFFER;
   8815   const GLsizei count = 1;
   8816   const GLenum attachments[] = { GL_COLOR_ATTACHMENT0 };
   8817 
   8818   SetupTexture();
   8819   DoBindFramebuffer(
   8820       GL_FRAMEBUFFER, client_framebuffer_id_, kServiceFramebufferId);
   8821   DoFramebufferTexture2D(GL_FRAMEBUFFER,
   8822                          GL_COLOR_ATTACHMENT0,
   8823                          GL_TEXTURE_2D,
   8824                          client_texture_id_,
   8825                          kServiceTextureId,
   8826                          0,
   8827                          GL_NO_ERROR);
   8828   FramebufferManager* framebuffer_manager = group().framebuffer_manager();
   8829   Framebuffer* framebuffer =
   8830       framebuffer_manager->GetFramebuffer(client_framebuffer_id_);
   8831   EXPECT_TRUE(framebuffer->IsCleared());
   8832 
   8833   EXPECT_CALL(*gl_, DiscardFramebufferEXT(target, count, _))
   8834       .Times(1)
   8835       .RetiresOnSaturation();
   8836   DiscardFramebufferEXTImmediate& cmd =
   8837       *GetImmediateAs<DiscardFramebufferEXTImmediate>();
   8838   cmd.Init(target, count, attachments);
   8839 
   8840   EXPECT_EQ(error::kNoError,
   8841             ExecuteImmediateCmd(cmd, sizeof(attachments)));
   8842   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   8843   EXPECT_FALSE(framebuffer->IsCleared());
   8844 }
   8845 
   8846 TEST_F(GLES2DecoderTest, DiscardFramebufferEXTUnsupported) {
   8847   const GLenum target = GL_FRAMEBUFFER;
   8848   const GLsizei count = 1;
   8849   const GLenum attachments[] = { GL_COLOR_EXT };
   8850   DiscardFramebufferEXTImmediate& cmd =
   8851       *GetImmediateAs<DiscardFramebufferEXTImmediate>();
   8852   cmd.Init(target, count, attachments);
   8853 
   8854   // Should not result into a call into GL.
   8855   EXPECT_EQ(error::kNoError,
   8856             ExecuteImmediateCmd(cmd, sizeof(attachments)));
   8857   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   8858 }
   8859 
   8860 TEST_F(GLES2DecoderManualInitTest, ClearUniformsBeforeFirstProgramUse) {
   8861   CommandLine command_line(0, NULL);
   8862   command_line.AppendSwitchASCII(
   8863       switches::kGpuDriverBugWorkarounds,
   8864       base::IntToString(gpu::CLEAR_UNIFORMS_BEFORE_FIRST_PROGRAM_USE));
   8865   InitDecoderWithCommandLine(
   8866       "",      // extensions
   8867       true,    // has alpha
   8868       false,   // has depth
   8869       false,   // has stencil
   8870       true,    // request alpha
   8871       false,   // request depth
   8872       false,   // request stencil
   8873       true,    // bind generates resource
   8874       &command_line);
   8875   {
   8876     static AttribInfo attribs[] = {
   8877       { kAttrib1Name, kAttrib1Size, kAttrib1Type, kAttrib1Location, },
   8878       { kAttrib2Name, kAttrib2Size, kAttrib2Type, kAttrib2Location, },
   8879       { kAttrib3Name, kAttrib3Size, kAttrib3Type, kAttrib3Location, },
   8880     };
   8881     static UniformInfo uniforms[] = {
   8882       { kUniform1Name, kUniform1Size, kUniform1Type,
   8883         kUniform1FakeLocation, kUniform1RealLocation,
   8884         kUniform1DesiredLocation },
   8885       { kUniform2Name, kUniform2Size, kUniform2Type,
   8886         kUniform2FakeLocation, kUniform2RealLocation,
   8887         kUniform2DesiredLocation },
   8888       { kUniform3Name, kUniform3Size, kUniform3Type,
   8889         kUniform3FakeLocation, kUniform3RealLocation,
   8890         kUniform3DesiredLocation },
   8891     };
   8892     SetupShader(attribs, arraysize(attribs), uniforms, arraysize(uniforms),
   8893                 client_program_id_, kServiceProgramId,
   8894                 client_vertex_shader_id_, kServiceVertexShaderId,
   8895                 client_fragment_shader_id_, kServiceFragmentShaderId);
   8896     TestHelper::SetupExpectationsForClearingUniforms(
   8897         gl_.get(), uniforms, arraysize(uniforms));
   8898   }
   8899 
   8900   {
   8901     EXPECT_CALL(*gl_, UseProgram(kServiceProgramId))
   8902         .Times(1)
   8903         .RetiresOnSaturation();
   8904     cmds::UseProgram cmd;
   8905     cmd.Init(client_program_id_);
   8906     EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   8907   }
   8908 }
   8909 
   8910 // TODO(gman): Complete this test.
   8911 // TEST_F(GLES2DecoderTest, CompressedTexImage2DGLError) {
   8912 // }
   8913 
   8914 // TODO(gman): BufferData
   8915 
   8916 // TODO(gman): BufferDataImmediate
   8917 
   8918 // TODO(gman): BufferSubData
   8919 
   8920 // TODO(gman): BufferSubDataImmediate
   8921 
   8922 // TODO(gman): CompressedTexImage2D
   8923 
   8924 // TODO(gman): CompressedTexImage2DImmediate
   8925 
   8926 // TODO(gman): CompressedTexSubImage2DImmediate
   8927 
   8928 // TODO(gman): DeleteProgram
   8929 
   8930 // TODO(gman): DeleteShader
   8931 
   8932 // TODO(gman): PixelStorei
   8933 
   8934 // TODO(gman): TexImage2D
   8935 
   8936 // TODO(gman): TexImage2DImmediate
   8937 
   8938 // TODO(gman): TexSubImage2DImmediate
   8939 
   8940 // TODO(gman): UseProgram
   8941 
   8942 // TODO(gman): SwapBuffers
   8943 
   8944 }  // namespace gles2
   8945 }  // namespace gpu
   8946