gl_native_gmb_backbuffer_unittest.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2016 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 <GLES2/gl2extchromium.h>
  7. #include "base/logging.h"
  8. #include "build/build_config.h"
  9. #include "gpu/command_buffer/client/gles2_lib.h"
  10. #include "gpu/command_buffer/service/image_factory.h"
  11. #include "gpu/command_buffer/tests/gl_manager.h"
  12. #include "gpu/command_buffer/tests/gl_test_utils.h"
  13. #include "gpu/command_buffer/tests/texture_image_factory.h"
  14. #include "gpu/config/gpu_test_config.h"
  15. #include "testing/gtest/include/gtest/gtest.h"
  16. #include "ui/gl/gl_context.h"
  17. #include "ui/gl/gl_image.h"
  18. #if BUILDFLAG(IS_MAC)
  19. #include "gpu/ipc/service/gpu_memory_buffer_factory_io_surface.h"
  20. #endif
  21. namespace gpu {
  22. class GLNativeGMBTest : public testing::Test {
  23. protected:
  24. void SetUp() override {
  25. gl_.Initialize(GLManager::Options());
  26. }
  27. void TearDown() override {
  28. gl_.Destroy();
  29. }
  30. // Runs a simple battery of tests.
  31. void RunBackbufferTestWithOptions(const GLManager::Options& options) {
  32. GLManager gl;
  33. gl.Initialize(options);
  34. gl.MakeCurrent();
  35. // Clear the back buffer and check that it has the right values.
  36. glClearColor(0.0f, 0.25f, 0.5f, 0.7f);
  37. glClear(GL_COLOR_BUFFER_BIT);
  38. uint8_t pixel[4];
  39. memset(pixel, 0, 4);
  40. glReadPixels(0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &pixel);
  41. EXPECT_NEAR(0u, pixel[0], 2);
  42. EXPECT_NEAR(64u, pixel[1], 2);
  43. EXPECT_NEAR(127u, pixel[2], 2);
  44. uint8_t alpha = options.backbuffer_alpha ? 178 : 255;
  45. EXPECT_NEAR(alpha, pixel[3], 2);
  46. // Resize, then clear the back buffer and check its contents.
  47. gfx::ColorSpace color_space = gfx::ColorSpace::CreateSRGB();
  48. glResizeCHROMIUM(10, 10, 1, color_space.AsGLColorSpace(), true);
  49. glClearColor(0.5f, 0.6f, 0.7f, 0.8f);
  50. glClear(GL_COLOR_BUFFER_BIT);
  51. memset(pixel, 0, 4);
  52. glReadPixels(0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &pixel);
  53. EXPECT_NEAR(128u, pixel[0], 2);
  54. EXPECT_NEAR(153u, pixel[1], 2);
  55. EXPECT_NEAR(178u, pixel[2], 2);
  56. uint8_t alpha2 = options.backbuffer_alpha ? 204 : 255;
  57. EXPECT_NEAR(alpha2, pixel[3], 2);
  58. // Swap buffers, then clear the back buffer and check its contents.
  59. ::gles2::GetGLContext()->SwapBuffers(0, 1);
  60. glClearColor(0.1f, 0.2f, 0.3f, 0.4f);
  61. glClear(GL_COLOR_BUFFER_BIT);
  62. memset(pixel, 0, 4);
  63. glReadPixels(0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &pixel);
  64. EXPECT_NEAR(25u, pixel[0], 2);
  65. EXPECT_NEAR(51u, pixel[1], 2);
  66. EXPECT_NEAR(76u, pixel[2], 2);
  67. uint8_t alpha3 = options.backbuffer_alpha ? 102 : 255;
  68. EXPECT_NEAR(alpha3, pixel[3], 2);
  69. gl.Destroy();
  70. }
  71. GLManager gl_;
  72. };
  73. TEST_F(GLNativeGMBTest, TestNativeGMBBackbufferWithDifferentConfigurations) {
  74. if (!GLTestHelper::HasExtension("GL_ARB_texture_rectangle")) {
  75. LOG(INFO) << "GL_ARB_texture_rectangle not supported. Skipping test...";
  76. return;
  77. }
  78. // TODO(jonahr): Test fails on Linux/Mac with ANGLE/passthrough
  79. // (crbug.com/1099768)
  80. gpu::GPUTestBotConfig bot_config;
  81. if (bot_config.LoadCurrentConfig(nullptr) &&
  82. bot_config.Matches("linux mac passthrough")) {
  83. return;
  84. }
  85. #if BUILDFLAG(IS_MAC)
  86. GpuMemoryBufferFactoryIOSurface image_factory;
  87. #else
  88. TextureImageFactory image_factory;
  89. image_factory.SetRequiredTextureType(GL_TEXTURE_RECTANGLE_ARB);
  90. #endif
  91. for (int has_alpha = 0; has_alpha <= 1; ++has_alpha) {
  92. for (int msaa = 0; msaa <= 1; ++msaa) {
  93. for (int preserve_backbuffer = 0; preserve_backbuffer <= 1;
  94. ++preserve_backbuffer) {
  95. GLManager::Options options;
  96. options.image_factory = &image_factory;
  97. options.multisampled = msaa == 1;
  98. options.backbuffer_alpha = has_alpha == 1;
  99. options.preserve_backbuffer = preserve_backbuffer;
  100. RunBackbufferTestWithOptions(options);
  101. }
  102. }
  103. }
  104. }
  105. } // namespace gpu