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