gl_stream_draw_unittest.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include <GLES2/gl2.h>
  5. #include <GLES2/gl2ext.h>
  6. #include <stdint.h>
  7. #include "build/build_config.h"
  8. #include "gpu/command_buffer/tests/gl_manager.h"
  9. #include "gpu/command_buffer/tests/gl_test_utils.h"
  10. #include "testing/gmock/include/gmock/gmock.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. #define SHADER(Src) #Src
  13. namespace gpu {
  14. class GLStreamDrawTest : public testing::Test {
  15. protected:
  16. static const int kSize = 4;
  17. void SetUp() override {
  18. GLManager::Options options;
  19. options.size = gfx::Size(kSize, kSize);
  20. gl_.Initialize(options);
  21. }
  22. void TearDown() override { gl_.Destroy(); }
  23. GLManager gl_;
  24. };
  25. namespace {
  26. GLuint SetupProgram() {
  27. static const char* v_shader_str = SHADER(
  28. attribute vec4 a_position;
  29. attribute vec4 a_color;
  30. varying vec4 v_color;
  31. void main()
  32. {
  33. gl_Position = a_position;
  34. v_color = a_color;
  35. }
  36. );
  37. static const char* f_shader_str = SHADER(
  38. precision mediump float;
  39. varying vec4 v_color;
  40. void main()
  41. {
  42. gl_FragColor = v_color;
  43. }
  44. );
  45. GLuint program = GLTestHelper::LoadProgram(v_shader_str, f_shader_str);
  46. glUseProgram(program);
  47. return program;
  48. }
  49. } // anonymous namespace.
  50. TEST_F(GLStreamDrawTest, Basic) {
  51. static GLfloat float_red[4] = { 1.0f, 0.0f, 0.0f, 1.0f, };
  52. static GLfloat float_green[4] = { 0.0f, 1.0f, 0.0f, 1.0f, };
  53. static uint8_t expected_red[4] = {
  54. 255, 0, 0, 255,
  55. };
  56. static uint8_t expected_green[4] = {
  57. 0, 255, 0, 255,
  58. };
  59. GLuint program = SetupProgram();
  60. GLuint position_loc = glGetAttribLocation(program, "a_position");
  61. GLuint color_loc = glGetAttribLocation(program, "a_color");
  62. GLTestHelper::SetupUnitQuad(position_loc);
  63. GLTestHelper::SetupColorsForUnitQuad(color_loc, float_red, GL_STREAM_DRAW);
  64. glDrawArrays(GL_TRIANGLES, 0, 6);
  65. EXPECT_TRUE(
  66. GLTestHelper::CheckPixels(0, 0, kSize, kSize, 0, expected_red, nullptr));
  67. GLTestHelper::SetupColorsForUnitQuad(color_loc, float_green, GL_STATIC_DRAW);
  68. glDrawArrays(GL_TRIANGLES, 0, 6);
  69. EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, kSize, kSize, 0, expected_green,
  70. nullptr));
  71. GLTestHelper::CheckGLError("no errors", __LINE__);
  72. }
  73. // http://crbug.com/281565
  74. #if !BUILDFLAG(IS_ANDROID)
  75. TEST_F(GLStreamDrawTest, DrawElements) {
  76. static GLfloat float_red[4] = { 1.0f, 0.0f, 0.0f, 1.0f, };
  77. static GLfloat float_green[4] = { 0.0f, 1.0f, 0.0f, 1.0f, };
  78. static uint8_t expected_red[4] = {
  79. 255, 0, 0, 255,
  80. };
  81. static uint8_t expected_green[4] = {
  82. 0, 255, 0, 255,
  83. };
  84. GLuint program = SetupProgram();
  85. GLuint position_loc = glGetAttribLocation(program, "a_position");
  86. GLuint color_loc = glGetAttribLocation(program, "a_color");
  87. GLTestHelper::SetupUnitQuad(position_loc);
  88. GLTestHelper::SetupColorsForUnitQuad(color_loc, float_red, GL_STREAM_DRAW);
  89. GLuint index_buffer = 0;
  90. glGenBuffers(1, &index_buffer);
  91. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer);
  92. static GLubyte indices[] = { 0, 1, 2, 3, 4, 5, };
  93. glBufferData(
  94. GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STREAM_DRAW);
  95. glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, nullptr);
  96. EXPECT_TRUE(
  97. GLTestHelper::CheckPixels(0, 0, kSize, kSize, 0, expected_red, nullptr));
  98. GLTestHelper::SetupColorsForUnitQuad(color_loc, float_green, GL_STATIC_DRAW);
  99. glBufferData(
  100. GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
  101. glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, nullptr);
  102. EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, kSize, kSize, 0, expected_green,
  103. nullptr));
  104. GLTestHelper::CheckGLError("no errors", __LINE__);
  105. }
  106. #endif
  107. TEST_F(GLStreamDrawTest, VertexArrayObjects) {
  108. if (!GLTestHelper::HasExtension("GL_OES_vertex_array_object")) {
  109. return;
  110. }
  111. static GLfloat float_red[4] = { 1.0f, 0.0f, 0.0f, 1.0f, };
  112. static GLfloat float_green[4] = { 0.0f, 1.0f, 0.0f, 1.0f, };
  113. static uint8_t expected_red[4] = {
  114. 255, 0, 0, 255,
  115. };
  116. static uint8_t expected_green[4] = {
  117. 0, 255, 0, 255,
  118. };
  119. GLuint program = SetupProgram();
  120. GLuint position_loc = glGetAttribLocation(program, "a_position");
  121. GLuint color_loc = glGetAttribLocation(program, "a_color");
  122. GLuint vaos[2];
  123. glGenVertexArraysOES(2, vaos);
  124. glBindVertexArrayOES(vaos[0]);
  125. GLuint position_buffer = GLTestHelper::SetupUnitQuad(position_loc);
  126. GLTestHelper::SetupColorsForUnitQuad(color_loc, float_red, GL_STREAM_DRAW);
  127. glBindVertexArrayOES(vaos[1]);
  128. glBindBuffer(GL_ARRAY_BUFFER, position_buffer);
  129. glEnableVertexAttribArray(position_loc);
  130. glVertexAttribPointer(position_loc, 2, GL_FLOAT, GL_FALSE, 0, 0);
  131. GLTestHelper::SetupColorsForUnitQuad(color_loc, float_green, GL_STATIC_DRAW);
  132. for (int ii = 0; ii < 2; ++ii) {
  133. glBindVertexArrayOES(vaos[0]);
  134. glDrawArrays(GL_TRIANGLES, 0, 6);
  135. EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, kSize, kSize, 0, expected_red,
  136. nullptr));
  137. glBindVertexArrayOES(vaos[1]);
  138. glDrawArrays(GL_TRIANGLES, 0, 6);
  139. EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, kSize, kSize, 0, expected_green,
  140. nullptr));
  141. }
  142. GLTestHelper::CheckGLError("no errors", __LINE__);
  143. }
  144. } // namespace gpu