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(stream_texture, Update())
   5763       .Times(1)
   5764       .RetiresOnSaturation();
   5765   EXPECT_CALL(*gl_, DrawElements(_, _, _, _))
   5766       .Times(1);
   5767   DrawElements cmd;
   5768   cmd.Init(GL_TRIANGLES, kValidIndexRangeCount, GL_UNSIGNED_SHORT,
   5769            kValidIndexRangeStart * 2);
   5770   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   5771   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   5772 
   5773   EXPECT_CALL(*stream_texture_manager(),
   5774               DestroyStreamTexture(kServiceTextureId))
   5775       .Times(1)
   5776       .RetiresOnSaturation();
   5777 }
   5778 
   5779 TEST_F(GLES2DecoderManualInitTest, BindStreamTextureCHROMIUMInvalid) {
   5780   InitDecoder(
   5781       "GL_CHROMIUM_stream_texture",  // extensions
   5782       false,   // has alpha
   5783       false,   // has depth
   5784       false,   // has stencil
   5785       false,   // request alpha
   5786       false,   // request depth
   5787       false,   // request stencil
   5788       true);   // bind generates resource
   5789 
   5790   TextureRef* texture_ref = GetTexture(client_texture_id_);
   5791   group().texture_manager()->SetStreamTexture(texture_ref, true);
   5792 
   5793   BindTexture cmd;
   5794   cmd.Init(GL_TEXTURE_2D, client_texture_id_);
   5795   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   5796   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   5797 
   5798   BindTexture cmd2;
   5799   cmd2.Init(GL_TEXTURE_CUBE_MAP, client_texture_id_);
   5800   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd2));
   5801   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   5802 
   5803   EXPECT_CALL(*stream_texture_manager(),
   5804               DestroyStreamTexture(kServiceTextureId))
   5805       .Times(1)
   5806       .RetiresOnSaturation();
   5807 }
   5808 
   5809 TEST_F(GLES2DecoderManualInitTest, DestroyStreamTextureCHROMIUM) {
   5810   InitDecoder(
   5811       "GL_CHROMIUM_stream_texture",  // extensions
   5812       false,   // has alpha
   5813       false,   // has depth
   5814       false,   // has stencil
   5815       false,   // request alpha
   5816       false,   // request depth
   5817       false,   // request stencil
   5818       true);   // bind generates resource
   5819 
   5820   TextureRef* texture_ref = GetTexture(client_texture_id_);
   5821   group().texture_manager()->SetStreamTexture(texture_ref, true);
   5822 
   5823   EXPECT_CALL(*stream_texture_manager(),
   5824               DestroyStreamTexture(kServiceTextureId))
   5825       .Times(1)
   5826       .RetiresOnSaturation();
   5827 
   5828   DestroyStreamTextureCHROMIUM cmd;
   5829   cmd.Init(client_texture_id_);
   5830   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   5831   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   5832   EXPECT_FALSE(texture_ref->texture()->IsStreamTexture());
   5833   EXPECT_EQ(0U, texture_ref->texture()->target());
   5834 }
   5835 
   5836 TEST_F(GLES2DecoderManualInitTest, DestroyStreamTextureCHROMIUMInvalid) {
   5837   InitDecoder(
   5838       "GL_CHROMIUM_stream_texture",  // extensions
   5839       false,   // has alpha
   5840       false,   // has depth
   5841       false,   // has stencil
   5842       false,   // request alpha
   5843       false,   // request depth
   5844       false,   // request stencil
   5845       true);   // bind generates resource
   5846 
   5847   DestroyStreamTextureCHROMIUM cmd;
   5848   cmd.Init(client_texture_id_);
   5849   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   5850   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   5851 }
   5852 
   5853 TEST_F(GLES2DecoderManualInitTest, DestroyStreamTextureCHROMIUMBadId) {
   5854   InitDecoder(
   5855       "GL_CHROMIUM_stream_texture",  // extensions
   5856       false,   // has alpha
   5857       false,   // has depth
   5858       false,   // has stencil
   5859       false,   // request alpha
   5860       false,   // request depth
   5861       false,   // request stencil
   5862       true);   // bind generates resource
   5863 
   5864   DestroyStreamTextureCHROMIUM cmd;
   5865   cmd.Init(GL_ZERO);
   5866   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   5867   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   5868 }
   5869 
   5870 TEST_F(GLES2DecoderManualInitTest, StreamTextureCHROMIUMNullMgr) {
   5871   InitDecoder(
   5872       "",  // extensions
   5873       false,   // has alpha
   5874       false,   // has depth
   5875       false,   // has stencil
   5876       false,   // request alpha
   5877       false,   // request depth
   5878       false,   // request stencil
   5879       true);   // bind generates resource
   5880 
   5881   CreateStreamTextureCHROMIUM cmd;
   5882   cmd.Init(client_texture_id_, shared_memory_id_, shared_memory_offset_);
   5883   EXPECT_EQ(error::kInvalidArguments, ExecuteCmd(cmd));
   5884   GetGLError(); // ignore internal error
   5885 
   5886   TextureRef* texture_ref = GetTexture(client_texture_id_);
   5887   group().texture_manager()->SetStreamTexture(texture_ref, true);
   5888 
   5889   DestroyStreamTextureCHROMIUM cmd2;
   5890   cmd2.Init(client_texture_id_);
   5891   EXPECT_EQ(error::kInvalidArguments, ExecuteCmd(cmd2));
   5892   GetGLError(); // ignore internal error
   5893 }
   5894 
   5895 TEST_F(GLES2DecoderManualInitTest, ReCreateStreamTextureCHROMIUM) {
   5896   const GLuint kObjectId = 123;
   5897   InitDecoder(
   5898       "GL_CHROMIUM_stream_texture GL_OES_EGL_image_external",  // extensions
   5899       false,   // has alpha
   5900       false,   // has depth
   5901       false,   // has stencil
   5902       false,   // request alpha
   5903       false,   // request depth
   5904       false,   // request stencil
   5905       true);   // bind generates resource
   5906 
   5907   EXPECT_CALL(*stream_texture_manager(),
   5908               DestroyStreamTexture(kServiceTextureId))
   5909       .Times(1)
   5910       .RetiresOnSaturation();
   5911   EXPECT_CALL(*stream_texture_manager(),
   5912               CreateStreamTexture(kServiceTextureId, client_texture_id_))
   5913       .WillOnce(Return(kObjectId))
   5914       .RetiresOnSaturation();
   5915 
   5916   TextureRef* texture_ref = GetTexture(client_texture_id_);
   5917   group().texture_manager()->SetStreamTexture(texture_ref, true);
   5918 
   5919   DoBindTexture(GL_TEXTURE_EXTERNAL_OES, client_texture_id_, kServiceTextureId);
   5920   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   5921 
   5922   DestroyStreamTextureCHROMIUM cmd;
   5923   cmd.Init(client_texture_id_);
   5924   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   5925   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   5926   EXPECT_FALSE(texture_ref->texture()->IsStreamTexture());
   5927 
   5928   CreateStreamTextureCHROMIUM cmd2;
   5929   cmd2.Init(client_texture_id_, shared_memory_id_, shared_memory_offset_);
   5930   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd2));
   5931   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   5932   EXPECT_TRUE(texture_ref->texture()->IsStreamTexture());
   5933 
   5934   EXPECT_CALL(*stream_texture_manager(),
   5935               DestroyStreamTexture(kServiceTextureId))
   5936       .Times(1)
   5937       .RetiresOnSaturation();
   5938 }
   5939 
   5940 TEST_F(GLES2DecoderManualInitTest, ProduceAndConsumeStreamTextureCHROMIUM) {
   5941   InitDecoder(
   5942       "GL_CHROMIUM_stream_texture GL_OES_EGL_image_external",  // extensions
   5943       false,   // has alpha
   5944       false,   // has depth
   5945       false,   // has stencil
   5946       false,   // request alpha
   5947       false,   // request depth
   5948       false,   // request stencil
   5949       true);   // bind generates resource
   5950 
   5951   TextureRef* texture_ref = GetTexture(client_texture_id_);
   5952   group().texture_manager()->SetStreamTexture(texture_ref, true);
   5953 
   5954   DoBindTexture(GL_TEXTURE_EXTERNAL_OES, client_texture_id_, kServiceTextureId);
   5955 
   5956   GLbyte mailbox[GL_MAILBOX_SIZE_CHROMIUM];
   5957   group().mailbox_manager()->GenerateMailboxName(
   5958       reinterpret_cast<MailboxName*>(mailbox));
   5959 
   5960   memcpy(shared_memory_address_, mailbox, sizeof(mailbox));
   5961 
   5962   EXPECT_EQ(kServiceTextureId, texture_ref->service_id());
   5963 
   5964   ProduceTextureCHROMIUM produce_cmd;
   5965   produce_cmd.Init(
   5966       GL_TEXTURE_EXTERNAL_OES, kSharedMemoryId, kSharedMemoryOffset);
   5967   EXPECT_EQ(error::kNoError, ExecuteCmd(produce_cmd));
   5968   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   5969 
   5970   // Create new texture for consume.
   5971   EXPECT_CALL(*gl_, GenTextures(_, _))
   5972       .WillOnce(SetArgumentPointee<1>(kNewServiceId))
   5973       .RetiresOnSaturation();
   5974   DoBindTexture(GL_TEXTURE_EXTERNAL_OES, kNewClientId, kNewServiceId);
   5975 
   5976   // Assigns and binds original service size texture ID.
   5977   EXPECT_CALL(*gl_, DeleteTextures(1, _))
   5978       .Times(1)
   5979       .RetiresOnSaturation();
   5980   EXPECT_CALL(*gl_, BindTexture(GL_TEXTURE_EXTERNAL_OES, kServiceTextureId))
   5981       .Times(1)
   5982       .RetiresOnSaturation();
   5983 
   5984   // Shared mem got clobbered from GetError() above.
   5985   memcpy(shared_memory_address_, mailbox, sizeof(mailbox));
   5986   ConsumeTextureCHROMIUM consume_cmd;
   5987   consume_cmd.Init(
   5988       GL_TEXTURE_EXTERNAL_OES, kSharedMemoryId, kSharedMemoryOffset);
   5989   EXPECT_EQ(error::kNoError, ExecuteCmd(consume_cmd));
   5990   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   5991 
   5992   // Service ID is restored.
   5993   EXPECT_EQ(kServiceTextureId, texture_ref->service_id());
   5994 
   5995   EXPECT_CALL(*stream_texture_manager(),
   5996               DestroyStreamTexture(kServiceTextureId))
   5997       .Times(1)
   5998       .RetiresOnSaturation();
   5999 }
   6000 
   6001 TEST_F(GLES2DecoderManualInitTest, ARBTextureRectangleBindTexture) {
   6002   InitDecoder(
   6003       "GL_ARB_texture_rectangle",  // extensions
   6004       false,   // has alpha
   6005       false,   // has depth
   6006       false,   // has stencil
   6007       false,   // request alpha
   6008       false,   // request depth
   6009       false,   // request stencil
   6010       true);   // bind generates resource
   6011   EXPECT_CALL(*gl_, BindTexture(GL_TEXTURE_RECTANGLE_ARB, kNewServiceId));
   6012   EXPECT_CALL(*gl_, GenTextures(1, _))
   6013      .WillOnce(SetArgumentPointee<1>(kNewServiceId));
   6014   BindTexture cmd;
   6015   cmd.Init(GL_TEXTURE_RECTANGLE_ARB, kNewClientId);
   6016   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6017   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   6018   Texture* texture = GetTexture(kNewClientId)->texture();
   6019   EXPECT_TRUE(texture != NULL);
   6020   EXPECT_TRUE(texture->target() == GL_TEXTURE_RECTANGLE_ARB);
   6021 }
   6022 
   6023 TEST_F(GLES2DecoderManualInitTest, ARBTextureRectangleGetBinding) {
   6024   InitDecoder(
   6025       "GL_ARB_texture_rectangle",  // extensions
   6026       false,   // has alpha
   6027       false,   // has depth
   6028       false,   // has stencil
   6029       false,   // request alpha
   6030       false,   // request depth
   6031       false,   // request stencil
   6032       true);   // bind generates resource
   6033   DoBindTexture(
   6034       GL_TEXTURE_RECTANGLE_ARB, client_texture_id_, kServiceTextureId);
   6035 
   6036   EXPECT_CALL(*gl_, GetError())
   6037       .WillOnce(Return(GL_NO_ERROR))
   6038       .WillOnce(Return(GL_NO_ERROR))
   6039       .RetiresOnSaturation();
   6040   typedef GetIntegerv::Result Result;
   6041   Result* result = static_cast<Result*>(shared_memory_address_);
   6042   EXPECT_CALL(*gl_, GetIntegerv(GL_TEXTURE_BINDING_RECTANGLE_ARB,
   6043                                 result->GetData()))
   6044       .Times(0);
   6045   result->size = 0;
   6046   GetIntegerv cmd;
   6047   cmd.Init(GL_TEXTURE_BINDING_RECTANGLE_ARB,
   6048            shared_memory_id_,
   6049            shared_memory_offset_);
   6050   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6051   EXPECT_EQ(decoder_->GetGLES2Util()->GLGetNumValuesReturned(
   6052       GL_TEXTURE_BINDING_RECTANGLE_ARB), result->GetNumResults());
   6053   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   6054   EXPECT_EQ(client_texture_id_, (uint32)result->GetData()[0]);
   6055 }
   6056 
   6057 TEST_F(GLES2DecoderManualInitTest, ARBTextureRectangleTextureDefaults) {
   6058   InitDecoder(
   6059       "GL_ARB_texture_rectangle",  // extensions
   6060       false,   // has alpha
   6061       false,   // has depth
   6062       false,   // has stencil
   6063       false,   // request alpha
   6064       false,   // request depth
   6065       false,   // request stencil
   6066       true);   // bind generates resource
   6067   DoBindTexture(
   6068       GL_TEXTURE_RECTANGLE_ARB, client_texture_id_, kServiceTextureId);
   6069 
   6070   Texture* texture = GetTexture(client_texture_id_)->texture();
   6071   EXPECT_TRUE(texture != NULL);
   6072   EXPECT_TRUE(texture->target() == GL_TEXTURE_RECTANGLE_ARB);
   6073   EXPECT_TRUE(texture->min_filter() == GL_LINEAR);
   6074   EXPECT_TRUE(texture->wrap_s() == GL_CLAMP_TO_EDGE);
   6075   EXPECT_TRUE(texture->wrap_t() == GL_CLAMP_TO_EDGE);
   6076 }
   6077 
   6078 TEST_F(GLES2DecoderManualInitTest, ARBTextureRectangleTextureParam) {
   6079   InitDecoder(
   6080       "GL_ARB_texture_rectangle",  // extensions
   6081       false,   // has alpha
   6082       false,   // has depth
   6083       false,   // has stencil
   6084       false,   // request alpha
   6085       false,   // request depth
   6086       false,   // request stencil
   6087       true);   // bind generates resource
   6088 
   6089   DoBindTexture(
   6090       GL_TEXTURE_RECTANGLE_ARB, client_texture_id_, kServiceTextureId);
   6091 
   6092   EXPECT_CALL(*gl_, TexParameteri(GL_TEXTURE_RECTANGLE_ARB,
   6093                                   GL_TEXTURE_MIN_FILTER,
   6094                                   GL_NEAREST));
   6095   EXPECT_CALL(*gl_, TexParameteri(GL_TEXTURE_RECTANGLE_ARB,
   6096                                   GL_TEXTURE_MIN_FILTER,
   6097                                   GL_LINEAR));
   6098   EXPECT_CALL(*gl_, TexParameteri(GL_TEXTURE_RECTANGLE_ARB,
   6099                                   GL_TEXTURE_WRAP_S,
   6100                                   GL_CLAMP_TO_EDGE));
   6101   EXPECT_CALL(*gl_, TexParameteri(GL_TEXTURE_RECTANGLE_ARB,
   6102                                   GL_TEXTURE_WRAP_T,
   6103                                   GL_CLAMP_TO_EDGE));
   6104   TexParameteri cmd;
   6105   cmd.Init(GL_TEXTURE_RECTANGLE_ARB,
   6106            GL_TEXTURE_MIN_FILTER,
   6107            GL_NEAREST);
   6108   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6109   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   6110 
   6111   cmd.Init(GL_TEXTURE_RECTANGLE_ARB,
   6112            GL_TEXTURE_MIN_FILTER,
   6113            GL_LINEAR);
   6114   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6115   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   6116 
   6117   cmd.Init(GL_TEXTURE_RECTANGLE_ARB,
   6118            GL_TEXTURE_WRAP_S,
   6119            GL_CLAMP_TO_EDGE);
   6120   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6121   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   6122 
   6123   cmd.Init(GL_TEXTURE_RECTANGLE_ARB,
   6124            GL_TEXTURE_WRAP_T,
   6125            GL_CLAMP_TO_EDGE);
   6126   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6127   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   6128 
   6129   Texture* texture = GetTexture(client_texture_id_)->texture();
   6130   EXPECT_TRUE(texture != NULL);
   6131   EXPECT_TRUE(texture->target() == GL_TEXTURE_RECTANGLE_ARB);
   6132   EXPECT_TRUE(texture->min_filter() == GL_LINEAR);
   6133   EXPECT_TRUE(texture->wrap_s() == GL_CLAMP_TO_EDGE);
   6134   EXPECT_TRUE(texture->wrap_t() == GL_CLAMP_TO_EDGE);
   6135 }
   6136 
   6137 TEST_F(GLES2DecoderManualInitTest, ARBTextureRectangleTextureParamInvalid) {
   6138   InitDecoder(
   6139       "GL_ARB_texture_rectangle",  // extensions
   6140       false,   // has alpha
   6141       false,   // has depth
   6142       false,   // has stencil
   6143       false,   // request alpha
   6144       false,   // request depth
   6145       false,   // request stencil
   6146       true);   // bind generates resource
   6147 
   6148   DoBindTexture(
   6149       GL_TEXTURE_RECTANGLE_ARB, client_texture_id_, kServiceTextureId);
   6150 
   6151   TexParameteri cmd;
   6152   cmd.Init(GL_TEXTURE_RECTANGLE_ARB,
   6153            GL_TEXTURE_MIN_FILTER,
   6154            GL_NEAREST_MIPMAP_NEAREST);
   6155   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6156   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
   6157 
   6158   cmd.Init(GL_TEXTURE_RECTANGLE_ARB,
   6159            GL_TEXTURE_WRAP_S,
   6160            GL_REPEAT);
   6161   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6162   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
   6163 
   6164   cmd.Init(GL_TEXTURE_RECTANGLE_ARB,
   6165            GL_TEXTURE_WRAP_T,
   6166            GL_REPEAT);
   6167   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6168   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
   6169 
   6170   Texture* texture = GetTexture(client_texture_id_)->texture();
   6171   EXPECT_TRUE(texture != NULL);
   6172   EXPECT_TRUE(texture->target() == GL_TEXTURE_RECTANGLE_ARB);
   6173   EXPECT_TRUE(texture->min_filter() == GL_LINEAR);
   6174   EXPECT_TRUE(texture->wrap_s() == GL_CLAMP_TO_EDGE);
   6175   EXPECT_TRUE(texture->wrap_t() == GL_CLAMP_TO_EDGE);
   6176 }
   6177 
   6178 TEST_F(GLES2DecoderManualInitTest, ARBTextureRectangleTexImage2DError) {
   6179   InitDecoder(
   6180       "GL_ARB_texture_rectangle",  // extensions
   6181       false,   // has alpha
   6182       false,   // has depth
   6183       false,   // has stencil
   6184       false,   // request alpha
   6185       false,   // request depth
   6186       false,   // request stencil
   6187       true);   // bind generates resource
   6188 
   6189   GLenum target = GL_TEXTURE_RECTANGLE_ARB;
   6190   GLint level = 0;
   6191   GLenum internal_format = GL_RGBA;
   6192   GLsizei width = 2;
   6193   GLsizei height = 4;
   6194   GLint border = 0;
   6195   GLenum format = GL_RGBA;
   6196   GLenum type = GL_UNSIGNED_BYTE;
   6197   DoBindTexture(
   6198       GL_TEXTURE_RECTANGLE_ARB, client_texture_id_, kServiceTextureId);
   6199   ASSERT_TRUE(GetTexture(client_texture_id_) != NULL);
   6200   TexImage2D cmd;
   6201   cmd.Init(target, level, internal_format, width, height, border, format,
   6202            type, kSharedMemoryId, kSharedMemoryOffset);
   6203   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6204 
   6205   // TexImage2D is not allowed with GL_TEXTURE_RECTANGLE_ARB targets.
   6206   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
   6207 }
   6208 
   6209 TEST_F(GLES2DecoderTest, EnableFeatureCHROMIUMBadBucket) {
   6210   const uint32 kBadBucketId = 123;
   6211   EnableFeatureCHROMIUM cmd;
   6212   cmd.Init(kBadBucketId, shared_memory_id_, shared_memory_offset_);
   6213   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   6214 }
   6215 
   6216 TEST_F(GLES2DecoderTest, RequestExtensionCHROMIUMBadBucket) {
   6217   const uint32 kBadBucketId = 123;
   6218   RequestExtensionCHROMIUM cmd;
   6219   cmd.Init(kBadBucketId);
   6220   EXPECT_NE(error::kNoError, ExecuteCmd(cmd));
   6221 }
   6222 
   6223 TEST_F(GLES2DecoderTest, TexSubImage2DClearsAfterTexImage2DNULL) {
   6224   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   6225   DoTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   6226                0, 0);
   6227   SetupClearTextureExpections(
   6228       kServiceTextureId, kServiceTextureId, GL_TEXTURE_2D, GL_TEXTURE_2D,
   6229       0, GL_RGBA, GL_UNSIGNED_BYTE, 2, 2);
   6230   EXPECT_CALL(*gl_, TexSubImage2D(
   6231       GL_TEXTURE_2D, 0, 1, 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,
   6232       shared_memory_address_))
   6233       .Times(1)
   6234       .RetiresOnSaturation();
   6235   TexSubImage2D cmd;
   6236   cmd.Init(
   6237       GL_TEXTURE_2D, 0, 1, 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,
   6238       kSharedMemoryId, kSharedMemoryOffset, GL_FALSE);
   6239   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6240   // Test if we call it again it does not clear.
   6241   EXPECT_CALL(*gl_, TexSubImage2D(
   6242       GL_TEXTURE_2D, 0, 1, 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,
   6243       shared_memory_address_))
   6244       .Times(1)
   6245       .RetiresOnSaturation();
   6246   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6247 }
   6248 
   6249 TEST_F(GLES2DecoderTest, TexSubImage2DDoesNotClearAfterTexImage2DNULLThenData) {
   6250   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   6251   DoTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   6252                0, 0);
   6253   DoTexImage2DSameSize(
   6254       GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   6255       kSharedMemoryId, kSharedMemoryOffset);
   6256   EXPECT_CALL(*gl_, TexSubImage2D(
   6257       GL_TEXTURE_2D, 0, 1, 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,
   6258       shared_memory_address_))
   6259       .Times(1)
   6260       .RetiresOnSaturation();
   6261   TexSubImage2D cmd;
   6262   cmd.Init(
   6263       GL_TEXTURE_2D, 0, 1, 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,
   6264       kSharedMemoryId, kSharedMemoryOffset, GL_FALSE);
   6265   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6266   // Test if we call it again it does not clear.
   6267   EXPECT_CALL(*gl_, TexSubImage2D(
   6268       GL_TEXTURE_2D, 0, 1, 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,
   6269       shared_memory_address_))
   6270       .Times(1)
   6271       .RetiresOnSaturation();
   6272   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6273 }
   6274 
   6275 TEST_F(GLES2DecoderANGLETest,
   6276        TexSubImage2DDoesNotClearAfterTexImage2DNULLThenData) {
   6277   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   6278   DoTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   6279                0, 0);
   6280   DoTexImage2DSameSize(
   6281       GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   6282       kSharedMemoryId, kSharedMemoryOffset);
   6283   EXPECT_CALL(*gl_, TexSubImage2D(
   6284       GL_TEXTURE_2D, 0, 1, 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,
   6285       shared_memory_address_))
   6286       .Times(1)
   6287       .RetiresOnSaturation();
   6288   TexSubImage2D cmd;
   6289   cmd.Init(
   6290       GL_TEXTURE_2D, 0, 1, 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,
   6291       kSharedMemoryId, kSharedMemoryOffset, GL_FALSE);
   6292   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6293   // Test if we call it again it does not clear.
   6294   EXPECT_CALL(*gl_, TexSubImage2D(
   6295       GL_TEXTURE_2D, 0, 1, 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,
   6296       shared_memory_address_))
   6297       .Times(1)
   6298       .RetiresOnSaturation();
   6299   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6300 }
   6301 
   6302 TEST_F(GLES2DecoderTest, TexSubImage2DClearsAfterTexImage2DWithDataThenNULL) {
   6303   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   6304   // Put in data (so it should be marked as cleared)
   6305   DoTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   6306                kSharedMemoryId, kSharedMemoryOffset);
   6307   // Put in no data.
   6308   TexImage2D tex_cmd;
   6309   tex_cmd.Init(
   6310       GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0, 0);
   6311   // It won't actually call TexImage2D, just mark it as uncleared.
   6312   EXPECT_EQ(error::kNoError, ExecuteCmd(tex_cmd));
   6313   // Next call to TexSubImage2d should clear.
   6314   SetupClearTextureExpections(
   6315       kServiceTextureId, kServiceTextureId, GL_TEXTURE_2D, GL_TEXTURE_2D,
   6316       0, GL_RGBA, GL_UNSIGNED_BYTE, 2, 2);
   6317   EXPECT_CALL(*gl_, TexSubImage2D(
   6318       GL_TEXTURE_2D, 0, 1, 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,
   6319       shared_memory_address_))
   6320       .Times(1)
   6321       .RetiresOnSaturation();
   6322   TexSubImage2D cmd;
   6323   cmd.Init(
   6324       GL_TEXTURE_2D, 0, 1, 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,
   6325       kSharedMemoryId, kSharedMemoryOffset, GL_FALSE);
   6326   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6327 }
   6328 
   6329 TEST_F(GLES2DecoderWithShaderTest, DrawArraysClearsAfterTexImage2DNULL) {
   6330   SetupAllNeededVertexBuffers();
   6331   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   6332   // Create an uncleared texture with 2 levels.
   6333   DoTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   6334                0, 0);
   6335   DoTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   6336                0, 0);
   6337   // Expect 2 levels will be cleared.
   6338   SetupClearTextureExpections(
   6339       kServiceTextureId, kServiceTextureId, GL_TEXTURE_2D, GL_TEXTURE_2D,
   6340       0, GL_RGBA, GL_UNSIGNED_BYTE, 2, 2);
   6341   SetupClearTextureExpections(
   6342       kServiceTextureId, kServiceTextureId, GL_TEXTURE_2D, GL_TEXTURE_2D,
   6343       1, GL_RGBA, GL_UNSIGNED_BYTE, 1, 1);
   6344   SetupExpectationsForApplyingDefaultDirtyState();
   6345   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   6346       .Times(1)
   6347       .RetiresOnSaturation();
   6348   DrawArrays cmd;
   6349   cmd.Init(GL_TRIANGLES, 0, kNumVertices);
   6350   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6351   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   6352 
   6353   // But not again
   6354   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   6355       .Times(1)
   6356       .RetiresOnSaturation();
   6357   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6358   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   6359 }
   6360 
   6361 TEST_F(GLES2DecoderWithShaderTest, DrawElementsClearsAfterTexImage2DNULL) {
   6362   SetupAllNeededVertexBuffers();
   6363   SetupIndexBuffer();
   6364   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   6365   // Create an uncleared texture with 2 levels.
   6366   DoTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   6367                0, 0);
   6368   DoTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   6369                0, 0);
   6370   // Expect 2 levels will be cleared.
   6371   SetupClearTextureExpections(
   6372       kServiceTextureId, kServiceTextureId, GL_TEXTURE_2D, GL_TEXTURE_2D,
   6373       0, GL_RGBA, GL_UNSIGNED_BYTE, 2, 2);
   6374   SetupClearTextureExpections(
   6375       kServiceTextureId, kServiceTextureId, GL_TEXTURE_2D, GL_TEXTURE_2D,
   6376       1, GL_RGBA, GL_UNSIGNED_BYTE, 1, 1);
   6377   SetupExpectationsForApplyingDefaultDirtyState();
   6378 
   6379   EXPECT_CALL(*gl_, DrawElements(GL_TRIANGLES, kValidIndexRangeCount,
   6380                                  GL_UNSIGNED_SHORT,
   6381                                  BufferOffset(kValidIndexRangeStart * 2)))
   6382       .Times(1)
   6383       .RetiresOnSaturation();
   6384   DrawElements cmd;
   6385   cmd.Init(GL_TRIANGLES, kValidIndexRangeCount, GL_UNSIGNED_SHORT,
   6386            kValidIndexRangeStart * 2);
   6387   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6388   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   6389 
   6390   // But not again
   6391   EXPECT_CALL(*gl_, DrawElements(GL_TRIANGLES, kValidIndexRangeCount,
   6392                                  GL_UNSIGNED_SHORT,
   6393                                  BufferOffset(kValidIndexRangeStart * 2)))
   6394       .Times(1)
   6395       .RetiresOnSaturation();
   6396   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6397   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   6398 }
   6399 
   6400 TEST_F(GLES2DecoderWithShaderTest, DrawClearsAfterTexImage2DNULLInFBO) {
   6401   const GLuint kFBOClientTextureId = 4100;
   6402   const GLuint kFBOServiceTextureId = 4101;
   6403 
   6404   SetupAllNeededVertexBuffers();
   6405   // Register a texture id.
   6406   EXPECT_CALL(*gl_, GenTextures(_, _))
   6407       .WillOnce(SetArgumentPointee<1>(kFBOServiceTextureId))
   6408       .RetiresOnSaturation();
   6409   GenHelper<GenTexturesImmediate>(kFBOClientTextureId);
   6410 
   6411   // Setup "render to" texture.
   6412   DoBindTexture(GL_TEXTURE_2D, kFBOClientTextureId, kFBOServiceTextureId);
   6413   DoTexImage2D(
   6414       GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0, 0);
   6415   DoBindFramebuffer(
   6416       GL_FRAMEBUFFER, client_framebuffer_id_, kServiceFramebufferId);
   6417   DoFramebufferTexture2D(
   6418       GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
   6419       kFBOClientTextureId, kFBOServiceTextureId, 0, GL_NO_ERROR);
   6420 
   6421   // Setup "render from" texture.
   6422   SetupTexture();
   6423 
   6424   SetupExpectationsForFramebufferClearing(
   6425       GL_FRAMEBUFFER,         // target
   6426       GL_COLOR_BUFFER_BIT,    // clear bits
   6427       0, 0, 0, 0,             // color
   6428       0,                      // stencil
   6429       1.0f,                   // depth
   6430       false);                 // scissor test
   6431 
   6432   SetupExpectationsForApplyingDirtyState(
   6433       false,   // Framebuffer is RGB
   6434       false,   // Framebuffer has depth
   6435       false,   // Framebuffer has stencil
   6436       0x1111,  // color bits
   6437       false,   // depth mask
   6438       false,   // depth enabled
   6439       0,       // front stencil mask
   6440       0,       // back stencil mask
   6441       false,   // stencil enabled
   6442       false,   // cull_face_enabled
   6443       false,   // scissor_test_enabled
   6444       false);  // blend_enabled
   6445 
   6446   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   6447       .Times(1)
   6448       .RetiresOnSaturation();
   6449   DrawArrays cmd;
   6450   cmd.Init(GL_TRIANGLES, 0, kNumVertices);
   6451   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6452   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   6453 
   6454   // But not again.
   6455   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   6456       .Times(1)
   6457       .RetiresOnSaturation();
   6458   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6459   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   6460 }
   6461 
   6462 TEST_F(GLES2DecoderWithShaderTest, DrawWitFBOThatCantClearDoesNotDraw) {
   6463   const GLuint kFBOClientTextureId = 4100;
   6464   const GLuint kFBOServiceTextureId = 4101;
   6465 
   6466   // Register a texture id.
   6467   EXPECT_CALL(*gl_, GenTextures(_, _))
   6468       .WillOnce(SetArgumentPointee<1>(kFBOServiceTextureId))
   6469       .RetiresOnSaturation();
   6470   GenHelper<GenTexturesImmediate>(kFBOClientTextureId);
   6471 
   6472   // Setup "render to" texture.
   6473   DoBindTexture(GL_TEXTURE_2D, kFBOClientTextureId, kFBOServiceTextureId);
   6474   DoTexImage2D(
   6475       GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0, 0);
   6476   DoBindFramebuffer(
   6477       GL_FRAMEBUFFER, client_framebuffer_id_, kServiceFramebufferId);
   6478   DoFramebufferTexture2D(
   6479       GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
   6480       kFBOClientTextureId, kFBOServiceTextureId, 0, GL_NO_ERROR);
   6481 
   6482   // Setup "render from" texture.
   6483   SetupTexture();
   6484 
   6485   EXPECT_CALL(*gl_, CheckFramebufferStatusEXT(GL_FRAMEBUFFER))
   6486       .WillOnce(Return(GL_FRAMEBUFFER_UNSUPPORTED))
   6487       .RetiresOnSaturation();
   6488   EXPECT_CALL(*gl_, DrawArrays(_, _, _))
   6489       .Times(0)
   6490       .RetiresOnSaturation();
   6491   DrawArrays cmd;
   6492   cmd.Init(GL_TRIANGLES, 0, kNumVertices);
   6493   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6494   EXPECT_EQ(GL_INVALID_FRAMEBUFFER_OPERATION, GetGLError());
   6495 }
   6496 
   6497 TEST_F(GLES2DecoderTest, CopyTexImage2DMarksTextureAsCleared) {
   6498   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   6499 
   6500   TextureManager* manager = group().texture_manager();
   6501   TextureRef* texture_ref = manager->GetTexture(client_texture_id_);
   6502   ASSERT_TRUE(texture_ref != NULL);
   6503   Texture* texture = texture_ref->texture();
   6504 
   6505   EXPECT_CALL(*gl_, GetError())
   6506       .WillOnce(Return(GL_NO_ERROR))
   6507       .RetiresOnSaturation();
   6508   EXPECT_CALL(*gl_, CopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, 1, 1, 0))
   6509       .Times(1)
   6510       .RetiresOnSaturation();
   6511   EXPECT_CALL(*gl_, GetError())
   6512       .WillOnce(Return(GL_NO_ERROR))
   6513       .RetiresOnSaturation();
   6514   CopyTexImage2D cmd;
   6515   cmd.Init(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, 1, 1, 0);
   6516   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6517 
   6518   EXPECT_TRUE(texture->SafeToRenderFrom());
   6519 }
   6520 
   6521 TEST_F(GLES2DecoderTest, CopyTexSubImage2DClearsUnclearedTexture) {
   6522   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   6523   DoTexImage2D(
   6524       GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0, 0);
   6525 
   6526   SetupClearTextureExpections(
   6527       kServiceTextureId, kServiceTextureId, GL_TEXTURE_2D, GL_TEXTURE_2D,
   6528       0, GL_RGBA, GL_UNSIGNED_BYTE, 2, 2);
   6529   EXPECT_CALL(*gl_, CopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, 1, 1))
   6530       .Times(1)
   6531       .RetiresOnSaturation();
   6532   CopyTexSubImage2D cmd;
   6533   cmd.Init(GL_TEXTURE_2D, 0, 0, 0, 0, 0, 1, 1);
   6534   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6535 }
   6536 
   6537 TEST_F(GLES2DecoderManualInitTest, CompressedImage2DMarksTextureAsCleared) {
   6538   InitDecoder(
   6539       "GL_EXT_texture_compression_s3tc",  // extensions
   6540       false,   // has alpha
   6541       false,   // has depth
   6542       false,   // has stencil
   6543       false,   // request alpha
   6544       false,   // request depth
   6545       false,   // request stencil
   6546       true);   // bind generates resource
   6547 
   6548   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   6549   EXPECT_CALL(*gl_, GetError())
   6550       .WillOnce(Return(GL_NO_ERROR))
   6551       .RetiresOnSaturation();
   6552   EXPECT_CALL(*gl_, CompressedTexImage2D(
   6553       GL_TEXTURE_2D, 0, GL_COMPRESSED_RGB_S3TC_DXT1_EXT, 4, 4, 0, 8, _))
   6554       .Times(1)
   6555       .RetiresOnSaturation();
   6556   EXPECT_CALL(*gl_, GetError())
   6557       .WillOnce(Return(GL_NO_ERROR))
   6558       .RetiresOnSaturation();
   6559   CompressedTexImage2D cmd;
   6560   cmd.Init(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGB_S3TC_DXT1_EXT, 4, 4, 0,
   6561            8, kSharedMemoryId, kSharedMemoryOffset);
   6562   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6563   TextureManager* manager = group().texture_manager();
   6564   TextureRef* texture_ref = manager->GetTexture(client_texture_id_);
   6565   EXPECT_TRUE(texture_ref->texture()->SafeToRenderFrom());
   6566 }
   6567 
   6568 TEST_F(GLES2DecoderWithShaderTest, UnClearedAttachmentsGetClearedOnClear) {
   6569   const GLuint kFBOClientTextureId = 4100;
   6570   const GLuint kFBOServiceTextureId = 4101;
   6571 
   6572   // Register a texture id.
   6573   EXPECT_CALL(*gl_, GenTextures(_, _))
   6574       .WillOnce(SetArgumentPointee<1>(kFBOServiceTextureId))
   6575       .RetiresOnSaturation();
   6576   GenHelper<GenTexturesImmediate>(kFBOClientTextureId);
   6577 
   6578   // Setup "render to" texture.
   6579   DoBindTexture(GL_TEXTURE_2D, kFBOClientTextureId, kFBOServiceTextureId);
   6580   DoTexImage2D(
   6581       GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0, 0);
   6582   DoBindFramebuffer(
   6583       GL_FRAMEBUFFER, client_framebuffer_id_, kServiceFramebufferId);
   6584   DoFramebufferTexture2D(
   6585       GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
   6586       kFBOClientTextureId, kFBOServiceTextureId, 0, GL_NO_ERROR);
   6587 
   6588   // Setup "render from" texture.
   6589   SetupTexture();
   6590 
   6591   SetupExpectationsForFramebufferClearing(
   6592       GL_FRAMEBUFFER,         // target
   6593       GL_COLOR_BUFFER_BIT,    // clear bits
   6594       0, 0, 0, 0,             // color
   6595       0,                      // stencil
   6596       1.0f,                   // depth
   6597       false);                 // scissor test
   6598   SetupExpectationsForApplyingDirtyState(
   6599       false,   // Framebuffer is RGB
   6600       false,   // Framebuffer has depth
   6601       false,   // Framebuffer has stencil
   6602       0x1111,  // color bits
   6603       false,   // depth mask
   6604       false,   // depth enabled
   6605       0,       // front stencil mask
   6606       0,       // back stencil mask
   6607       false,   // stencil enabled
   6608       false,   // cull_face_enabled
   6609       false,   // scissor_test_enabled
   6610       false);  // blend_enabled
   6611 
   6612   EXPECT_CALL(*gl_, Clear(GL_COLOR_BUFFER_BIT))
   6613       .Times(1)
   6614       .RetiresOnSaturation();
   6615 
   6616   Clear cmd;
   6617   cmd.Init(GL_COLOR_BUFFER_BIT);
   6618   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6619   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   6620 }
   6621 
   6622 TEST_F(GLES2DecoderWithShaderTest, UnClearedAttachmentsGetClearedOnReadPixels) {
   6623   const GLuint kFBOClientTextureId = 4100;
   6624   const GLuint kFBOServiceTextureId = 4101;
   6625 
   6626   // Register a texture id.
   6627   EXPECT_CALL(*gl_, GenTextures(_, _))
   6628       .WillOnce(SetArgumentPointee<1>(kFBOServiceTextureId))
   6629       .RetiresOnSaturation();
   6630   GenHelper<GenTexturesImmediate>(kFBOClientTextureId);
   6631 
   6632   // Setup "render to" texture.
   6633   DoBindTexture(GL_TEXTURE_2D, kFBOClientTextureId, kFBOServiceTextureId);
   6634   DoTexImage2D(
   6635       GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0, 0);
   6636   DoBindFramebuffer(
   6637       GL_FRAMEBUFFER, client_framebuffer_id_, kServiceFramebufferId);
   6638   DoFramebufferTexture2D(
   6639       GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
   6640       kFBOClientTextureId, kFBOServiceTextureId, 0, GL_NO_ERROR);
   6641 
   6642   // Setup "render from" texture.
   6643   SetupTexture();
   6644 
   6645   SetupExpectationsForFramebufferClearing(
   6646       GL_FRAMEBUFFER,         // target
   6647       GL_COLOR_BUFFER_BIT,    // clear bits
   6648       0, 0, 0, 0,             // color
   6649       0,                      // stencil
   6650       1.0f,                   // depth
   6651       false);                 // scissor test
   6652 
   6653   EXPECT_CALL(*gl_, GetError())
   6654      .WillOnce(Return(GL_NO_ERROR))
   6655      .WillOnce(Return(GL_NO_ERROR))
   6656      .RetiresOnSaturation();
   6657   EXPECT_CALL(*gl_, ReadPixels(0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, _))
   6658       .Times(1)
   6659       .RetiresOnSaturation();
   6660   typedef ReadPixels::Result Result;
   6661   Result* result = GetSharedMemoryAs<Result*>();
   6662   uint32 result_shm_id = kSharedMemoryId;
   6663   uint32 result_shm_offset = kSharedMemoryOffset;
   6664   uint32 pixels_shm_id = kSharedMemoryId;
   6665   uint32 pixels_shm_offset = kSharedMemoryOffset + sizeof(*result);
   6666   ReadPixels cmd;
   6667   cmd.Init(0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,
   6668            pixels_shm_id, pixels_shm_offset,
   6669            result_shm_id, result_shm_offset,
   6670            false);
   6671   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6672   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   6673 }
   6674 
   6675 TEST_F(GLES2DecoderManualInitTest,
   6676        UnClearedAttachmentsGetClearedOnReadPixelsAndDrawBufferGetsRestored) {
   6677   InitDecoder(
   6678       "GL_EXT_framebuffer_multisample",  // extensions
   6679       false,   // has alpha
   6680       false,   // has depth
   6681       false,   // has stencil
   6682       false,   // request alpha
   6683       false,   // request depth
   6684       false,   // request stencil
   6685       true);   // bind generates resource
   6686   const GLuint kFBOClientTextureId = 4100;
   6687   const GLuint kFBOServiceTextureId = 4101;
   6688 
   6689   // Register a texture id.
   6690   EXPECT_CALL(*gl_, GenTextures(_, _))
   6691       .WillOnce(SetArgumentPointee<1>(kFBOServiceTextureId))
   6692       .RetiresOnSaturation();
   6693   GenHelper<GenTexturesImmediate>(kFBOClientTextureId);
   6694 
   6695   // Setup "render from" texture.
   6696   DoBindTexture(GL_TEXTURE_2D, kFBOClientTextureId, kFBOServiceTextureId);
   6697   DoTexImage2D(
   6698       GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0, 0);
   6699   DoBindFramebuffer(
   6700       GL_READ_FRAMEBUFFER, client_framebuffer_id_, kServiceFramebufferId);
   6701   DoFramebufferTexture2D(
   6702       GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
   6703       kFBOClientTextureId, kFBOServiceTextureId, 0, GL_NO_ERROR);
   6704 
   6705   SetupExpectationsForFramebufferClearingMulti(
   6706       kServiceFramebufferId,  // read framebuffer service id
   6707       0,                      // backbuffer service id
   6708       GL_READ_FRAMEBUFFER,    // target
   6709       GL_COLOR_BUFFER_BIT,    // clear bits
   6710       0, 0, 0, 0,             // color
   6711       0,                      // stencil
   6712       1.0f,                   // depth
   6713       false);                 // scissor test
   6714 
   6715   EXPECT_CALL(*gl_, GetError())
   6716      .WillOnce(Return(GL_NO_ERROR))
   6717      .WillOnce(Return(GL_NO_ERROR))
   6718      .RetiresOnSaturation();
   6719   EXPECT_CALL(*gl_, ReadPixels(0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, _))
   6720       .Times(1)
   6721       .RetiresOnSaturation();
   6722   typedef ReadPixels::Result Result;
   6723   uint32 result_shm_id = kSharedMemoryId;
   6724   uint32 result_shm_offset = kSharedMemoryOffset;
   6725   uint32 pixels_shm_id = kSharedMemoryId;
   6726   uint32 pixels_shm_offset = kSharedMemoryOffset + sizeof(Result);
   6727   ReadPixels cmd;
   6728   cmd.Init(0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,
   6729            pixels_shm_id, pixels_shm_offset,
   6730            result_shm_id, result_shm_offset,
   6731            false);
   6732   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6733   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   6734 }
   6735 
   6736 TEST_F(GLES2DecoderWithShaderTest, DrawClearsAfterRenderbufferStorageInFBO) {
   6737   SetupTexture();
   6738   DoBindRenderbuffer(GL_RENDERBUFFER, client_renderbuffer_id_,
   6739                     kServiceRenderbufferId);
   6740   DoBindFramebuffer(GL_FRAMEBUFFER, client_framebuffer_id_,
   6741                     kServiceFramebufferId);
   6742   DoRenderbufferStorage(
   6743       GL_RENDERBUFFER, GL_RGBA4, GL_RGBA, 100, 50, GL_NO_ERROR);
   6744   DoFramebufferRenderbuffer(
   6745       GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER,
   6746       client_renderbuffer_id_, kServiceRenderbufferId, GL_NO_ERROR);
   6747 
   6748   SetupExpectationsForFramebufferClearing(
   6749       GL_FRAMEBUFFER,         // target
   6750       GL_COLOR_BUFFER_BIT,    // clear bits
   6751       0, 0, 0, 0,             // color
   6752       0,                      // stencil
   6753       1.0f,                   // depth
   6754       false);                 // scissor test
   6755 
   6756   AddExpectationsForSimulatedAttrib0(kNumVertices, 0);
   6757   SetupExpectationsForApplyingDirtyState(
   6758       false,   // Framebuffer is RGB
   6759       false,   // Framebuffer has depth
   6760       false,   // Framebuffer has stencil
   6761       0x1111,  // color bits
   6762       false,   // depth mask
   6763       false,   // depth enabled
   6764       0,       // front stencil mask
   6765       0,       // back stencil mask
   6766       false,   // stencil enabled
   6767       false,   // cull_face_enabled
   6768       false,   // scissor_test_enabled
   6769       false);  // blend_enabled
   6770 
   6771   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   6772       .Times(1)
   6773       .RetiresOnSaturation();
   6774   DrawArrays cmd;
   6775   cmd.Init(GL_TRIANGLES, 0, kNumVertices);
   6776   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6777   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   6778 }
   6779 
   6780 TEST_F(GLES2DecoderTest, DrawArraysClearsAfterTexImage2DNULLCubemap) {
   6781   static const GLenum faces[] = {
   6782     GL_TEXTURE_CUBE_MAP_POSITIVE_X,
   6783     GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
   6784     GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
   6785     GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
   6786     GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
   6787     GL_TEXTURE_CUBE_MAP_NEGATIVE_Z,
   6788   };
   6789   SetupCubemapProgram();
   6790   DoBindTexture(GL_TEXTURE_CUBE_MAP, client_texture_id_, kServiceTextureId);
   6791   // Fill out all the faces for 2 levels, leave 2 uncleared.
   6792   for (int ii = 0; ii < 6; ++ii) {
   6793     GLenum face = faces[ii];
   6794     int32 shm_id =
   6795         (face == GL_TEXTURE_CUBE_MAP_NEGATIVE_Y) ? 0 : kSharedMemoryId;
   6796     uint32 shm_offset =
   6797         (face == GL_TEXTURE_CUBE_MAP_NEGATIVE_Y) ? 0 : kSharedMemoryOffset;
   6798     DoTexImage2D(face, 0, GL_RGBA, 2, 2, 0, GL_RGBA,
   6799                  GL_UNSIGNED_BYTE, shm_id, shm_offset);
   6800     DoTexImage2D(face, 1, GL_RGBA, 1, 1, 0, GL_RGBA,
   6801                  GL_UNSIGNED_BYTE, shm_id, shm_offset);
   6802   }
   6803   // Expect 2 levels will be cleared.
   6804   SetupClearTextureExpections(
   6805       kServiceTextureId, kServiceTextureId, GL_TEXTURE_CUBE_MAP,
   6806       GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_RGBA, GL_UNSIGNED_BYTE, 2, 2);
   6807   SetupClearTextureExpections(
   6808       kServiceTextureId, kServiceTextureId, GL_TEXTURE_CUBE_MAP,
   6809       GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, 1);
   6810   AddExpectationsForSimulatedAttrib0(kNumVertices, 0);
   6811   SetupExpectationsForApplyingDefaultDirtyState();
   6812   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   6813       .Times(1)
   6814       .RetiresOnSaturation();
   6815   DrawArrays cmd;
   6816   cmd.Init(GL_TRIANGLES, 0, kNumVertices);
   6817   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6818 }
   6819 
   6820 TEST_F(GLES2DecoderTest, TextureUsageAngleExtNotEnabledByDefault) {
   6821   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   6822 
   6823   TexParameteri cmd;
   6824   cmd.Init(GL_TEXTURE_2D,
   6825            GL_TEXTURE_USAGE_ANGLE,
   6826            GL_FRAMEBUFFER_ATTACHMENT_ANGLE);
   6827   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6828   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
   6829 }
   6830 
   6831 TEST_F(GLES2DecoderWithShaderTest,
   6832        DrawClearsAfterRenderbuffersWithMultipleAttachments) {
   6833   const GLuint kFBOClientTextureId = 4100;
   6834   const GLuint kFBOServiceTextureId = 4101;
   6835 
   6836   // Register a texture id.
   6837   EXPECT_CALL(*gl_, GenTextures(_, _))
   6838       .WillOnce(SetArgumentPointee<1>(kFBOServiceTextureId))
   6839       .RetiresOnSaturation();
   6840   GenHelper<GenTexturesImmediate>(kFBOClientTextureId);
   6841 
   6842   // Setup "render to" texture.
   6843   DoBindTexture(GL_TEXTURE_2D, kFBOClientTextureId, kFBOServiceTextureId);
   6844   DoTexImage2D(
   6845       GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0, 0);
   6846   DoBindFramebuffer(
   6847       GL_FRAMEBUFFER, client_framebuffer_id_, kServiceFramebufferId);
   6848   DoFramebufferTexture2D(
   6849       GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
   6850       kFBOClientTextureId, kFBOServiceTextureId, 0, GL_NO_ERROR);
   6851 
   6852   DoBindRenderbuffer(GL_RENDERBUFFER, client_renderbuffer_id_,
   6853                     kServiceRenderbufferId);
   6854   DoBindFramebuffer(GL_FRAMEBUFFER, client_framebuffer_id_,
   6855                     kServiceFramebufferId);
   6856   DoRenderbufferStorage(
   6857       GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT,
   6858       1, 1, GL_NO_ERROR);
   6859   DoFramebufferRenderbuffer(
   6860       GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER,
   6861       client_renderbuffer_id_, kServiceRenderbufferId, GL_NO_ERROR);
   6862 
   6863   SetupTexture();
   6864   SetupExpectationsForFramebufferClearing(
   6865       GL_FRAMEBUFFER,         // target
   6866       GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT,    // clear bits
   6867       0, 0, 0, 0,             // color
   6868       0,                      // stencil
   6869       1.0f,                   // depth
   6870       false);                 // scissor test
   6871 
   6872   AddExpectationsForSimulatedAttrib0(kNumVertices, 0);
   6873   SetupExpectationsForApplyingDirtyState(
   6874       false,   // Framebuffer is RGB
   6875       true,    // Framebuffer has depth
   6876       false,   // Framebuffer has stencil
   6877       0x1111,  // color bits
   6878       true,    // depth mask
   6879       false,   // depth enabled
   6880       0,       // front stencil mask
   6881       0,       // back stencil mask
   6882       false,   // stencil enabled
   6883       false,   // cull_face_enabled
   6884       false,   // scissor_test_enabled
   6885       false);  // blend_enabled
   6886 
   6887   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   6888       .Times(1)
   6889       .RetiresOnSaturation();
   6890   DrawArrays cmd;
   6891   cmd.Init(GL_TRIANGLES, 0, kNumVertices);
   6892   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6893   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   6894 }
   6895 
   6896 TEST_F(GLES2DecoderWithShaderTest, CopyTexImageWithInCompleteFBOFails) {
   6897   GLenum target = GL_TEXTURE_2D;
   6898   GLint level = 0;
   6899   GLenum internal_format = GL_RGBA;
   6900   GLsizei width = 2;
   6901   GLsizei height = 4;
   6902   GLint border = 0;
   6903   SetupTexture();
   6904   DoBindRenderbuffer(GL_RENDERBUFFER, client_renderbuffer_id_,
   6905                     kServiceRenderbufferId);
   6906   DoBindFramebuffer(GL_FRAMEBUFFER, client_framebuffer_id_,
   6907                     kServiceFramebufferId);
   6908   DoRenderbufferStorage(
   6909       GL_RENDERBUFFER, GL_RGBA4, GL_RGBA, 0, 0, GL_NO_ERROR);
   6910   DoFramebufferRenderbuffer(
   6911       GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER,
   6912       client_renderbuffer_id_, kServiceRenderbufferId, GL_NO_ERROR);
   6913 
   6914   EXPECT_CALL(*gl_, CopyTexImage2D(_, _, _, _, _, _, _, _))
   6915       .Times(0)
   6916       .RetiresOnSaturation();
   6917   CopyTexImage2D cmd;
   6918   cmd.Init(target, level, internal_format, 0, 0, width, height, border);
   6919   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   6920   EXPECT_EQ(GL_INVALID_FRAMEBUFFER_OPERATION, GetGLError());
   6921 }
   6922 
   6923 void GLES2DecoderWithShaderTest::CheckRenderbufferChangesMarkFBOAsNotComplete(
   6924     bool bound_fbo) {
   6925   FramebufferManager* framebuffer_manager = group().framebuffer_manager();
   6926   SetupTexture();
   6927   DoBindRenderbuffer(GL_RENDERBUFFER, client_renderbuffer_id_,
   6928                     kServiceRenderbufferId);
   6929   DoBindFramebuffer(GL_FRAMEBUFFER, client_framebuffer_id_,
   6930                     kServiceFramebufferId);
   6931   DoRenderbufferStorage(
   6932       GL_RENDERBUFFER, GL_RGBA4, GL_RGBA, 1, 1, GL_NO_ERROR);
   6933   DoFramebufferRenderbuffer(
   6934       GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER,
   6935       client_renderbuffer_id_, kServiceRenderbufferId, GL_NO_ERROR);
   6936 
   6937 
   6938   if (!bound_fbo) {
   6939     DoBindFramebuffer(GL_FRAMEBUFFER, 0, 0);
   6940   }
   6941 
   6942   Framebuffer* framebuffer =
   6943       framebuffer_manager->GetFramebuffer(client_framebuffer_id_);
   6944   ASSERT_TRUE(framebuffer != NULL);
   6945   framebuffer_manager->MarkAsComplete(framebuffer);
   6946   EXPECT_TRUE(framebuffer_manager->IsComplete(framebuffer));
   6947 
   6948   // Test that renderbufferStorage marks fbo as not complete.
   6949   DoRenderbufferStorage(
   6950       GL_RENDERBUFFER, GL_RGBA4, GL_RGBA, 1, 1, GL_NO_ERROR);
   6951   EXPECT_FALSE(framebuffer_manager->IsComplete(framebuffer));
   6952   framebuffer_manager->MarkAsComplete(framebuffer);
   6953   EXPECT_TRUE(framebuffer_manager->IsComplete(framebuffer));
   6954 
   6955   // Test deleting renderbuffer marks fbo as not complete.
   6956   DoDeleteRenderbuffer(client_renderbuffer_id_, kServiceRenderbufferId);
   6957   if (bound_fbo) {
   6958     EXPECT_FALSE(framebuffer_manager->IsComplete(framebuffer));
   6959   } else {
   6960     EXPECT_TRUE(framebuffer_manager->IsComplete(framebuffer));
   6961   }
   6962 }
   6963 
   6964 TEST_F(GLES2DecoderWithShaderTest,
   6965        RenderbufferChangesMarkFBOAsNotCompleteBoundFBO) {
   6966   CheckRenderbufferChangesMarkFBOAsNotComplete(true);
   6967 }
   6968 
   6969 TEST_F(GLES2DecoderWithShaderTest,
   6970        RenderbufferChangesMarkFBOAsNotCompleteUnboundFBO) {
   6971   CheckRenderbufferChangesMarkFBOAsNotComplete(false);
   6972 }
   6973 
   6974 void GLES2DecoderWithShaderTest::CheckTextureChangesMarkFBOAsNotComplete(
   6975     bool bound_fbo) {
   6976   FramebufferManager* framebuffer_manager = group().framebuffer_manager();
   6977   const GLuint kFBOClientTextureId = 4100;
   6978   const GLuint kFBOServiceTextureId = 4101;
   6979 
   6980   // Register a texture id.
   6981   EXPECT_CALL(*gl_, GenTextures(_, _))
   6982       .WillOnce(SetArgumentPointee<1>(kFBOServiceTextureId))
   6983       .RetiresOnSaturation();
   6984   GenHelper<GenTexturesImmediate>(kFBOClientTextureId);
   6985 
   6986   SetupTexture();
   6987 
   6988   // Setup "render to" texture.
   6989   DoBindTexture(GL_TEXTURE_2D, kFBOClientTextureId, kFBOServiceTextureId);
   6990   DoTexImage2D(
   6991       GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0, 0);
   6992   DoBindFramebuffer(
   6993       GL_FRAMEBUFFER, client_framebuffer_id_, kServiceFramebufferId);
   6994   DoFramebufferTexture2D(
   6995       GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
   6996       kFBOClientTextureId, kFBOServiceTextureId, 0, GL_NO_ERROR);
   6997 
   6998   DoBindRenderbuffer(GL_RENDERBUFFER, client_renderbuffer_id_,
   6999                     kServiceRenderbufferId);
   7000   DoBindFramebuffer(GL_FRAMEBUFFER, client_framebuffer_id_,
   7001                     kServiceFramebufferId);
   7002   DoRenderbufferStorage(
   7003       GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT,
   7004       1, 1, GL_NO_ERROR);
   7005   DoFramebufferRenderbuffer(
   7006       GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER,
   7007       client_renderbuffer_id_, kServiceRenderbufferId, GL_NO_ERROR);
   7008 
   7009   if (!bound_fbo) {
   7010     DoBindFramebuffer(GL_FRAMEBUFFER, 0, 0);
   7011   }
   7012 
   7013   Framebuffer* framebuffer =
   7014       framebuffer_manager->GetFramebuffer(client_framebuffer_id_);
   7015   ASSERT_TRUE(framebuffer != NULL);
   7016   framebuffer_manager->MarkAsComplete(framebuffer);
   7017   EXPECT_TRUE(framebuffer_manager->IsComplete(framebuffer));
   7018 
   7019   // Test TexImage2D marks fbo as not complete.
   7020   DoTexImage2D(
   7021       GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, 0, 0);
   7022   EXPECT_FALSE(framebuffer_manager->IsComplete(framebuffer));
   7023   framebuffer_manager->MarkAsComplete(framebuffer);
   7024   EXPECT_TRUE(framebuffer_manager->IsComplete(framebuffer));
   7025 
   7026   // Test CopyImage2D marks fbo as not complete.
   7027   EXPECT_CALL(*gl_, GetError())
   7028       .WillOnce(Return(GL_NO_ERROR))
   7029       .RetiresOnSaturation();
   7030   EXPECT_CALL(*gl_, CopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, 1, 1, 0))
   7031       .Times(1)
   7032       .RetiresOnSaturation();
   7033   EXPECT_CALL(*gl_, GetError())
   7034       .WillOnce(Return(GL_NO_ERROR))
   7035       .RetiresOnSaturation();
   7036   CopyTexImage2D cmd;
   7037   cmd.Init(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, 1, 1, 0);
   7038   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   7039   EXPECT_FALSE(framebuffer_manager->IsComplete(framebuffer));
   7040 
   7041   // Test deleting texture marks fbo as not complete.
   7042   framebuffer_manager->MarkAsComplete(framebuffer);
   7043   EXPECT_TRUE(framebuffer_manager->IsComplete(framebuffer));
   7044   DoDeleteTexture(kFBOClientTextureId, kFBOServiceTextureId);
   7045 
   7046   if (bound_fbo) {
   7047     EXPECT_FALSE(framebuffer_manager->IsComplete(framebuffer));
   7048   } else {
   7049     EXPECT_TRUE(framebuffer_manager->IsComplete(framebuffer));
   7050   }
   7051 }
   7052 
   7053 TEST_F(GLES2DecoderWithShaderTest, TextureChangesMarkFBOAsNotCompleteBoundFBO) {
   7054   CheckTextureChangesMarkFBOAsNotComplete(true);
   7055 }
   7056 
   7057 TEST_F(GLES2DecoderWithShaderTest,
   7058        TextureChangesMarkFBOAsNotCompleteUnboundFBO) {
   7059   CheckTextureChangesMarkFBOAsNotComplete(false);
   7060 }
   7061 
   7062 TEST_F(GLES2DecoderWithShaderTest,
   7063        DrawingWithFBOTwiceChecksForFBOCompleteOnce) {
   7064   const GLuint kFBOClientTextureId = 4100;
   7065   const GLuint kFBOServiceTextureId = 4101;
   7066 
   7067   SetupAllNeededVertexBuffers();
   7068 
   7069   // Register a texture id.
   7070   EXPECT_CALL(*gl_, GenTextures(_, _))
   7071       .WillOnce(SetArgumentPointee<1>(kFBOServiceTextureId))
   7072       .RetiresOnSaturation();
   7073   GenHelper<GenTexturesImmediate>(kFBOClientTextureId);
   7074 
   7075   // Setup "render to" texture that is cleared.
   7076   DoBindTexture(GL_TEXTURE_2D, kFBOClientTextureId, kFBOServiceTextureId);
   7077   DoTexImage2D(
   7078       GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   7079       kSharedMemoryId, kSharedMemoryOffset);
   7080   DoBindFramebuffer(
   7081       GL_FRAMEBUFFER, client_framebuffer_id_, kServiceFramebufferId);
   7082   DoFramebufferTexture2D(
   7083       GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
   7084       kFBOClientTextureId, kFBOServiceTextureId, 0, GL_NO_ERROR);
   7085 
   7086   // Setup "render from" texture.
   7087   SetupTexture();
   7088 
   7089   // Make sure we check for framebuffer complete.
   7090   EXPECT_CALL(*gl_, CheckFramebufferStatusEXT(GL_FRAMEBUFFER))
   7091       .WillOnce(Return(GL_FRAMEBUFFER_COMPLETE))
   7092       .RetiresOnSaturation();
   7093 
   7094   SetupExpectationsForApplyingDirtyState(
   7095       false,   // Framebuffer is RGB
   7096       false,   // Framebuffer has depth
   7097       false,   // Framebuffer has stencil
   7098       0x1111,  // color bits
   7099       false,   // depth mask
   7100       false,   // depth enabled
   7101       0,       // front stencil mask
   7102       0,       // back stencil mask
   7103       false,   // stencil enabled
   7104       false,   // cull_face_enabled
   7105       false,   // scissor_test_enabled
   7106       false);  // blend_enabled
   7107 
   7108   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   7109       .Times(1)
   7110       .RetiresOnSaturation();
   7111   DrawArrays cmd;
   7112   cmd.Init(GL_TRIANGLES, 0, kNumVertices);
   7113   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   7114   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   7115 
   7116   // But not again.
   7117   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   7118       .Times(1)
   7119       .RetiresOnSaturation();
   7120   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   7121   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   7122 }
   7123 
   7124 TEST_F(GLES2DecoderTest, BeginQueryEXTDisabled) {
   7125   // Test something fails if off.
   7126 }
   7127 
   7128 TEST_F(GLES2DecoderManualInitTest, BeginEndQueryEXT) {
   7129   InitDecoder(
   7130       "GL_EXT_occlusion_query_boolean",      // extensions
   7131       true,    // has alpha
   7132       false,   // has depth
   7133       false,   // has stencil
   7134       true,    // request alpha
   7135       false,   // request depth
   7136       false,   // request stencil
   7137       true);   // bind generates resource
   7138 
   7139   // Test end fails if no begin.
   7140   EndQueryEXT end_cmd;
   7141   end_cmd.Init(GL_ANY_SAMPLES_PASSED_EXT, 1);
   7142   EXPECT_EQ(error::kNoError, ExecuteCmd(end_cmd));
   7143   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   7144 
   7145   BeginQueryEXT begin_cmd;
   7146 
   7147   // Test id = 0 fails.
   7148   begin_cmd.Init(
   7149       GL_ANY_SAMPLES_PASSED_EXT, 0, kSharedMemoryId, kSharedMemoryOffset);
   7150   EXPECT_EQ(error::kNoError, ExecuteCmd(begin_cmd));
   7151   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   7152 
   7153   GenHelper<GenQueriesEXTImmediate>(kNewClientId);
   7154 
   7155   // Test valid parameters work.
   7156   EXPECT_CALL(*gl_, GenQueriesARB(1, _))
   7157      .WillOnce(SetArgumentPointee<1>(kNewServiceId))
   7158      .RetiresOnSaturation();
   7159   EXPECT_CALL(*gl_, BeginQueryARB(GL_ANY_SAMPLES_PASSED_EXT, kNewServiceId))
   7160       .Times(1)
   7161       .RetiresOnSaturation();
   7162   begin_cmd.Init(
   7163       GL_ANY_SAMPLES_PASSED_EXT, kNewClientId,
   7164       kSharedMemoryId, kSharedMemoryOffset);
   7165   EXPECT_EQ(error::kNoError, ExecuteCmd(begin_cmd));
   7166   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   7167 
   7168   QueryManager* query_manager = decoder_->GetQueryManager();
   7169   ASSERT_TRUE(query_manager != NULL);
   7170   QueryManager::Query* query = query_manager->GetQuery(kNewClientId);
   7171   ASSERT_TRUE(query != NULL);
   7172   EXPECT_FALSE(query->pending());
   7173 
   7174   // Test trying begin again fails
   7175   EXPECT_EQ(error::kNoError, ExecuteCmd(begin_cmd));
   7176   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   7177 
   7178   // Test end fails with different target
   7179   end_cmd.Init(GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT, 1);
   7180   EXPECT_EQ(error::kNoError, ExecuteCmd(end_cmd));
   7181   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   7182 
   7183   // Test end succeeds
   7184   EXPECT_CALL(*gl_, EndQueryARB(GL_ANY_SAMPLES_PASSED_EXT))
   7185       .Times(1)
   7186       .RetiresOnSaturation();
   7187   end_cmd.Init(GL_ANY_SAMPLES_PASSED_EXT, 1);
   7188   EXPECT_EQ(error::kNoError, ExecuteCmd(end_cmd));
   7189   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   7190   EXPECT_TRUE(query->pending());
   7191 
   7192   EXPECT_CALL(*gl_, DeleteQueriesARB(1, _))
   7193       .Times(1)
   7194       .RetiresOnSaturation();
   7195 }
   7196 
   7197 static void CheckBeginEndQueryBadMemoryFails(
   7198     GLES2DecoderTestBase* test,
   7199     GLuint client_id,
   7200     GLuint service_id,
   7201     int32 shm_id,
   7202     uint32 shm_offset) {
   7203   ::testing::StrictMock< ::gfx::MockGLInterface>* gl = test->GetGLMock();
   7204 
   7205   BeginQueryEXT begin_cmd;
   7206 
   7207   test->GenHelper<GenQueriesEXTImmediate>(client_id);
   7208 
   7209   EXPECT_CALL(*gl, GenQueriesARB(1, _))
   7210      .WillOnce(SetArgumentPointee<1>(service_id))
   7211      .RetiresOnSaturation();
   7212   EXPECT_CALL(*gl, BeginQueryARB(GL_ANY_SAMPLES_PASSED_EXT, service_id))
   7213       .Times(1)
   7214       .RetiresOnSaturation();
   7215 
   7216   // Test bad shared memory fails
   7217   begin_cmd.Init(GL_ANY_SAMPLES_PASSED_EXT, client_id, shm_id, shm_offset);
   7218   error::Error error1 = test->ExecuteCmd(begin_cmd);
   7219 
   7220   EXPECT_CALL(*gl, EndQueryARB(GL_ANY_SAMPLES_PASSED_EXT))
   7221       .Times(1)
   7222       .RetiresOnSaturation();
   7223 
   7224   EndQueryEXT end_cmd;
   7225   end_cmd.Init(GL_ANY_SAMPLES_PASSED_EXT, 1);
   7226   error::Error error2 = test->ExecuteCmd(end_cmd);
   7227 
   7228   EXPECT_CALL(*gl,
   7229       GetQueryObjectuivARB(service_id, GL_QUERY_RESULT_AVAILABLE_EXT, _))
   7230       .WillOnce(SetArgumentPointee<2>(1))
   7231       .RetiresOnSaturation();
   7232   EXPECT_CALL(*gl,
   7233       GetQueryObjectuivARB(service_id, GL_QUERY_RESULT_EXT, _))
   7234       .WillOnce(SetArgumentPointee<2>(1))
   7235       .RetiresOnSaturation();
   7236 
   7237   QueryManager* query_manager = test->GetDecoder()->GetQueryManager();
   7238   ASSERT_TRUE(query_manager != NULL);
   7239   bool process_success = query_manager->ProcessPendingQueries();
   7240 
   7241   EXPECT_TRUE(error1 != error::kNoError ||
   7242               error2 != error::kNoError ||
   7243               !process_success);
   7244 
   7245   EXPECT_CALL(*gl, DeleteQueriesARB(1, _))
   7246       .Times(1)
   7247       .RetiresOnSaturation();
   7248 }
   7249 
   7250 TEST_F(GLES2DecoderManualInitTest, BeginEndQueryEXTBadMemoryIdFails) {
   7251   InitDecoder(
   7252       "GL_EXT_occlusion_query_boolean",      // extensions
   7253       true,    // has alpha
   7254       false,   // has depth
   7255       false,   // has stencil
   7256       true,    // request alpha
   7257       false,   // request depth
   7258       false,   // request stencil
   7259       true);   // bind generates resource
   7260 
   7261   CheckBeginEndQueryBadMemoryFails(
   7262       this, kNewClientId, kNewServiceId,
   7263       kInvalidSharedMemoryId, kSharedMemoryOffset);
   7264 }
   7265 
   7266 TEST_F(GLES2DecoderManualInitTest, BeginEndQueryEXTBadMemoryOffsetFails) {
   7267   InitDecoder(
   7268       "GL_EXT_occlusion_query_boolean",      // extensions
   7269       true,    // has alpha
   7270       false,   // has depth
   7271       false,   // has stencil
   7272       true,    // request alpha
   7273       false,   // request depth
   7274       false,   // request stencil
   7275       true);   // bind generates resource
   7276 
   7277   CheckBeginEndQueryBadMemoryFails(
   7278       this, kNewClientId, kNewServiceId,
   7279       kSharedMemoryId, kInvalidSharedMemoryOffset);
   7280 }
   7281 
   7282 TEST_F(GLES2DecoderTest, BeginEndQueryEXTCommandsIssuedCHROMIUM) {
   7283   BeginQueryEXT begin_cmd;
   7284 
   7285   GenHelper<GenQueriesEXTImmediate>(kNewClientId);
   7286 
   7287   // Test valid parameters work.
   7288   begin_cmd.Init(
   7289       GL_COMMANDS_ISSUED_CHROMIUM, kNewClientId,
   7290       kSharedMemoryId, kSharedMemoryOffset);
   7291   EXPECT_EQ(error::kNoError, ExecuteCmd(begin_cmd));
   7292   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   7293 
   7294   QueryManager* query_manager = decoder_->GetQueryManager();
   7295   ASSERT_TRUE(query_manager != NULL);
   7296   QueryManager::Query* query = query_manager->GetQuery(kNewClientId);
   7297   ASSERT_TRUE(query != NULL);
   7298   EXPECT_FALSE(query->pending());
   7299 
   7300   // Test end succeeds
   7301   EndQueryEXT end_cmd;
   7302   end_cmd.Init(GL_COMMANDS_ISSUED_CHROMIUM, 1);
   7303   EXPECT_EQ(error::kNoError, ExecuteCmd(end_cmd));
   7304   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   7305   EXPECT_FALSE(query->pending());
   7306 }
   7307 
   7308 TEST_F(GLES2DecoderTest, BeginEndQueryEXTGetErrorQueryCHROMIUM) {
   7309   BeginQueryEXT begin_cmd;
   7310 
   7311   GenHelper<GenQueriesEXTImmediate>(kNewClientId);
   7312 
   7313   // Test valid parameters work.
   7314   begin_cmd.Init(
   7315       GL_GET_ERROR_QUERY_CHROMIUM, kNewClientId,
   7316       kSharedMemoryId, kSharedMemoryOffset);
   7317   EXPECT_EQ(error::kNoError, ExecuteCmd(begin_cmd));
   7318   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   7319 
   7320   QueryManager* query_manager = decoder_->GetQueryManager();
   7321   ASSERT_TRUE(query_manager != NULL);
   7322   QueryManager::Query* query = query_manager->GetQuery(kNewClientId);
   7323   ASSERT_TRUE(query != NULL);
   7324   EXPECT_FALSE(query->pending());
   7325 
   7326   // Test end succeeds
   7327   QuerySync* sync = static_cast<QuerySync*>(shared_memory_address_);
   7328 
   7329   EXPECT_CALL(*gl_, GetError())
   7330       .WillOnce(Return(GL_INVALID_VALUE))
   7331       .RetiresOnSaturation();
   7332 
   7333   EndQueryEXT end_cmd;
   7334   end_cmd.Init(GL_GET_ERROR_QUERY_CHROMIUM, 1);
   7335   EXPECT_EQ(error::kNoError, ExecuteCmd(end_cmd));
   7336   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   7337   EXPECT_FALSE(query->pending());
   7338   EXPECT_EQ(static_cast<GLenum>(GL_INVALID_VALUE),
   7339             static_cast<GLenum>(sync->result));
   7340 }
   7341 
   7342 TEST_F(GLES2DecoderTest, GenMailboxCHROMIUM) {
   7343   const uint32 kBucketId = 123;
   7344 
   7345   GenMailboxCHROMIUM gen_mailbox_cmd;
   7346   gen_mailbox_cmd.Init(kBucketId);
   7347   EXPECT_EQ(error::kNoError, ExecuteCmd(gen_mailbox_cmd));
   7348 
   7349   CommonDecoder::Bucket* bucket = decoder_->GetBucket(kBucketId);
   7350   ASSERT_TRUE(bucket != NULL);
   7351   ASSERT_EQ(static_cast<uint32>(GL_MAILBOX_SIZE_CHROMIUM), bucket->size());
   7352 
   7353   static const GLbyte zero[GL_MAILBOX_SIZE_CHROMIUM] = {
   7354     0
   7355   };
   7356   EXPECT_NE(0, memcmp(zero,
   7357                       bucket->GetData(0, GL_MAILBOX_SIZE_CHROMIUM),
   7358                       sizeof(zero)));
   7359 }
   7360 
   7361 TEST_F(GLES2DecoderTest, ProduceAndConsumeTextureCHROMIUM) {
   7362   GLbyte mailbox[GL_MAILBOX_SIZE_CHROMIUM];
   7363   group().mailbox_manager()->GenerateMailboxName(
   7364       reinterpret_cast<MailboxName*>(mailbox));
   7365 
   7366   memcpy(shared_memory_address_, mailbox, sizeof(mailbox));
   7367 
   7368   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   7369   DoTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 3, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   7370                0, 0);
   7371   DoTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA, 2, 4, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   7372                0, 0);
   7373   TextureRef* texture_ref = group().texture_manager()->GetTexture(
   7374       client_texture_id_);
   7375   ASSERT_TRUE(texture_ref != NULL);
   7376   Texture* texture = texture_ref->texture();
   7377   EXPECT_EQ(kServiceTextureId, texture->service_id());
   7378 
   7379   ProduceTextureCHROMIUM produce_cmd;
   7380   produce_cmd.Init(GL_TEXTURE_2D, kSharedMemoryId, kSharedMemoryOffset);
   7381   EXPECT_EQ(error::kNoError, ExecuteCmd(produce_cmd));
   7382   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   7383 
   7384   // Texture didn't change.
   7385   GLsizei width;
   7386   GLsizei height;
   7387   GLenum type;
   7388   GLenum internal_format;
   7389 
   7390   EXPECT_TRUE(texture->GetLevelSize(GL_TEXTURE_2D, 0, &width, &height));
   7391   EXPECT_EQ(3, width);
   7392   EXPECT_EQ(1, height);
   7393   EXPECT_TRUE(texture->GetLevelType(GL_TEXTURE_2D, 0, &type, &internal_format));
   7394   EXPECT_EQ(static_cast<GLenum>(GL_RGBA), internal_format);
   7395   EXPECT_EQ(static_cast<GLenum>(GL_UNSIGNED_BYTE), type);
   7396 
   7397   EXPECT_TRUE(texture->GetLevelSize(GL_TEXTURE_2D, 1, &width, &height));
   7398   EXPECT_EQ(2, width);
   7399   EXPECT_EQ(4, height);
   7400   EXPECT_TRUE(texture->GetLevelType(GL_TEXTURE_2D, 1, &type, &internal_format));
   7401   EXPECT_EQ(static_cast<GLenum>(GL_RGBA), internal_format);
   7402   EXPECT_EQ(static_cast<GLenum>(GL_UNSIGNED_BYTE), type);
   7403 
   7404   // Service ID has not changed.
   7405   EXPECT_EQ(kServiceTextureId, texture->service_id());
   7406 
   7407   // Create new texture for consume.
   7408   EXPECT_CALL(*gl_, GenTextures(_, _))
   7409       .WillOnce(SetArgumentPointee<1>(kNewServiceId))
   7410       .RetiresOnSaturation();
   7411   DoBindTexture(GL_TEXTURE_2D, kNewClientId, kNewServiceId);
   7412 
   7413   // Assigns and binds original service size texture ID.
   7414   EXPECT_CALL(*gl_, DeleteTextures(1, _))
   7415       .Times(1)
   7416       .RetiresOnSaturation();
   7417   EXPECT_CALL(*gl_, BindTexture(GL_TEXTURE_2D, kServiceTextureId))
   7418       .Times(1)
   7419       .RetiresOnSaturation();
   7420 
   7421   memcpy(shared_memory_address_, mailbox, sizeof(mailbox));
   7422   ConsumeTextureCHROMIUM consume_cmd;
   7423   consume_cmd.Init(GL_TEXTURE_2D, kSharedMemoryId, kSharedMemoryOffset);
   7424   EXPECT_EQ(error::kNoError, ExecuteCmd(consume_cmd));
   7425   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   7426 
   7427   // Texture is redefined.
   7428   EXPECT_TRUE(texture->GetLevelSize(GL_TEXTURE_2D, 0, &width, &height));
   7429   EXPECT_EQ(3, width);
   7430   EXPECT_EQ(1, height);
   7431   EXPECT_TRUE(texture->GetLevelType(GL_TEXTURE_2D, 0, &type, &internal_format));
   7432   EXPECT_EQ(static_cast<GLenum>(GL_RGBA), internal_format);
   7433   EXPECT_EQ(static_cast<GLenum>(GL_UNSIGNED_BYTE), type);
   7434 
   7435   EXPECT_TRUE(texture->GetLevelSize(GL_TEXTURE_2D, 1, &width, &height));
   7436   EXPECT_EQ(2, width);
   7437   EXPECT_EQ(4, height);
   7438   EXPECT_TRUE(texture->GetLevelType(GL_TEXTURE_2D, 1, &type, &internal_format));
   7439   EXPECT_EQ(static_cast<GLenum>(GL_RGBA), internal_format);
   7440   EXPECT_EQ(static_cast<GLenum>(GL_UNSIGNED_BYTE), type);
   7441 
   7442   // Service ID is restored.
   7443   EXPECT_EQ(kServiceTextureId, texture->service_id());
   7444 }
   7445 
   7446 
   7447 TEST_F(GLES2DecoderTest, CanChangeSurface) {
   7448   scoped_refptr<GLSurfaceMock> other_surface(new GLSurfaceMock);
   7449   EXPECT_CALL(*other_surface.get(), GetBackingFrameBufferObject()).
   7450       WillOnce(Return(7));
   7451   EXPECT_CALL(*gl_, BindFramebufferEXT(GL_FRAMEBUFFER_EXT, 7));
   7452 
   7453   decoder_->SetSurface(other_surface);
   7454 }
   7455 
   7456 TEST_F(GLES2DecoderTest, IsEnabledReturnsCachedValue) {
   7457   // NOTE: There are no expectations because no GL functions should be
   7458   // called for DEPTH_TEST or STENCIL_TEST
   7459   static const GLenum kStates[] = {
   7460     GL_DEPTH_TEST,
   7461     GL_STENCIL_TEST,
   7462   };
   7463   for (size_t ii = 0; ii < arraysize(kStates); ++ii) {
   7464     Enable enable_cmd;
   7465     GLenum state = kStates[ii];
   7466     enable_cmd.Init(state);
   7467     EXPECT_EQ(error::kNoError, ExecuteCmd(enable_cmd));
   7468     IsEnabled::Result* result =
   7469         static_cast<IsEnabled::Result*>(shared_memory_address_);
   7470     IsEnabled is_enabled_cmd;
   7471     is_enabled_cmd.Init(state, shared_memory_id_, shared_memory_offset_);
   7472     EXPECT_EQ(error::kNoError, ExecuteCmd(is_enabled_cmd));
   7473     EXPECT_NE(0u, *result);
   7474     Disable disable_cmd;
   7475     disable_cmd.Init(state);
   7476     EXPECT_EQ(error::kNoError, ExecuteCmd(disable_cmd));
   7477     EXPECT_EQ(error::kNoError, ExecuteCmd(is_enabled_cmd));
   7478     EXPECT_EQ(0u, *result);
   7479   }
   7480 }
   7481 
   7482 TEST_F(GLES2DecoderManualInitTest, DepthTextureBadArgs) {
   7483   InitDecoder(
   7484       "GL_ANGLE_depth_texture",      // extensions
   7485       false,   // has alpha
   7486       true,    // has depth
   7487       true,    // has stencil
   7488       false,   // request alpha
   7489       true,    // request depth
   7490       true,    // request stencil
   7491       true);   // bind generates resource
   7492 
   7493   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   7494   // Check trying to upload data fails.
   7495   TexImage2D tex_cmd;
   7496   tex_cmd.Init(
   7497       GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT,
   7498       1, 1, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT,
   7499       kSharedMemoryId, kSharedMemoryOffset);
   7500   EXPECT_EQ(error::kNoError, ExecuteCmd(tex_cmd));
   7501   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   7502   // Try level > 0.
   7503   tex_cmd.Init(
   7504       GL_TEXTURE_2D, 1, GL_DEPTH_COMPONENT,
   7505       1, 1, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, 0, 0);
   7506   EXPECT_EQ(error::kNoError, ExecuteCmd(tex_cmd));
   7507   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   7508   // Make a 1 pixel depth texture.
   7509   DoTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT,
   7510                1, 1, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, 0, 0);
   7511   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   7512 
   7513   // Check that trying to update it fails.
   7514   TexSubImage2D tex_sub_cmd;
   7515   tex_sub_cmd.Init(
   7516       GL_TEXTURE_2D, 0, 0, 0, 1, 1, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT,
   7517       kSharedMemoryId, kSharedMemoryOffset, GL_FALSE);
   7518   EXPECT_EQ(error::kNoError, ExecuteCmd(tex_sub_cmd));
   7519   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   7520 
   7521   // Check that trying to CopyTexImage2D fails
   7522   CopyTexImage2D copy_tex_cmd;
   7523   copy_tex_cmd.Init(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 0, 0, 1, 1, 0);
   7524   EXPECT_EQ(error::kNoError, ExecuteCmd(copy_tex_cmd));
   7525   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   7526 
   7527   // Check that trying to CopyTexSubImage2D fails
   7528   CopyTexSubImage2D copy_sub_cmd;
   7529   copy_sub_cmd.Init(GL_TEXTURE_2D, 0, 0, 0, 0, 0, 1, 1);
   7530   EXPECT_EQ(error::kNoError, ExecuteCmd(copy_sub_cmd));
   7531   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   7532 }
   7533 
   7534 TEST_F(GLES2DecoderManualInitTest, GenerateMipmapDepthTexture) {
   7535   InitDecoder(
   7536       "GL_ANGLE_depth_texture",      // extensions
   7537       false,   // has alpha
   7538       true,    // has depth
   7539       true,    // has stencil
   7540       false,   // request alpha
   7541       true,    // request depth
   7542       true,    // request stencil
   7543       true);   // bind generates resource
   7544   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   7545   DoTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT,
   7546                2, 2, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT,
   7547                0, 0);
   7548   GenerateMipmap cmd;
   7549   cmd.Init(GL_TEXTURE_2D);
   7550   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   7551   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   7552 }
   7553 
   7554 TEST_F(GLES2DecoderANGLEManualInitTest, DrawClearsDepthTexture) {
   7555   InitDecoder(
   7556       "GL_ANGLE_depth_texture",      // extensions
   7557       true,    // has alpha
   7558       true,    // has depth
   7559       false,   // has stencil
   7560       true,    // request alpha
   7561       true,    // request depth
   7562       false,   // request stencil
   7563       true);   // bind generates resource
   7564 
   7565   SetupDefaultProgram();
   7566   SetupAllNeededVertexBuffers();
   7567   const GLenum attachment = GL_DEPTH_ATTACHMENT;
   7568   const GLenum target = GL_TEXTURE_2D;
   7569   const GLint level = 0;
   7570   DoBindTexture(target, client_texture_id_, kServiceTextureId);
   7571 
   7572   // Create a depth texture.
   7573   DoTexImage2D(target, level, GL_DEPTH_COMPONENT, 1, 1, 0,
   7574                GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, 0, 0);
   7575 
   7576   EXPECT_CALL(*gl_, GenFramebuffersEXT(1, _))
   7577       .Times(1)
   7578       .RetiresOnSaturation();
   7579   EXPECT_CALL(*gl_, BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, _))
   7580       .Times(1)
   7581       .RetiresOnSaturation();
   7582 
   7583   EXPECT_CALL(*gl_, FramebufferTexture2DEXT(
   7584       GL_DRAW_FRAMEBUFFER_EXT, attachment, target, kServiceTextureId, level))
   7585       .Times(1)
   7586       .RetiresOnSaturation();
   7587   EXPECT_CALL(*gl_, CheckFramebufferStatusEXT(GL_DRAW_FRAMEBUFFER_EXT))
   7588       .WillOnce(Return(GL_FRAMEBUFFER_COMPLETE))
   7589       .RetiresOnSaturation();
   7590 
   7591   EXPECT_CALL(*gl_, ClearStencil(0))
   7592       .Times(1)
   7593       .RetiresOnSaturation();
   7594   EXPECT_CALL(*gl_, StencilMask(-1))
   7595       .Times(1)
   7596       .RetiresOnSaturation();
   7597   EXPECT_CALL(*gl_, ClearDepth(1.0f))
   7598       .Times(1)
   7599       .RetiresOnSaturation();
   7600   EXPECT_CALL(*gl_, DepthMask(true))
   7601       .Times(1)
   7602       .RetiresOnSaturation();
   7603   EXPECT_CALL(*gl_, Disable(GL_SCISSOR_TEST))
   7604       .Times(1)
   7605       .RetiresOnSaturation();
   7606 
   7607   EXPECT_CALL(*gl_, Clear(GL_DEPTH_BUFFER_BIT))
   7608       .Times(1)
   7609       .RetiresOnSaturation();
   7610 
   7611   SetupExpectationsForRestoreClearState(
   7612       0.0f, 0.0f, 0.0f, 0.0f, 0, 1.0f, false);
   7613 
   7614   EXPECT_CALL(*gl_, DeleteFramebuffersEXT(1, _))
   7615       .Times(1)
   7616       .RetiresOnSaturation();
   7617   EXPECT_CALL(*gl_, BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, 0))
   7618       .Times(1)
   7619       .RetiresOnSaturation();
   7620 
   7621   SetupExpectationsForApplyingDefaultDirtyState();
   7622   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   7623       .Times(1)
   7624       .RetiresOnSaturation();
   7625   DrawArrays cmd;
   7626   cmd.Init(GL_TRIANGLES, 0, kNumVertices);
   7627   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   7628   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   7629 }
   7630 
   7631 TEST_F(GLES2DecoderWithShaderTest, BindUniformLocationCHROMIUM) {
   7632   const GLint kLocation = 2;
   7633   const char* kName = "testing";
   7634   const uint32 kNameSize = strlen(kName);
   7635   const char* kBadName1 = "gl_testing";
   7636   const uint32 kBadName1Size = strlen(kBadName1);
   7637   const char* kBadName2 = "testing[1]";
   7638   const uint32 kBadName2Size = strlen(kBadName2);
   7639   memcpy(shared_memory_address_, kName, kNameSize);
   7640   BindUniformLocationCHROMIUM cmd;
   7641   cmd.Init(client_program_id_, kLocation, kSharedMemoryId, kSharedMemoryOffset,
   7642            kNameSize);
   7643   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   7644   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   7645   // check negative location
   7646   memcpy(shared_memory_address_, kName, kNameSize);
   7647   cmd.Init(client_program_id_, -1, kSharedMemoryId, kSharedMemoryOffset,
   7648            kNameSize);
   7649   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   7650   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   7651   // check highest location
   7652   memcpy(shared_memory_address_, kName, kNameSize);
   7653   GLint kMaxLocation =
   7654       (kMaxFragmentUniformVectors + kMaxVertexUniformVectors) * 4 - 1;
   7655   cmd.Init(client_program_id_, kMaxLocation, kSharedMemoryId,
   7656            kSharedMemoryOffset, kNameSize);
   7657   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   7658   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   7659   // check too high location
   7660   memcpy(shared_memory_address_, kName, kNameSize);
   7661   cmd.Init(client_program_id_, kMaxLocation + 1, kSharedMemoryId,
   7662            kSharedMemoryOffset, kNameSize);
   7663   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   7664   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   7665   // check bad name "gl_..."
   7666   memcpy(shared_memory_address_, kBadName1, kBadName1Size);
   7667   cmd.Init(client_program_id_, kLocation, kSharedMemoryId, kSharedMemoryOffset,
   7668            kBadName1Size);
   7669   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   7670   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   7671   // check bad name "name[1]" non zero
   7672   memcpy(shared_memory_address_, kBadName2, kBadName2Size);
   7673   cmd.Init(client_program_id_, kLocation, kSharedMemoryId, kSharedMemoryOffset,
   7674            kBadName2Size);
   7675   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   7676   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   7677 }
   7678 
   7679 class GLES2DecoderVertexArraysOESTest : public GLES2DecoderWithShaderTest {
   7680  public:
   7681   GLES2DecoderVertexArraysOESTest() { }
   7682 
   7683   bool vertex_array_deleted_manually_;
   7684 
   7685   virtual void SetUp() {
   7686     InitDecoder(
   7687         "GL_OES_vertex_array_object",  // extensions
   7688         false,  // has alpha
   7689         false,  // has depth
   7690         false,  // has stencil
   7691         false,  // request alpha
   7692         false,  // request depth
   7693         false,  // request stencil
   7694         true);  // bind generates resource
   7695     SetupDefaultProgram();
   7696 
   7697     AddExpectationsForGenVertexArraysOES();
   7698     GenHelper<GenVertexArraysOESImmediate>(client_vertexarray_id_);
   7699 
   7700     vertex_array_deleted_manually_ = false;
   7701   }
   7702 
   7703   virtual void TearDown() {
   7704     // This should only be set if the test handled deletion of the vertex array
   7705     // itself. Necessary because vertex_array_objects are not sharable, and thus
   7706     // not managed in the ContextGroup, meaning they will be destroyed during
   7707     // test tear down
   7708     if (!vertex_array_deleted_manually_) {
   7709       AddExpectationsForDeleteVertexArraysOES();
   7710     }
   7711 
   7712     GLES2DecoderWithShaderTest::TearDown();
   7713   }
   7714 
   7715   void GenVertexArraysOESValidArgs() {
   7716     AddExpectationsForGenVertexArraysOES();
   7717     GetSharedMemoryAs<GLuint*>()[0] = kNewClientId;
   7718     GenVertexArraysOES cmd;
   7719     cmd.Init(1, shared_memory_id_, shared_memory_offset_);
   7720     EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   7721     EXPECT_EQ(GL_NO_ERROR, GetGLError());
   7722     EXPECT_TRUE(GetVertexArrayInfo(kNewClientId) != NULL);
   7723     AddExpectationsForDeleteVertexArraysOES();
   7724   }
   7725 
   7726   void GenVertexArraysOESInvalidArgs() {
   7727     EXPECT_CALL(*gl_, GenVertexArraysOES(_, _)).Times(0);
   7728     GetSharedMemoryAs<GLuint*>()[0] = client_vertexarray_id_;
   7729     GenVertexArraysOES cmd;
   7730     cmd.Init(1, shared_memory_id_, shared_memory_offset_);
   7731     EXPECT_EQ(error::kInvalidArguments, ExecuteCmd(cmd));
   7732   }
   7733 
   7734   void GenVertexArraysOESImmediateValidArgs() {
   7735     AddExpectationsForGenVertexArraysOES();
   7736     GenVertexArraysOESImmediate* cmd =
   7737         GetImmediateAs<GenVertexArraysOESImmediate>();
   7738     GLuint temp = kNewClientId;
   7739     cmd->Init(1, &temp);
   7740     EXPECT_EQ(error::kNoError,
   7741               ExecuteImmediateCmd(*cmd, sizeof(temp)));
   7742     EXPECT_EQ(GL_NO_ERROR, GetGLError());
   7743     EXPECT_TRUE(GetVertexArrayInfo(kNewClientId) != NULL);
   7744     AddExpectationsForDeleteVertexArraysOES();
   7745   }
   7746 
   7747   void GenVertexArraysOESImmediateInvalidArgs() {
   7748     EXPECT_CALL(*gl_, GenVertexArraysOES(_, _)).Times(0);
   7749     GenVertexArraysOESImmediate* cmd =
   7750         GetImmediateAs<GenVertexArraysOESImmediate>();
   7751     cmd->Init(1, &client_vertexarray_id_);
   7752     EXPECT_EQ(error::kInvalidArguments,
   7753               ExecuteImmediateCmd(*cmd, sizeof(&client_vertexarray_id_)));
   7754   }
   7755 
   7756   void DeleteVertexArraysOESValidArgs() {
   7757     AddExpectationsForDeleteVertexArraysOES();
   7758     GetSharedMemoryAs<GLuint*>()[0] = client_vertexarray_id_;
   7759     DeleteVertexArraysOES cmd;
   7760     cmd.Init(1, shared_memory_id_, shared_memory_offset_);
   7761     EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   7762     EXPECT_EQ(GL_NO_ERROR, GetGLError());
   7763     EXPECT_TRUE(
   7764         GetVertexArrayInfo(client_vertexarray_id_) == NULL);
   7765     vertex_array_deleted_manually_ = true;
   7766   }
   7767 
   7768   void DeleteVertexArraysOESInvalidArgs() {
   7769     GetSharedMemoryAs<GLuint*>()[0] = kInvalidClientId;
   7770     DeleteVertexArraysOES cmd;
   7771     cmd.Init(1, shared_memory_id_, shared_memory_offset_);
   7772     EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   7773   }
   7774 
   7775   void DeleteVertexArraysOESImmediateValidArgs() {
   7776     AddExpectationsForDeleteVertexArraysOES();
   7777     DeleteVertexArraysOESImmediate& cmd =
   7778         *GetImmediateAs<DeleteVertexArraysOESImmediate>();
   7779     cmd.Init(1, &client_vertexarray_id_);
   7780     EXPECT_EQ(error::kNoError,
   7781               ExecuteImmediateCmd(cmd, sizeof(client_vertexarray_id_)));
   7782     EXPECT_EQ(GL_NO_ERROR, GetGLError());
   7783     EXPECT_TRUE(
   7784         GetVertexArrayInfo(client_vertexarray_id_) == NULL);
   7785     vertex_array_deleted_manually_ = true;
   7786   }
   7787 
   7788   void DeleteVertexArraysOESImmediateInvalidArgs() {
   7789     DeleteVertexArraysOESImmediate& cmd =
   7790         *GetImmediateAs<DeleteVertexArraysOESImmediate>();
   7791     GLuint temp = kInvalidClientId;
   7792     cmd.Init(1, &temp);
   7793     EXPECT_EQ(error::kNoError,
   7794               ExecuteImmediateCmd(cmd, sizeof(temp)));
   7795   }
   7796 
   7797   void IsVertexArrayOESValidArgs() {
   7798     IsVertexArrayOES cmd;
   7799     cmd.Init(client_vertexarray_id_, shared_memory_id_, shared_memory_offset_);
   7800     EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   7801     EXPECT_EQ(GL_NO_ERROR, GetGLError());
   7802   }
   7803 
   7804   void IsVertexArrayOESInvalidArgsBadSharedMemoryId() {
   7805     IsVertexArrayOES cmd;
   7806     cmd.Init(
   7807         client_vertexarray_id_, kInvalidSharedMemoryId, shared_memory_offset_);
   7808     EXPECT_EQ(error::kOutOfBounds, ExecuteCmd(cmd));
   7809     cmd.Init(
   7810         client_vertexarray_id_, shared_memory_id_, kInvalidSharedMemoryOffset);
   7811     EXPECT_EQ(error::kOutOfBounds, ExecuteCmd(cmd));
   7812   }
   7813 
   7814   void BindVertexArrayOESValidArgs() {
   7815     AddExpectationsForBindVertexArrayOES();
   7816     BindVertexArrayOES cmd;
   7817     cmd.Init(client_vertexarray_id_);
   7818     EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   7819     EXPECT_EQ(GL_NO_ERROR, GetGLError());
   7820   }
   7821 
   7822   void BindVertexArrayOESValidArgsNewId() {
   7823     BindVertexArrayOES cmd;
   7824     cmd.Init(kNewClientId);
   7825     EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   7826     EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   7827   }
   7828 };
   7829 
   7830 class GLES2DecoderEmulatedVertexArraysOESTest
   7831     : public GLES2DecoderVertexArraysOESTest {
   7832  public:
   7833   GLES2DecoderEmulatedVertexArraysOESTest() { }
   7834 
   7835   virtual void SetUp() {
   7836     InitDecoder(
   7837         "",     // extensions
   7838         false,  // has alpha
   7839         false,  // has depth
   7840         false,  // has stencil
   7841         false,  // request alpha
   7842         false,  // request depth
   7843         false,  // request stencil
   7844         true);  // bind generates resource
   7845     SetupDefaultProgram();
   7846 
   7847     AddExpectationsForGenVertexArraysOES();
   7848     GenHelper<GenVertexArraysOESImmediate>(client_vertexarray_id_);
   7849 
   7850     vertex_array_deleted_manually_ = false;
   7851   }
   7852 };
   7853 
   7854 // Test vertex array objects with native support
   7855 TEST_F(GLES2DecoderVertexArraysOESTest, GenVertexArraysOESValidArgs) {
   7856   GenVertexArraysOESValidArgs();
   7857 }
   7858 TEST_F(GLES2DecoderEmulatedVertexArraysOESTest, GenVertexArraysOESValidArgs) {
   7859   GenVertexArraysOESValidArgs();
   7860 }
   7861 
   7862 TEST_F(GLES2DecoderVertexArraysOESTest, GenVertexArraysOESInvalidArgs) {
   7863   GenVertexArraysOESInvalidArgs();
   7864 }
   7865 TEST_F(GLES2DecoderEmulatedVertexArraysOESTest, ) {
   7866   GenVertexArraysOESInvalidArgs();
   7867 }
   7868 
   7869 TEST_F(GLES2DecoderVertexArraysOESTest, GenVertexArraysOESImmediateValidArgs) {
   7870   GenVertexArraysOESImmediateValidArgs();
   7871 }
   7872 TEST_F(GLES2DecoderEmulatedVertexArraysOESTest,
   7873     GenVertexArraysOESImmediateValidArgs) {
   7874   GenVertexArraysOESImmediateValidArgs();
   7875 }
   7876 
   7877 TEST_F(GLES2DecoderVertexArraysOESTest,
   7878     GenVertexArraysOESImmediateInvalidArgs) {
   7879   GenVertexArraysOESImmediateInvalidArgs();
   7880 }
   7881 TEST_F(GLES2DecoderEmulatedVertexArraysOESTest,
   7882     GenVertexArraysOESImmediateInvalidArgs) {
   7883   GenVertexArraysOESImmediateInvalidArgs();
   7884 }
   7885 
   7886 TEST_F(GLES2DecoderVertexArraysOESTest, DeleteVertexArraysOESValidArgs) {
   7887   DeleteVertexArraysOESValidArgs();
   7888 }
   7889 TEST_F(GLES2DecoderEmulatedVertexArraysOESTest,
   7890     DeleteVertexArraysOESValidArgs) {
   7891   DeleteVertexArraysOESValidArgs();
   7892 }
   7893 
   7894 TEST_F(GLES2DecoderVertexArraysOESTest, DeleteVertexArraysOESInvalidArgs) {
   7895   DeleteVertexArraysOESInvalidArgs();
   7896 }
   7897 TEST_F(GLES2DecoderEmulatedVertexArraysOESTest,
   7898     DeleteVertexArraysOESInvalidArgs) {
   7899   DeleteVertexArraysOESInvalidArgs();
   7900 }
   7901 
   7902 TEST_F(GLES2DecoderVertexArraysOESTest,
   7903     DeleteVertexArraysOESImmediateValidArgs) {
   7904   DeleteVertexArraysOESImmediateValidArgs();
   7905 }
   7906 TEST_F(GLES2DecoderEmulatedVertexArraysOESTest,
   7907     DeleteVertexArraysOESImmediateValidArgs) {
   7908   DeleteVertexArraysOESImmediateValidArgs();
   7909 }
   7910 
   7911 TEST_F(GLES2DecoderVertexArraysOESTest,
   7912     DeleteVertexArraysOESImmediateInvalidArgs) {
   7913   DeleteVertexArraysOESImmediateInvalidArgs();
   7914 }
   7915 TEST_F(GLES2DecoderEmulatedVertexArraysOESTest,
   7916     DeleteVertexArraysOESImmediateInvalidArgs) {
   7917   DeleteVertexArraysOESImmediateInvalidArgs();
   7918 }
   7919 
   7920 TEST_F(GLES2DecoderVertexArraysOESTest, IsVertexArrayOESValidArgs) {
   7921   IsVertexArrayOESValidArgs();
   7922 }
   7923 TEST_F(GLES2DecoderEmulatedVertexArraysOESTest, IsVertexArrayOESValidArgs) {
   7924   IsVertexArrayOESValidArgs();
   7925 }
   7926 
   7927 TEST_F(GLES2DecoderVertexArraysOESTest,
   7928     IsVertexArrayOESInvalidArgsBadSharedMemoryId) {
   7929   IsVertexArrayOESInvalidArgsBadSharedMemoryId();
   7930 }
   7931 TEST_F(GLES2DecoderEmulatedVertexArraysOESTest,
   7932     IsVertexArrayOESInvalidArgsBadSharedMemoryId) {
   7933   IsVertexArrayOESInvalidArgsBadSharedMemoryId();
   7934 }
   7935 
   7936 TEST_F(GLES2DecoderVertexArraysOESTest, BindVertexArrayOESValidArgs) {
   7937   BindVertexArrayOESValidArgs();
   7938 }
   7939 TEST_F(GLES2DecoderEmulatedVertexArraysOESTest, BindVertexArrayOESValidArgs) {
   7940   BindVertexArrayOESValidArgs();
   7941 }
   7942 
   7943 TEST_F(GLES2DecoderVertexArraysOESTest, BindVertexArrayOESValidArgsNewId) {
   7944   BindVertexArrayOESValidArgsNewId();
   7945 }
   7946 TEST_F(GLES2DecoderEmulatedVertexArraysOESTest,
   7947     BindVertexArrayOESValidArgsNewId) {
   7948   BindVertexArrayOESValidArgsNewId();
   7949 }
   7950 
   7951 TEST_F(GLES2DecoderTest, BindTexImage2DCHROMIUM) {
   7952   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   7953   DoTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 3, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   7954                0, 0);
   7955   TextureRef* texture_ref = group().texture_manager()->GetTexture(
   7956       client_texture_id_);
   7957   ASSERT_TRUE(texture_ref != NULL);
   7958   Texture* texture = texture_ref->texture();
   7959   EXPECT_EQ(kServiceTextureId, texture->service_id());
   7960 
   7961   group().image_manager()->AddImage(gfx::GLImage::CreateGLImage(0).get(), 1);
   7962   EXPECT_FALSE(group().image_manager()->LookupImage(1) == NULL);
   7963 
   7964   GLsizei width;
   7965   GLsizei height;
   7966   GLenum type;
   7967   GLenum internal_format;
   7968 
   7969   EXPECT_TRUE(texture->GetLevelSize(GL_TEXTURE_2D, 0, &width, &height));
   7970   EXPECT_EQ(3, width);
   7971   EXPECT_EQ(1, height);
   7972   EXPECT_TRUE(texture->GetLevelType(GL_TEXTURE_2D, 0, &type, &internal_format));
   7973   EXPECT_EQ(static_cast<GLenum>(GL_RGBA), internal_format);
   7974   EXPECT_EQ(static_cast<GLenum>(GL_UNSIGNED_BYTE), type);
   7975   EXPECT_TRUE(texture->GetLevelImage(GL_TEXTURE_2D, 0) == NULL);
   7976 
   7977   // Bind image to texture.
   7978   // ScopedGLErrorSuppressor calls GetError on its constructor and destructor.
   7979   EXPECT_CALL(*gl_, GetError())
   7980       .WillOnce(Return(GL_NO_ERROR))
   7981       .WillOnce(Return(GL_NO_ERROR))
   7982       .RetiresOnSaturation();
   7983   BindTexImage2DCHROMIUM bind_tex_image_2d_cmd;
   7984   bind_tex_image_2d_cmd.Init(GL_TEXTURE_2D, 1);
   7985   EXPECT_EQ(error::kNoError, ExecuteCmd(bind_tex_image_2d_cmd));
   7986   EXPECT_TRUE(texture->GetLevelSize(GL_TEXTURE_2D, 0, &width, &height));
   7987   // Image should now be set.
   7988   EXPECT_FALSE(texture->GetLevelImage(GL_TEXTURE_2D, 0) == NULL);
   7989 
   7990   // Define new texture image.
   7991   DoTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 3, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   7992                0, 0);
   7993   EXPECT_TRUE(texture->GetLevelSize(GL_TEXTURE_2D, 0, &width, &height));
   7994   // Image should no longer be set.
   7995   EXPECT_TRUE(texture->GetLevelImage(GL_TEXTURE_2D, 0) == NULL);
   7996 }
   7997 
   7998 TEST_F(GLES2DecoderTest, ReleaseTexImage2DCHROMIUM) {
   7999   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   8000   DoTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 3, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   8001                0, 0);
   8002   TextureRef* texture_ref = group().texture_manager()->GetTexture(
   8003       client_texture_id_);
   8004   ASSERT_TRUE(texture_ref != NULL);
   8005   Texture* texture = texture_ref->texture();
   8006   EXPECT_EQ(kServiceTextureId, texture->service_id());
   8007 
   8008   group().image_manager()->AddImage(gfx::GLImage::CreateGLImage(0).get(), 1);
   8009   EXPECT_FALSE(group().image_manager()->LookupImage(1) == NULL);
   8010 
   8011   GLsizei width;
   8012   GLsizei height;
   8013   GLenum type;
   8014   GLenum internal_format;
   8015 
   8016   EXPECT_TRUE(texture->GetLevelSize(GL_TEXTURE_2D, 0, &width, &height));
   8017   EXPECT_EQ(3, width);
   8018   EXPECT_EQ(1, height);
   8019   EXPECT_TRUE(texture->GetLevelType(GL_TEXTURE_2D, 0, &type, &internal_format));
   8020   EXPECT_EQ(static_cast<GLenum>(GL_RGBA), internal_format);
   8021   EXPECT_EQ(static_cast<GLenum>(GL_UNSIGNED_BYTE), type);
   8022   EXPECT_TRUE(texture->GetLevelImage(GL_TEXTURE_2D, 0) == NULL);
   8023 
   8024   // Bind image to texture.
   8025   // ScopedGLErrorSuppressor calls GetError on its constructor and destructor.
   8026   EXPECT_CALL(*gl_, GetError())
   8027       .WillOnce(Return(GL_NO_ERROR))
   8028       .WillOnce(Return(GL_NO_ERROR))
   8029       .RetiresOnSaturation();
   8030   BindTexImage2DCHROMIUM bind_tex_image_2d_cmd;
   8031   bind_tex_image_2d_cmd.Init(GL_TEXTURE_2D, 1);
   8032   EXPECT_EQ(error::kNoError, ExecuteCmd(bind_tex_image_2d_cmd));
   8033   EXPECT_TRUE(texture->GetLevelSize(GL_TEXTURE_2D, 0, &width, &height));
   8034   // Image should now be set.
   8035   EXPECT_FALSE(texture->GetLevelImage(GL_TEXTURE_2D, 0) == NULL);
   8036 
   8037   // Release image from texture.
   8038   // ScopedGLErrorSuppressor calls GetError on its constructor and destructor.
   8039   EXPECT_CALL(*gl_, GetError())
   8040       .WillOnce(Return(GL_NO_ERROR))
   8041       .WillOnce(Return(GL_NO_ERROR))
   8042       .RetiresOnSaturation();
   8043   ReleaseTexImage2DCHROMIUM release_tex_image_2d_cmd;
   8044   release_tex_image_2d_cmd.Init(GL_TEXTURE_2D, 1);
   8045   EXPECT_EQ(error::kNoError, ExecuteCmd(release_tex_image_2d_cmd));
   8046   EXPECT_TRUE(texture->GetLevelSize(GL_TEXTURE_2D, 0, &width, &height));
   8047   // Image should no longer be set.
   8048   EXPECT_TRUE(texture->GetLevelImage(GL_TEXTURE_2D, 0) == NULL);
   8049 }
   8050 
   8051 TEST_F(GLES2DecoderManualInitTest, GpuMemoryManagerCHROMIUM) {
   8052   InitDecoder(
   8053       "GL_ARB_texture_rectangle",  // extensions
   8054       false,   // has alpha
   8055       false,   // has depth
   8056       false,   // has stencil
   8057       false,   // request alpha
   8058       false,   // request depth
   8059       false,   // request stencil
   8060       true);   // bind generates resource
   8061 
   8062   Texture* texture = GetTexture(client_texture_id_)->texture();
   8063   EXPECT_TRUE(texture != NULL);
   8064   EXPECT_TRUE(texture->pool() == GL_TEXTURE_POOL_UNMANAGED_CHROMIUM);
   8065 
   8066   DoBindTexture(
   8067       GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   8068 
   8069   TexParameteri cmd;
   8070   cmd.Init(GL_TEXTURE_2D,
   8071            GL_TEXTURE_POOL_CHROMIUM,
   8072            GL_TEXTURE_POOL_UNMANAGED_CHROMIUM);
   8073   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   8074   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   8075 
   8076   cmd.Init(GL_TEXTURE_2D,
   8077            GL_TEXTURE_POOL_CHROMIUM,
   8078            GL_TEXTURE_POOL_MANAGED_CHROMIUM);
   8079   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   8080   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   8081 
   8082   EXPECT_TRUE(texture->pool() == GL_TEXTURE_POOL_MANAGED_CHROMIUM);
   8083 
   8084   cmd.Init(GL_TEXTURE_2D,
   8085            GL_TEXTURE_POOL_CHROMIUM,
   8086            GL_NONE);
   8087   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   8088   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
   8089 }
   8090 
   8091 TEST_F(GLES2DecoderManualInitTest, AsyncPixelTransfers) {
   8092   InitDecoder(
   8093       "GL_CHROMIUM_async_pixel_transfers",  // extensions
   8094       false, false, false,  // has alpha/depth/stencil
   8095       false, false, false,  // request alpha/depth/stencil
   8096       true);   // bind generates resource
   8097 
   8098   // Set up the texture.
   8099   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   8100   TextureRef* texture_ref = GetTexture(client_texture_id_);
   8101   Texture* texture = texture_ref->texture();
   8102 
   8103   // Set a mock Async delegate
   8104   StrictMock<gpu::MockAsyncPixelTransferManager>* manager =
   8105       new StrictMock<gpu::MockAsyncPixelTransferManager>;
   8106   manager->Initialize(group().texture_manager());
   8107   decoder_->SetAsyncPixelTransferManagerForTest(manager);
   8108   StrictMock<gpu::MockAsyncPixelTransferDelegate>* delegate = NULL;
   8109 
   8110   // Tex(Sub)Image2D upload commands.
   8111   AsyncTexImage2DCHROMIUM teximage_cmd;
   8112   teximage_cmd.Init(GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 0, GL_RGBA,
   8113                     GL_UNSIGNED_BYTE, kSharedMemoryId, kSharedMemoryOffset);
   8114   AsyncTexSubImage2DCHROMIUM texsubimage_cmd;
   8115   texsubimage_cmd.Init(GL_TEXTURE_2D, 0, 0, 0, 8, 8, GL_RGBA,
   8116                       GL_UNSIGNED_BYTE, kSharedMemoryId, kSharedMemoryOffset);
   8117   WaitAsyncTexImage2DCHROMIUM wait_cmd;
   8118   wait_cmd.Init(GL_TEXTURE_2D);
   8119 
   8120   // No transfer state exists initially.
   8121   EXPECT_FALSE(
   8122       decoder_->GetAsyncPixelTransferManager()->GetPixelTransferDelegate(
   8123           texture_ref));
   8124 
   8125   base::Closure bind_callback;
   8126 
   8127   // AsyncTexImage2D
   8128   {
   8129     // Create transfer state since it doesn't exist.
   8130     EXPECT_CALL(*manager, CreatePixelTransferDelegateImpl(texture_ref, _))
   8131         .WillOnce(Return(
   8132             delegate = new StrictMock<gpu::MockAsyncPixelTransferDelegate>))
   8133         .RetiresOnSaturation();
   8134     EXPECT_CALL(*delegate, AsyncTexImage2D(_, _, _))
   8135         .WillOnce(SaveArg<2>(&bind_callback))
   8136         .RetiresOnSaturation();
   8137     // Command succeeds.
   8138     EXPECT_EQ(error::kNoError, ExecuteCmd(teximage_cmd));
   8139     EXPECT_EQ(GL_NO_ERROR, GetGLError());
   8140     EXPECT_EQ(
   8141         delegate,
   8142         decoder_->GetAsyncPixelTransferManager()->GetPixelTransferDelegate(
   8143             texture_ref));
   8144     EXPECT_TRUE(texture->IsImmutable());
   8145     // The texture is safe but the level has not been defined yet.
   8146     EXPECT_TRUE(texture->SafeToRenderFrom());
   8147     GLsizei width, height;
   8148     EXPECT_FALSE(texture->GetLevelSize(GL_TEXTURE_2D, 0, &width, &height));
   8149   }
   8150   {
   8151     // Async redefinitions are not allowed!
   8152     // Command fails.
   8153     EXPECT_EQ(error::kNoError, ExecuteCmd(teximage_cmd));
   8154     EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   8155     EXPECT_EQ(
   8156         delegate,
   8157         decoder_->GetAsyncPixelTransferManager()->GetPixelTransferDelegate(
   8158             texture_ref));
   8159     EXPECT_TRUE(texture->IsImmutable());
   8160     EXPECT_TRUE(texture->SafeToRenderFrom());
   8161   }
   8162 
   8163   // Binding/defining of the async transfer
   8164   {
   8165     // TODO(epenner): We should check that the manager gets the
   8166     // BindCompletedAsyncTransfers() call, which is required to
   8167     // guarantee the delegate calls the bind callback.
   8168 
   8169     // Simulate the bind callback from the delegate.
   8170     bind_callback.Run();
   8171 
   8172     // After the bind callback is run, the texture is safe,
   8173     // and has the right size etc.
   8174     EXPECT_TRUE(texture->SafeToRenderFrom());
   8175     GLsizei width, height;
   8176     EXPECT_TRUE(texture->GetLevelSize(GL_TEXTURE_2D, 0, &width, &height));
   8177     EXPECT_EQ(width, 8);
   8178     EXPECT_EQ(height, 8);
   8179   }
   8180 
   8181   // AsyncTexSubImage2D
   8182   decoder_->GetAsyncPixelTransferManager()
   8183       ->ClearPixelTransferDelegateForTest(texture_ref);
   8184   texture->SetImmutable(false);
   8185   {
   8186     // Create transfer state since it doesn't exist.
   8187     EXPECT_CALL(*manager, CreatePixelTransferDelegateImpl(texture_ref, _))
   8188         .WillOnce(Return(
   8189             delegate = new StrictMock<gpu::MockAsyncPixelTransferDelegate>))
   8190         .RetiresOnSaturation();
   8191     EXPECT_CALL(*delegate, AsyncTexSubImage2D(_, _))
   8192         .RetiresOnSaturation();
   8193     // Command succeeds.
   8194     EXPECT_EQ(error::kNoError, ExecuteCmd(texsubimage_cmd));
   8195     EXPECT_EQ(GL_NO_ERROR, GetGLError());
   8196     EXPECT_EQ(
   8197         delegate,
   8198         decoder_->GetAsyncPixelTransferManager()->GetPixelTransferDelegate(
   8199             texture_ref));
   8200     EXPECT_TRUE(texture->IsImmutable());
   8201     EXPECT_TRUE(texture->SafeToRenderFrom());
   8202   }
   8203   {
   8204     // No transfer is in progress.
   8205     EXPECT_CALL(*delegate, TransferIsInProgress())
   8206         .WillOnce(Return(false))  // texSubImage validation
   8207         .WillOnce(Return(false))  // async validation
   8208         .RetiresOnSaturation();
   8209     EXPECT_CALL(*delegate, AsyncTexSubImage2D(_, _))
   8210         .RetiresOnSaturation();
   8211     // Command succeeds.
   8212     EXPECT_EQ(error::kNoError, ExecuteCmd(texsubimage_cmd));
   8213     EXPECT_EQ(GL_NO_ERROR, GetGLError());
   8214     EXPECT_EQ(
   8215         delegate,
   8216         decoder_->GetAsyncPixelTransferManager()->GetPixelTransferDelegate(
   8217             texture_ref));
   8218     EXPECT_TRUE(texture->IsImmutable());
   8219     EXPECT_TRUE(texture->SafeToRenderFrom());
   8220   }
   8221   {
   8222     // A transfer is still in progress!
   8223     EXPECT_CALL(*delegate, TransferIsInProgress())
   8224         .WillOnce(Return(true))
   8225         .RetiresOnSaturation();
   8226     // No async call, command fails.
   8227     EXPECT_EQ(error::kNoError, ExecuteCmd(texsubimage_cmd));
   8228     EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   8229     EXPECT_EQ(
   8230         delegate,
   8231         decoder_->GetAsyncPixelTransferManager()->GetPixelTransferDelegate(
   8232             texture_ref));
   8233     EXPECT_TRUE(texture->IsImmutable());
   8234     EXPECT_TRUE(texture->SafeToRenderFrom());
   8235   }
   8236 
   8237   // Delete delegate on DeleteTexture.
   8238   {
   8239     EXPECT_CALL(*delegate, Destroy()).RetiresOnSaturation();
   8240     DoDeleteTexture(client_texture_id_, kServiceTextureId);
   8241     EXPECT_FALSE(
   8242         decoder_->GetAsyncPixelTransferManager()->GetPixelTransferDelegate(
   8243             texture_ref));
   8244     delegate = NULL;
   8245   }
   8246 
   8247   // WaitAsyncTexImage2D
   8248   {
   8249     // Get a fresh texture since the existing texture cannot be respecified
   8250     // asynchronously and AsyncTexSubImage2D does not involved binding.
   8251     EXPECT_CALL(*gl_, GenTextures(1, _))
   8252         .WillOnce(SetArgumentPointee<1>(kServiceTextureId));
   8253     DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   8254     texture_ref = GetTexture(client_texture_id_);
   8255     texture = texture_ref->texture();
   8256     texture->SetImmutable(false);
   8257     // Create transfer state since it doesn't exist.
   8258     EXPECT_CALL(*manager, CreatePixelTransferDelegateImpl(texture_ref, _))
   8259         .WillOnce(Return(
   8260             delegate = new StrictMock<gpu::MockAsyncPixelTransferDelegate>))
   8261         .RetiresOnSaturation();
   8262     EXPECT_CALL(*delegate, AsyncTexImage2D(_, _, _))
   8263         .RetiresOnSaturation();
   8264     // Start async transfer.
   8265     EXPECT_EQ(error::kNoError, ExecuteCmd(teximage_cmd));
   8266     EXPECT_EQ(GL_NO_ERROR, GetGLError());
   8267     EXPECT_EQ(
   8268         delegate,
   8269         decoder_->GetAsyncPixelTransferManager()->GetPixelTransferDelegate(
   8270             texture_ref));
   8271 
   8272     EXPECT_TRUE(texture->IsImmutable());
   8273     // Wait for completion.
   8274     EXPECT_CALL(*delegate, WaitForTransferCompletion());
   8275     EXPECT_CALL(*manager, BindCompletedAsyncTransfers());
   8276     EXPECT_EQ(error::kNoError, ExecuteCmd(wait_cmd));
   8277     EXPECT_EQ(GL_NO_ERROR, GetGLError());
   8278   }
   8279 }
   8280 
   8281 TEST_F(GLES2DecoderManualInitTest, AsyncPixelTransferManager) {
   8282   InitDecoder(
   8283       "GL_CHROMIUM_async_pixel_transfers",  // extensions
   8284       false, false, false,  // has alpha/depth/stencil
   8285       false, false, false,  // request alpha/depth/stencil
   8286       true);   // bind generates resource
   8287 
   8288   // Set up the texture.
   8289   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   8290   TextureRef* texture_ref = GetTexture(client_texture_id_);
   8291 
   8292   // Set a mock Async delegate.
   8293   StrictMock<gpu::MockAsyncPixelTransferManager>* manager =
   8294       new StrictMock<gpu::MockAsyncPixelTransferManager>;
   8295   manager->Initialize(group().texture_manager());
   8296   decoder_->SetAsyncPixelTransferManagerForTest(manager);
   8297   StrictMock<gpu::MockAsyncPixelTransferDelegate>* delegate = NULL;
   8298 
   8299   AsyncTexImage2DCHROMIUM teximage_cmd;
   8300   teximage_cmd.Init(GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 0, GL_RGBA,
   8301                     GL_UNSIGNED_BYTE, kSharedMemoryId, kSharedMemoryOffset);
   8302 
   8303   // No transfer delegate exists initially.
   8304   EXPECT_FALSE(
   8305       decoder_->GetAsyncPixelTransferManager()->GetPixelTransferDelegate(
   8306           texture_ref));
   8307 
   8308   // Create delegate on AsyncTexImage2D.
   8309   {
   8310     EXPECT_CALL(*manager, CreatePixelTransferDelegateImpl(texture_ref, _))
   8311         .WillOnce(Return(
   8312              delegate = new StrictMock<gpu::MockAsyncPixelTransferDelegate>))
   8313         .RetiresOnSaturation();
   8314     EXPECT_CALL(*delegate, AsyncTexImage2D(_, _, _)).RetiresOnSaturation();
   8315 
   8316     // Command succeeds.
   8317     EXPECT_EQ(error::kNoError, ExecuteCmd(teximage_cmd));
   8318     EXPECT_EQ(GL_NO_ERROR, GetGLError());
   8319   }
   8320 
   8321   // Delegate is cached.
   8322   EXPECT_EQ(delegate,
   8323             decoder_->GetAsyncPixelTransferManager()->GetPixelTransferDelegate(
   8324                 texture_ref));
   8325 
   8326   // Delete delegate on manager teardown.
   8327   {
   8328     EXPECT_CALL(*delegate, Destroy()).RetiresOnSaturation();
   8329     decoder_->ResetAsyncPixelTransferManagerForTest();
   8330 
   8331     // Texture ref still valid.
   8332     EXPECT_EQ(texture_ref, GetTexture(client_texture_id_));
   8333   }
   8334 }
   8335 
   8336 namespace {
   8337 
   8338 class SizeOnlyMemoryTracker : public MemoryTracker {
   8339  public:
   8340   SizeOnlyMemoryTracker() {
   8341     // These are the default textures. 1 for TEXTURE_2D and 6 faces for
   8342     // TEXTURE_CUBE_MAP.
   8343     const size_t kInitialUnmanagedPoolSize = 7 * 4;
   8344     const size_t kInitialManagedPoolSize = 0;
   8345     pool_infos_[MemoryTracker::kUnmanaged].initial_size =
   8346         kInitialUnmanagedPoolSize;
   8347     pool_infos_[MemoryTracker::kManaged].initial_size =
   8348         kInitialManagedPoolSize;
   8349   }
   8350 
   8351   // Ensure a certain amount of GPU memory is free. Returns true on success.
   8352   MOCK_METHOD1(EnsureGPUMemoryAvailable, bool(size_t size_needed));
   8353 
   8354   virtual void TrackMemoryAllocatedChange(
   8355       size_t old_size, size_t new_size, Pool pool) {
   8356     PoolInfo& info = pool_infos_[pool];
   8357     info.size += new_size - old_size;
   8358   }
   8359 
   8360   size_t GetPoolSize(Pool pool) {
   8361     const PoolInfo& info = pool_infos_[pool];
   8362     return info.size - info.initial_size;
   8363   }
   8364 
   8365  private:
   8366   virtual ~SizeOnlyMemoryTracker() {
   8367   }
   8368   struct PoolInfo {
   8369     PoolInfo()
   8370         : initial_size(0),
   8371           size(0) {
   8372     }
   8373     size_t initial_size;
   8374     size_t size;
   8375   };
   8376   std::map<Pool, PoolInfo> pool_infos_;
   8377 };
   8378 
   8379 }  // anonymous namespace.
   8380 
   8381 TEST_F(GLES2DecoderManualInitTest, MemoryTrackerInitialSize) {
   8382   scoped_refptr<SizeOnlyMemoryTracker> memory_tracker =
   8383       new SizeOnlyMemoryTracker();
   8384   set_memory_tracker(memory_tracker.get());
   8385   InitDecoder(
   8386       "",      // extensions
   8387       false,   // has alpha
   8388       false,   // has depth
   8389       false,   // has stencil
   8390       false,   // request alpha
   8391       false,   // request depth
   8392       false,   // request stencil
   8393       true);   // bind generates resource
   8394   // Expect that initial size - size is 0.
   8395   EXPECT_EQ(0u, memory_tracker->GetPoolSize(MemoryTracker::kUnmanaged));
   8396   EXPECT_EQ(0u, memory_tracker->GetPoolSize(MemoryTracker::kManaged));
   8397 }
   8398 
   8399 TEST_F(GLES2DecoderManualInitTest, MemoryTrackerTexImage2D) {
   8400   scoped_refptr<SizeOnlyMemoryTracker> memory_tracker =
   8401       new SizeOnlyMemoryTracker();
   8402   set_memory_tracker(memory_tracker.get());
   8403   InitDecoder(
   8404       "",      // extensions
   8405       false,   // has alpha
   8406       false,   // has depth
   8407       false,   // has stencil
   8408       false,   // request alpha
   8409       false,   // request depth
   8410       false,   // request stencil
   8411       true);   // bind generates resource
   8412   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   8413   EXPECT_CALL(*memory_tracker.get(), EnsureGPUMemoryAvailable(128))
   8414       .WillOnce(Return(true)).RetiresOnSaturation();
   8415   DoTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 8, 4, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   8416                kSharedMemoryId, kSharedMemoryOffset);
   8417   EXPECT_EQ(128u, memory_tracker->GetPoolSize(MemoryTracker::kUnmanaged));
   8418   EXPECT_CALL(*memory_tracker.get(), EnsureGPUMemoryAvailable(64))
   8419       .WillOnce(Return(true)).RetiresOnSaturation();
   8420   DoTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 4, 4, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   8421                kSharedMemoryId, kSharedMemoryOffset);
   8422   EXPECT_EQ(64u, memory_tracker->GetPoolSize(MemoryTracker::kUnmanaged));
   8423   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   8424   // Check we get out of memory and no call to glTexImage2D if Ensure fails.
   8425   EXPECT_CALL(*memory_tracker.get(), EnsureGPUMemoryAvailable(64))
   8426       .WillOnce(Return(false)).RetiresOnSaturation();
   8427   TexImage2D cmd;
   8428   cmd.Init(GL_TEXTURE_2D, 0, GL_RGBA, 4, 4, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   8429            kSharedMemoryId, kSharedMemoryOffset);
   8430   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   8431   EXPECT_EQ(GL_OUT_OF_MEMORY, GetGLError());
   8432   EXPECT_EQ(64u, memory_tracker->GetPoolSize(MemoryTracker::kUnmanaged));
   8433 }
   8434 
   8435 TEST_F(GLES2DecoderManualInitTest, MemoryTrackerTexStorage2DEXT) {
   8436   scoped_refptr<SizeOnlyMemoryTracker> memory_tracker =
   8437       new SizeOnlyMemoryTracker();
   8438   set_memory_tracker(memory_tracker.get());
   8439   InitDecoder(
   8440       "",      // extensions
   8441       false,   // has alpha
   8442       false,   // has depth
   8443       false,   // has stencil
   8444       false,   // request alpha
   8445       false,   // request depth
   8446       false,   // request stencil
   8447       true);   // bind generates resource
   8448   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   8449   // Check we get out of memory and no call to glTexStorage2DEXT
   8450   // if Ensure fails.
   8451   EXPECT_CALL(*memory_tracker.get(), EnsureGPUMemoryAvailable(128))
   8452       .WillOnce(Return(false)).RetiresOnSaturation();
   8453   TexStorage2DEXT cmd;
   8454   cmd.Init(GL_TEXTURE_2D, 1, GL_RGBA8, 8, 4);
   8455   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   8456   EXPECT_EQ(0u, memory_tracker->GetPoolSize(MemoryTracker::kUnmanaged));
   8457   EXPECT_EQ(GL_OUT_OF_MEMORY, GetGLError());
   8458 }
   8459 
   8460 TEST_F(GLES2DecoderManualInitTest, MemoryTrackerCopyTexImage2D) {
   8461   GLenum target = GL_TEXTURE_2D;
   8462   GLint level = 0;
   8463   GLenum internal_format = GL_RGBA;
   8464   GLsizei width = 4;
   8465   GLsizei height = 8;
   8466   GLint border = 0;
   8467   scoped_refptr<SizeOnlyMemoryTracker> memory_tracker =
   8468       new SizeOnlyMemoryTracker();
   8469   set_memory_tracker(memory_tracker.get());
   8470   InitDecoder(
   8471       "",      // extensions
   8472       true,    // has alpha
   8473       false,   // has depth
   8474       false,   // has stencil
   8475       true,    // request alpha
   8476       false,   // request depth
   8477       false,   // request stencil
   8478       true);   // bind generates resource
   8479   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   8480   EXPECT_CALL(*memory_tracker.get(), EnsureGPUMemoryAvailable(128))
   8481       .WillOnce(Return(true)).RetiresOnSaturation();
   8482   EXPECT_CALL(*gl_, GetError())
   8483       .WillOnce(Return(GL_NO_ERROR))
   8484       .WillOnce(Return(GL_NO_ERROR))
   8485       .RetiresOnSaturation();
   8486   EXPECT_CALL(*gl_, CopyTexImage2D(
   8487       target, level, internal_format, 0, 0, width, height, border))
   8488       .Times(1)
   8489       .RetiresOnSaturation();
   8490   CopyTexImage2D cmd;
   8491   cmd.Init(target, level, internal_format, 0, 0, width, height, border);
   8492   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   8493   EXPECT_EQ(128u, memory_tracker->GetPoolSize(MemoryTracker::kUnmanaged));
   8494   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   8495   // Check we get out of memory and no call to glCopyTexImage2D if Ensure fails.
   8496   EXPECT_CALL(*memory_tracker.get(), EnsureGPUMemoryAvailable(128))
   8497       .WillOnce(Return(false)).RetiresOnSaturation();
   8498   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   8499   EXPECT_EQ(GL_OUT_OF_MEMORY, GetGLError());
   8500   EXPECT_EQ(128u, memory_tracker->GetPoolSize(MemoryTracker::kUnmanaged));
   8501 }
   8502 
   8503 TEST_F(GLES2DecoderManualInitTest, MemoryTrackerRenderbufferStorage) {
   8504   scoped_refptr<SizeOnlyMemoryTracker> memory_tracker =
   8505       new SizeOnlyMemoryTracker();
   8506   set_memory_tracker(memory_tracker.get());
   8507   InitDecoder(
   8508       "",      // extensions
   8509       false,   // has alpha
   8510       false,   // has depth
   8511       false,   // has stencil
   8512       false,   // request alpha
   8513       false,   // request depth
   8514       false,   // request stencil
   8515       true);   // bind generates resource
   8516   DoBindRenderbuffer(GL_RENDERBUFFER, client_renderbuffer_id_,
   8517                     kServiceRenderbufferId);
   8518   EXPECT_CALL(*gl_, GetError())
   8519       .WillOnce(Return(GL_NO_ERROR))
   8520       .WillOnce(Return(GL_NO_ERROR))
   8521       .RetiresOnSaturation();
   8522   EXPECT_CALL(*memory_tracker.get(), EnsureGPUMemoryAvailable(128))
   8523       .WillOnce(Return(true)).RetiresOnSaturation();
   8524   EXPECT_CALL(*gl_, RenderbufferStorageEXT(
   8525       GL_RENDERBUFFER, GL_RGBA, 8, 4))
   8526       .Times(1)
   8527       .RetiresOnSaturation();
   8528   RenderbufferStorage cmd;
   8529   cmd.Init(GL_RENDERBUFFER, GL_RGBA4, 8, 4);
   8530   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   8531   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   8532   EXPECT_EQ(128u, memory_tracker->GetPoolSize(MemoryTracker::kUnmanaged));
   8533   // Check we get out of memory and no call to glRenderbufferStorage if Ensure
   8534   // fails.
   8535   EXPECT_CALL(*memory_tracker.get(), EnsureGPUMemoryAvailable(128))
   8536       .WillOnce(Return(false)).RetiresOnSaturation();
   8537   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   8538   EXPECT_EQ(GL_OUT_OF_MEMORY, GetGLError());
   8539   EXPECT_EQ(128u, memory_tracker->GetPoolSize(MemoryTracker::kUnmanaged));
   8540 }
   8541 
   8542 TEST_F(GLES2DecoderManualInitTest, MemoryTrackerBufferData) {
   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   DoBindBuffer(GL_ARRAY_BUFFER, client_buffer_id_,
   8556                kServiceBufferId);
   8557   EXPECT_CALL(*gl_, GetError())
   8558       .WillOnce(Return(GL_NO_ERROR))
   8559       .WillOnce(Return(GL_NO_ERROR))
   8560       .RetiresOnSaturation();
   8561   EXPECT_CALL(*memory_tracker.get(), EnsureGPUMemoryAvailable(128))
   8562       .WillOnce(Return(true)).RetiresOnSaturation();
   8563   EXPECT_CALL(*gl_, BufferData(GL_ARRAY_BUFFER, 128, _, GL_STREAM_DRAW))
   8564       .Times(1)
   8565       .RetiresOnSaturation();
   8566   BufferData cmd;
   8567   cmd.Init(GL_ARRAY_BUFFER, 128, 0, 0, GL_STREAM_DRAW);
   8568   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   8569   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   8570   EXPECT_EQ(128u, memory_tracker->GetPoolSize(MemoryTracker::kManaged));
   8571   // Check we get out of memory and no call to glBufferData if Ensure
   8572   // fails.
   8573   EXPECT_CALL(*memory_tracker.get(), EnsureGPUMemoryAvailable(128))
   8574       .WillOnce(Return(false)).RetiresOnSaturation();
   8575   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   8576   EXPECT_EQ(GL_OUT_OF_MEMORY, GetGLError());
   8577   EXPECT_EQ(128u, memory_tracker->GetPoolSize(MemoryTracker::kManaged));
   8578 }
   8579 
   8580 TEST_F(GLES2DecoderTest, DrawBuffersEXTImmediateSuccceeds) {
   8581   const GLsizei count = 1;
   8582   const GLenum bufs[] = { GL_COLOR_ATTACHMENT0 };
   8583   DrawBuffersEXTImmediate& cmd =
   8584       *GetImmediateAs<DrawBuffersEXTImmediate>();
   8585   cmd.Init(count, bufs);
   8586 
   8587   DoBindFramebuffer(GL_FRAMEBUFFER, client_framebuffer_id_,
   8588                     kServiceFramebufferId);
   8589   EXPECT_CALL(*gl_, DrawBuffersARB(count, _))
   8590       .Times(1)
   8591       .RetiresOnSaturation();
   8592   EXPECT_EQ(error::kNoError,
   8593             ExecuteImmediateCmd(cmd, sizeof(bufs)));
   8594   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   8595 }
   8596 
   8597 TEST_F(GLES2DecoderTest, DrawBuffersEXTImmediateFails) {
   8598   const GLsizei count = 1;
   8599   const GLenum bufs[] = { GL_COLOR_ATTACHMENT1_EXT };
   8600   DrawBuffersEXTImmediate& cmd =
   8601       *GetImmediateAs<DrawBuffersEXTImmediate>();
   8602   cmd.Init(count, bufs);
   8603 
   8604   DoBindFramebuffer(GL_FRAMEBUFFER, client_framebuffer_id_,
   8605                     kServiceFramebufferId);
   8606   EXPECT_EQ(error::kNoError,
   8607             ExecuteImmediateCmd(cmd, sizeof(bufs)));
   8608   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   8609 }
   8610 
   8611 TEST_F(GLES2DecoderTest, DrawBuffersEXTImmediateBackbuffer) {
   8612   const GLsizei count = 1;
   8613   const GLenum bufs[] = { GL_BACK };
   8614   DrawBuffersEXTImmediate& cmd =
   8615       *GetImmediateAs<DrawBuffersEXTImmediate>();
   8616   cmd.Init(count, bufs);
   8617 
   8618   DoBindFramebuffer(GL_FRAMEBUFFER, client_framebuffer_id_,
   8619                     kServiceFramebufferId);
   8620   EXPECT_EQ(error::kNoError,
   8621             ExecuteImmediateCmd(cmd, sizeof(bufs)));
   8622   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   8623 
   8624   DoBindFramebuffer(GL_FRAMEBUFFER, 0, 0);  // unbind
   8625 
   8626   EXPECT_CALL(*gl_, DrawBuffersARB(count, _))
   8627       .Times(1)
   8628       .RetiresOnSaturation();
   8629 
   8630   EXPECT_EQ(error::kNoError,
   8631             ExecuteImmediateCmd(cmd, sizeof(bufs)));
   8632   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   8633 }
   8634 
   8635 // TODO(gman): Complete this test.
   8636 // TEST_F(GLES2DecoderTest, CompressedTexImage2DGLError) {
   8637 // }
   8638 
   8639 // TODO(gman): BufferData
   8640 
   8641 // TODO(gman): BufferDataImmediate
   8642 
   8643 // TODO(gman): BufferSubData
   8644 
   8645 // TODO(gman): BufferSubDataImmediate
   8646 
   8647 // TODO(gman): CompressedTexImage2D
   8648 
   8649 // TODO(gman): CompressedTexImage2DImmediate
   8650 
   8651 // TODO(gman): CompressedTexSubImage2DImmediate
   8652 
   8653 // TODO(gman): DeleteProgram
   8654 
   8655 // TODO(gman): DeleteShader
   8656 
   8657 // TODO(gman): PixelStorei
   8658 
   8659 // TODO(gman): TexImage2D
   8660 
   8661 // TODO(gman): TexImage2DImmediate
   8662 
   8663 // TODO(gman): TexSubImage2DImmediate
   8664 
   8665 // TODO(gman): UseProgram
   8666 
   8667 // TODO(gman): SwapBuffers
   8668 
   8669 }  // namespace gles2
   8670 }  // namespace gpu
   8671