Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / gpu / command_buffer / tests / gl_virtual_contexts_unittest.cc
index 1776ff1..17cfa9f 100644 (file)
@@ -21,6 +21,11 @@ class GLVirtualContextsTest : public testing::Test {
   static const int kSize1 = 8;
   static const int kSize2 = 16;
 
+  static const GLfloat kFloatRed[4];
+  static const GLfloat kFloatGreen[4];
+  static const uint8 kExpectedRed[4];
+  static const uint8 kExpectedGreen[4];
+
   virtual void SetUp() {
     GLManager::Options options;
     options.size = gfx::Size(kSize0, kSize0);
@@ -40,15 +45,61 @@ class GLVirtualContextsTest : public testing::Test {
     gl_real_.Destroy();
   }
 
+  GLuint SetupColoredVertexProgram() {
+    static const char* v_shader_str = SHADER(
+        attribute vec4 a_position;
+        attribute vec4 a_color;
+        varying vec4 v_color;
+        void main()
+        {
+           gl_Position = a_position;
+           v_color = a_color;
+        }
+     );
+
+    static const char* f_shader_str = SHADER(
+        precision mediump float;
+        varying vec4 v_color;
+        void main()
+        {
+          gl_FragColor = v_color;
+        }
+    );
+
+    GLuint program = GLTestHelper::LoadProgram(v_shader_str, f_shader_str);
+    glUseProgram(program);
+    return program;
+  }
+
+  void SetUpColoredUnitQuad(const GLfloat* color) {
+    GLuint program1 = SetupColoredVertexProgram();
+    GLuint position_loc1 = glGetAttribLocation(program1, "a_position");
+    GLuint color_loc1 = glGetAttribLocation(program1, "a_color");
+    GLTestHelper::SetupUnitQuad(position_loc1);
+    GLTestHelper::SetupColorsForUnitQuad(color_loc1, color, GL_STATIC_DRAW);
+  }
+
   GLManager gl_real_;
   GLManager gl_real_shared_;
   GLManager gl1_;
   GLManager gl2_;
 };
 
+const GLfloat GLVirtualContextsTest::kFloatRed[4] = {
+    1.0f, 0.0f, 0.0f, 1.0f,
+};
+const GLfloat GLVirtualContextsTest::kFloatGreen[4] = {
+    0.0f, 1.0f, 0.0f, 1.0f,
+};
+const uint8 GLVirtualContextsTest::kExpectedRed[4] = {
+    255, 0, 0, 255,
+};
+const uint8 GLVirtualContextsTest::kExpectedGreen[4] = {
+    0, 255, 0, 255,
+};
+
 namespace {
 
-#if !defined(OS_ANDROID)
 void SetupSimpleShader(const uint8* color) {
   static const char* v_shader_str = SHADER(
       attribute vec4 a_Position;
@@ -91,12 +142,9 @@ void TestDraw(int size) {
   glDrawArrays(GL_TRIANGLES, 0, 6);
 }
 
-#endif  // !defined(OS_ANDROID)
-
 }  // anonymous namespace
 
 // http://crbug.com/281565
-#if !defined(OS_ANDROID)
 TEST_F(GLVirtualContextsTest, Basic) {
   struct TestInfo {
     int size;
@@ -139,7 +187,118 @@ TEST_F(GLVirtualContextsTest, Basic) {
     GLTestHelper::CheckGLError("no errors", __LINE__);
   }
 }
-#endif
+
+// http://crbug.com/363407
+TEST_F(GLVirtualContextsTest, VertexArrayObjectRestore) {
+  GLuint vao1 = 0, vao2 = 0;
+
+  gl1_.MakeCurrent();
+  // Set up red quad in vao1.
+  glGenVertexArraysOES(1, &vao1);
+  glBindVertexArrayOES(vao1);
+  SetUpColoredUnitQuad(kFloatRed);
+  glFinish();
+
+  gl2_.MakeCurrent();
+  // Set up green quad in vao2.
+  glGenVertexArraysOES(1, &vao2);
+  glBindVertexArrayOES(vao2);
+  SetUpColoredUnitQuad(kFloatGreen);
+  glFinish();
+
+  gl1_.MakeCurrent();
+  // Test to ensure that vao1 is still the active VAO for this context.
+  glDrawArrays(GL_TRIANGLES, 0, 6);
+  EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, kSize1, kSize1, 0, kExpectedRed));
+  glFinish();
+  GLTestHelper::CheckGLError("no errors", __LINE__);
+
+  gl2_.MakeCurrent();
+  // Test to ensure that vao2 is still the active VAO for this context.
+  glDrawArrays(GL_TRIANGLES, 0, 6);
+  EXPECT_TRUE(
+      GLTestHelper::CheckPixels(0, 0, kSize2, kSize2, 0, kExpectedGreen));
+  glFinish();
+  GLTestHelper::CheckGLError("no errors", __LINE__);
+}
+
+// http://crbug.com/363407
+TEST_F(GLVirtualContextsTest, VertexArrayObjectRestoreRebind) {
+  GLuint vao1 = 0, vao2 = 0;
+
+  gl1_.MakeCurrent();
+  // Set up red quad in vao1.
+  glGenVertexArraysOES(1, &vao1);
+  glBindVertexArrayOES(vao1);
+  SetUpColoredUnitQuad(kFloatRed);
+  glFinish();
+
+  gl2_.MakeCurrent();
+  // Set up green quad in new vao2.
+  glGenVertexArraysOES(1, &vao2);
+  glBindVertexArrayOES(vao2);
+  SetUpColoredUnitQuad(kFloatGreen);
+  glFinish();
+
+  gl1_.MakeCurrent();
+  // Test to ensure that vao1 hasn't been corrupted after rebinding.
+  // Bind 0 is required so that bind vao1 is not optimized away in the service.
+  glBindVertexArrayOES(0);
+  glBindVertexArrayOES(vao1);
+  glDrawArrays(GL_TRIANGLES, 0, 6);
+  EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, kSize1, kSize1, 0, kExpectedRed));
+  glFinish();
+  GLTestHelper::CheckGLError("no errors", __LINE__);
+
+  gl2_.MakeCurrent();
+  // Test to ensure that vao1 hasn't been corrupted after rebinding.
+  // Bind 0 is required so that bind vao2 is not optimized away in the service.
+  glBindVertexArrayOES(0);
+  glBindVertexArrayOES(vao2);
+  glDrawArrays(GL_TRIANGLES, 0, 6);
+  EXPECT_TRUE(
+      GLTestHelper::CheckPixels(0, 0, kSize2, kSize2, 0, kExpectedGreen));
+  glFinish();
+
+  GLTestHelper::CheckGLError("no errors", __LINE__);
+}
+
+// http://crbug.com/363407
+TEST_F(GLVirtualContextsTest, VertexArrayObjectRestoreDefault) {
+  gl1_.MakeCurrent();
+  // Set up red quad in default VAO.
+  SetUpColoredUnitQuad(kFloatRed);
+  glFinish();
+
+  gl2_.MakeCurrent();
+  // Set up green quad in default VAO.
+  SetUpColoredUnitQuad(kFloatGreen);
+  glFinish();
+
+  // Gen & bind a non-default VAO.
+  GLuint vao;
+  glGenVertexArraysOES(1, &vao);
+  glBindVertexArrayOES(vao);
+  glFinish();
+
+  gl1_.MakeCurrent();
+  // Test to ensure that default VAO on gl1_ is still valid.
+  glDrawArrays(GL_TRIANGLES, 0, 6);
+  EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, kSize1, kSize1, 0, kExpectedRed));
+  glFinish();
+
+  gl2_.MakeCurrent();
+  // Test to ensure that default VAO on gl2_ is still valid.
+  // This tests that a default VAO is restored even when it's not currently
+  // bound during the context switch.
+  glBindVertexArrayOES(0);
+  glDrawArrays(GL_TRIANGLES, 0, 6);
+  EXPECT_TRUE(
+      GLTestHelper::CheckPixels(0, 0, kSize2, kSize2, 0, kExpectedGreen));
+  glFinish();
+
+  GLTestHelper::CheckGLError("no errors", __LINE__);
+}
 
 }  // namespace gpu