gl_ext_srgb_unittest.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright (c) 2015 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 "gpu/command_buffer/tests/gl_manager.h"
  8. #include "gpu/command_buffer/tests/gl_test_utils.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. namespace gpu {
  11. // A collection of tests that exercise the GL_EXT_srgb extension.
  12. class GLEXTSRGBTest : public testing::Test {
  13. protected:
  14. void SetUp() override { gl_.Initialize(GLManager::Options()); }
  15. void TearDown() override { gl_.Destroy(); }
  16. bool IsApplicable() const {
  17. return GLTestHelper::HasExtension("GL_EXT_sRGB");
  18. }
  19. GLManager gl_;
  20. };
  21. // Test to ensure that GL_SRGB_ALPHA as glTex(Sub)Image2D parameter works. This
  22. // is tricky because GL_SRGB_ALPHA is valid in OpenGL ES 2.0 but not valid in
  23. // OpenGL.
  24. TEST_F(GLEXTSRGBTest, TexImageSRGBALPHAFormat) {
  25. if (!IsApplicable())
  26. return;
  27. static const int kWidth = 10;
  28. static const int kHeight = 10;
  29. static const int kSubImageX = kWidth / 2;
  30. static const int kSubImageY = kHeight / 2;
  31. static const int kSubImageWidth = kWidth / 2;
  32. static const int kSubImageHeight = kHeight / 2;
  33. static const uint8_t kImageColor[] = {255, 255, 255, 255};
  34. static const uint8_t kSubImageColor[] = {128, 128, 128, 128};
  35. uint8_t pixels[kWidth * kHeight * 4];
  36. GLuint tex = 0;
  37. glGenTextures(1, &tex);
  38. glBindTexture(GL_TEXTURE_2D, tex);
  39. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  40. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  41. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  42. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  43. memset(pixels, kImageColor[0], sizeof(pixels));
  44. glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB_ALPHA_EXT, kWidth, kHeight, 0,
  45. GL_SRGB_ALPHA_EXT, GL_UNSIGNED_BYTE, pixels);
  46. EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
  47. memset(pixels, kSubImageColor[0], sizeof(pixels));
  48. glTexSubImage2D(GL_TEXTURE_2D, 0, kSubImageX, kSubImageY, kSubImageWidth,
  49. kSubImageHeight, GL_SRGB_ALPHA_EXT, GL_UNSIGNED_BYTE, pixels);
  50. EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
  51. glBindTexture(GL_TEXTURE_2D, 0);
  52. GLuint fbo = 0;
  53. glGenFramebuffers(1, &fbo);
  54. glBindFramebuffer(GL_FRAMEBUFFER, fbo);
  55. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
  56. tex, 0);
  57. EXPECT_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE),
  58. glCheckFramebufferStatus(GL_FRAMEBUFFER));
  59. GLTestHelper::CheckPixels(0, 0, kSubImageX, kHeight, 0, kImageColor, nullptr);
  60. GLTestHelper::CheckPixels(0, 0, kWidth, kSubImageY, 0, kImageColor, nullptr);
  61. GLTestHelper::CheckPixels(kSubImageX, kSubImageY, kSubImageWidth,
  62. kSubImageHeight, 0, kSubImageColor, nullptr);
  63. }
  64. } // namespace gpu