JSONStringValueSerializer takes a StringPiece instead of std::string&.
[chromium-blink-merge.git] / gpu / command_buffer / service / gles2_cmd_clear_framebuffer.cc
blob4a9aaa7824f64ae174cd6e1bd52309beb431ebcb
1 // Copyright 2014 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.
5 #include "gpu/command_buffer/service/gles2_cmd_clear_framebuffer.h"
7 #include "base/basictypes.h"
8 #include "gpu/command_buffer/service/gl_utils.h"
9 #include "gpu/command_buffer/service/gles2_cmd_decoder.h"
11 namespace {
13 #define SHADER(src) \
14 "#ifdef GL_ES\n" \
15 "precision mediump float;\n" \
16 "#endif\n" #src
18 const char* g_vertex_shader_source = {
19 SHADER(
20 uniform float u_clear_depth;
21 attribute vec4 a_position;
22 void main(void) {
23 gl_Position = vec4(a_position.x, a_position.y, u_clear_depth, 1.0);
28 const char* g_fragment_shader_source = {
29 SHADER(
30 uniform vec4 u_clear_color;
31 void main(void) {
32 gl_FragColor = u_clear_color;
37 void CompileShader(GLuint shader, const char* shader_source) {
38 glShaderSource(shader, 1, &shader_source, 0);
39 glCompileShader(shader);
40 #if DCHECK_IS_ON()
41 GLint compile_status = GL_FALSE;
42 glGetShaderiv(shader, GL_COMPILE_STATUS, &compile_status);
43 if (GL_TRUE != compile_status) {
44 char buffer[1024];
45 GLsizei length = 0;
46 glGetShaderInfoLog(shader, sizeof(buffer), &length, buffer);
47 std::string log(buffer, length);
48 DLOG(ERROR) << "Error compiling shader: " << log;
49 DLOG(ERROR) << "Shader compilation failure.";
51 #endif
54 } // namespace
56 namespace gpu {
58 ClearFramebufferResourceManager::ClearFramebufferResourceManager(
59 const gles2::GLES2Decoder* decoder)
60 : initialized_(false), program_(0u), buffer_id_(0u) {
61 Initialize(decoder);
64 ClearFramebufferResourceManager::~ClearFramebufferResourceManager() {
65 Destroy();
66 DCHECK(!buffer_id_);
69 void ClearFramebufferResourceManager::Initialize(
70 const gles2::GLES2Decoder* decoder) {
71 static_assert(
72 kVertexPositionAttrib == 0u,
73 "kVertexPositionAttrib must be 0");
74 DCHECK(!buffer_id_);
76 glGenBuffersARB(1, &buffer_id_);
77 glBindBuffer(GL_ARRAY_BUFFER, buffer_id_);
78 const GLfloat kQuadVertices[] = {-1.0f, -1.0f,
79 1.0f, -1.0f,
80 1.0f, 1.0f,
81 -1.0f, 1.0f};
82 glBufferData(
83 GL_ARRAY_BUFFER, sizeof(kQuadVertices), kQuadVertices, GL_STATIC_DRAW);
84 decoder->RestoreBufferBindings();
85 initialized_ = true;
88 void ClearFramebufferResourceManager::Destroy() {
89 if (!initialized_)
90 return;
92 glDeleteProgram(program_);
93 glDeleteBuffersARB(1, &buffer_id_);
94 buffer_id_ = 0;
97 void ClearFramebufferResourceManager::ClearFramebuffer(
98 const gles2::GLES2Decoder* decoder,
99 const gfx::Size& framebuffer_size,
100 GLbitfield mask,
101 GLfloat clear_color_red,
102 GLfloat clear_color_green,
103 GLfloat clear_color_blue,
104 GLfloat clear_color_alpha,
105 GLfloat clear_depth_value,
106 GLint clear_stencil_value) {
107 if (!initialized_) {
108 DLOG(ERROR) << "Uninitialized manager.";
109 return;
112 if (!program_) {
113 program_ = glCreateProgram();
114 GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
115 CompileShader(vertex_shader, g_vertex_shader_source);
116 glAttachShader(program_, vertex_shader);
117 GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
118 CompileShader(fragment_shader, g_fragment_shader_source);
119 glAttachShader(program_, fragment_shader);
120 glBindAttribLocation(program_, kVertexPositionAttrib, "a_position");
121 glLinkProgram(program_);
122 #if DCHECK_IS_ON()
123 GLint linked = GL_FALSE;
124 glGetProgramiv(program_, GL_LINK_STATUS, &linked);
125 if (GL_TRUE != linked)
126 DLOG(ERROR) << "Program link failure.";
127 #endif
128 depth_handle_ = glGetUniformLocation(program_, "u_clear_depth");
129 color_handle_ = glGetUniformLocation(program_, "u_clear_color");
130 glDeleteShader(fragment_shader);
131 glDeleteShader(vertex_shader);
133 glUseProgram(program_);
135 #if DCHECK_IS_ON()
136 glValidateProgram(program_);
137 GLint validation_status = GL_FALSE;
138 glGetProgramiv(program_, GL_VALIDATE_STATUS, &validation_status);
139 if (GL_TRUE != validation_status)
140 DLOG(ERROR) << "Invalid shader.";
141 #endif
143 decoder->ClearAllAttributes();
144 glEnableVertexAttribArray(kVertexPositionAttrib);
146 glBindBuffer(GL_ARRAY_BUFFER, buffer_id_);
147 glVertexAttribPointer(kVertexPositionAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
149 glUniform1f(depth_handle_, clear_depth_value);
150 glUniform4f(color_handle_, clear_color_red, clear_color_green,
151 clear_color_blue, clear_color_alpha);
153 if (!(mask & GL_COLOR_BUFFER_BIT)) {
154 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
157 if (mask & GL_DEPTH_BUFFER_BIT) {
158 glEnable(GL_DEPTH_TEST);
159 glDepthFunc(GL_ALWAYS);
160 } else {
161 glDisable(GL_DEPTH_TEST);
162 glDepthMask(GL_FALSE);
165 if (mask & GL_STENCIL_BUFFER_BIT) {
166 glEnable(GL_STENCIL_TEST);
167 glStencilFunc(GL_ALWAYS, clear_stencil_value, 0xFF);
168 glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
169 } else {
170 glDisable(GL_STENCIL_TEST);
171 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
172 glStencilMask(0);
175 glDisable(GL_CULL_FACE);
176 glDisable(GL_BLEND);
177 glDisable(GL_POLYGON_OFFSET_FILL);
179 glViewport(0, 0, framebuffer_size.width(), framebuffer_size.height());
180 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
182 decoder->RestoreAllAttributes();
183 decoder->RestoreProgramBindings();
184 decoder->RestoreBufferBindings();
185 decoder->RestoreGlobalState();
188 } // namespace gpu