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