gl_test_utils.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. // Helper functions for GL.
  5. #ifndef GPU_COMMAND_BUFFER_TESTS_GL_TEST_UTILS_H_
  6. #define GPU_COMMAND_BUFFER_TESTS_GL_TEST_UTILS_H_
  7. #include <GLES2/gl2.h>
  8. #include <stdint.h>
  9. #include <vector>
  10. #include "base/memory/raw_ptr.h"
  11. #include "build/build_config.h"
  12. #include "gpu/command_buffer/tests/gl_manager.h"
  13. #include "ui/gl/gl_display.h"
  14. #include "ui/gl/gl_implementation.h"
  15. namespace gl {
  16. class GLImageNativePixmap;
  17. }
  18. namespace gpu {
  19. class GLTestHelper {
  20. public:
  21. static const uint8_t kCheckClearValue = 123u;
  22. static gl::GLDisplay* InitializeGL(gl::GLImplementation gl_impl);
  23. static gl::GLDisplay* InitializeGLDefault();
  24. static bool HasExtension(const char* extension);
  25. static bool CheckGLError(const char* msg, int line);
  26. // Compiles a shader.
  27. // Does not check for errors, always returns shader.
  28. static GLuint CompileShader(GLenum type, const char* shaderSrc);
  29. // Compiles a shader and checks for compilation errors.
  30. // Returns shader, 0 on failure.
  31. static GLuint LoadShader(GLenum type, const char* shaderSrc);
  32. // Attaches 2 shaders and links them to a program.
  33. // Does not check for errors, always returns program.
  34. static GLuint LinkProgram(GLuint vertex_shader, GLuint fragment_shader);
  35. // Attaches 2 shaders, links them to a program, and checks for errors.
  36. // Returns program, 0 on failure.
  37. static GLuint SetupProgram(GLuint vertex_shader, GLuint fragment_shader);
  38. // Compiles 2 shaders, attaches and links them to a program
  39. // Returns program, 0 on failure.
  40. static GLuint LoadProgram(
  41. const char* vertex_shader_source,
  42. const char* fragment_shader_source);
  43. // Make a unit quad with position only.
  44. // Returns the created buffer.
  45. static GLuint SetupUnitQuad(GLint position_location);
  46. // Returns a vector of size 2. The first is the array buffer object,
  47. // the second is the element array buffer object.
  48. static std::vector<GLuint> SetupIndexedUnitQuad(GLint position_location);
  49. // Make a 6 vertex colors.
  50. // Returns the created buffer.
  51. static GLuint SetupColorsForUnitQuad(
  52. GLint location, const GLfloat color[4], GLenum usage);
  53. // Checks an area of pixels for a color.
  54. // If mask is nullptr, compare all color channels; otherwise, compare the
  55. // channels whose corresponding mask bit is true.
  56. static bool CheckPixels(GLint x,
  57. GLint y,
  58. GLsizei width,
  59. GLsizei height,
  60. GLint tolerance,
  61. const uint8_t* color,
  62. const uint8_t* mask);
  63. static bool CheckPixels(GLint x,
  64. GLint y,
  65. GLsizei width,
  66. GLsizei height,
  67. GLint tolerance,
  68. const std::vector<uint8_t>& expected,
  69. const uint8_t* mask);
  70. // Uses ReadPixels to save an area of the current FBO/Backbuffer.
  71. static bool SaveBackbufferAsBMP(const char* filename, int width, int height);
  72. static void DrawTextureQuad(const GLenum texture_target,
  73. const char* vertex_src,
  74. const char* fragment_src,
  75. const char* position_name,
  76. const char* sampler_name,
  77. const char* face_name);
  78. };
  79. class GpuCommandBufferTestEGL {
  80. public:
  81. GpuCommandBufferTestEGL();
  82. ~GpuCommandBufferTestEGL();
  83. // Reinitialize GL to an EGL implementation if it is available and not
  84. // the current initialized GL implementation. Return true on success, false
  85. // otherwise.
  86. bool InitializeEGL(int width, int height);
  87. // Restore the default GL implementation.
  88. void RestoreGLDefault();
  89. // Returns whether the current context supports the named EGL extension.
  90. bool HasEGLExtension(const base::StringPiece& extension) {
  91. return gfx::HasExtension(egl_extensions_, extension);
  92. }
  93. // Returns whether the current context supports the named GL extension.
  94. bool HasGLExtension(const base::StringPiece& extension) {
  95. return gfx::HasExtension(gl_extensions_, extension);
  96. }
  97. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  98. // Create GLImageNativePixmap filled in with the given pixels.
  99. scoped_refptr<gl::GLImageNativePixmap> CreateGLImageNativePixmap(
  100. gfx::BufferFormat format,
  101. gfx::Size size,
  102. uint8_t* pixels) const;
  103. // Get some real dmabuf fds for testing by exporting an EGLImage created from
  104. // a GL texture.
  105. gfx::NativePixmapHandle CreateNativePixmapHandle(gfx::BufferFormat format,
  106. gfx::Size size,
  107. uint8_t* pixels);
  108. #endif
  109. protected:
  110. bool gl_reinitialized_;
  111. GLManager gl_;
  112. gl::GLWindowSystemBindingInfo window_system_binding_info_;
  113. gfx::ExtensionSet egl_extensions_;
  114. gfx::ExtensionSet gl_extensions_;
  115. raw_ptr<gl::GLDisplay> gl_display_ = nullptr;
  116. };
  117. } // namespace gpu
  118. #endif // GPU_COMMAND_BUFFER_TESTS_GL_TEST_UTILS_H_