1 // Copyright 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 
5 #include "ui/gl/gl_helper.h"
6 
7 #include <string>
8 
9 #include "base/check_op.h"
10 #include "ui/gl/gl_context.h"
11 #include "ui/gl/gl_version_info.h"
12 #include "ui/gl/scoped_binders.h"
13 
14 namespace gl {
15 
16 // static
CompileShader(GLenum type,const char * src)17 GLuint GLHelper::CompileShader(GLenum type, const char* src) {
18   GLuint shader = glCreateShader(type);
19   // Load the shader source.
20   glShaderSource(shader, 1, &src, nullptr);
21   // Compile the shader.
22   glCompileShader(shader);
23   return shader;
24 }
25 
26 // static
LoadShader(GLenum type,const char * src)27 GLuint GLHelper::LoadShader(GLenum type, const char* src) {
28   GLuint shader = CompileShader(type, src);
29 
30   // Check the compile status.
31   GLint value = 0;
32   glGetShaderiv(shader, GL_COMPILE_STATUS, &value);
33   if (!value) {
34     char buffer[1024];
35     GLsizei length = 0;
36     glGetShaderInfoLog(shader, sizeof(buffer), &length, buffer);
37     std::string log(buffer, length);
38     DCHECK_EQ(1, value) << "Error compiling shader: " << log;
39     glDeleteShader(shader);
40     shader = 0;
41   }
42   return shader;
43 }
44 
45 // static
LinkProgram(GLuint vertex_shader,GLuint fragment_shader)46 GLuint GLHelper::LinkProgram(GLuint vertex_shader, GLuint fragment_shader) {
47   // Create the program object.
48   GLuint program = glCreateProgram();
49   glAttachShader(program, vertex_shader);
50   glAttachShader(program, fragment_shader);
51   // Link the program.
52   glLinkProgram(program);
53   return program;
54 }
55 
56 // static
SetupProgram(GLuint vertex_shader,GLuint fragment_shader)57 GLuint GLHelper::SetupProgram(GLuint vertex_shader, GLuint fragment_shader) {
58   GLuint program = LinkProgram(vertex_shader, fragment_shader);
59   // Check the link status.
60   GLint linked = 0;
61   glGetProgramiv(program, GL_LINK_STATUS, &linked);
62   if (!linked) {
63     char buffer[1024];
64     GLsizei length = 0;
65     glGetProgramInfoLog(program, sizeof(buffer), &length, buffer);
66     std::string log(buffer, length);
67     DCHECK_EQ(1, linked) << "Error linking program: " << log;
68     glDeleteProgram(program);
69     program = 0;
70   }
71   return program;
72 }
73 
74 // static
SetupQuadVertexBuffer()75 GLuint GLHelper::SetupQuadVertexBuffer() {
76   GLuint vertex_buffer = 0;
77   glGenBuffersARB(1, &vertex_buffer);
78   ScopedBufferBinder buffer_binder(GL_ARRAY_BUFFER, vertex_buffer);
79   GLfloat data[] = {-1.f, -1.f, 1.f, -1.f, -1.f, 1.f, 1.f, 1.f};
80   glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
81   return vertex_buffer;
82 }
83 
84 // static
DrawQuad(GLuint vertex_buffer)85 void GLHelper::DrawQuad(GLuint vertex_buffer) {
86   ScopedBufferBinder buffer_binder(GL_ARRAY_BUFFER, vertex_buffer);
87   ScopedVertexAttribArray vertex_attrib_array(0, 2, GL_FLOAT, GL_FALSE,
88                                               sizeof(GLfloat) * 2, 0);
89   ScopedCapability disable_blending(GL_BLEND, GL_FALSE);
90   ScopedCapability disable_culling(GL_CULL_FACE, GL_FALSE);
91   ScopedCapability disable_dithering(GL_DITHER, GL_FALSE);
92   ScopedCapability disable_depth_test(GL_DEPTH_TEST, GL_FALSE);
93   ScopedCapability disable_scissor_test(GL_SCISSOR_TEST, GL_FALSE);
94   ScopedColorMask color_mask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
95 
96   glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
97 }
98 
99 // static
ShouldTestsUseVAOs()100 bool GLHelper::ShouldTestsUseVAOs() {
101   return GLContext::GetCurrent()->GetVersionInfo()->is_desktop_core_profile;
102 }
103 
104 }  // namespace gl
105