| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/client/gl_canvas.h" | 5 #include "remoting/client/gl_canvas.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "remoting/client/gl_helpers.h" | 8 #include "remoting/client/gl_helpers.h" |
| 9 | 9 |
| 10 namespace { | 10 namespace { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 // projection component 1. | 37 // projection component 1. |
| 38 " gl_Position = vec4(tex_to_view * trans_position, 1.0);\n" | 38 " gl_Position = vec4(tex_to_view * trans_position, 1.0);\n" |
| 39 "}"; | 39 "}"; |
| 40 | 40 |
| 41 const char kDrawTexFrag[] = | 41 const char kDrawTexFrag[] = |
| 42 "precision mediump float;\n" | 42 "precision mediump float;\n" |
| 43 | 43 |
| 44 // Region on the texture to be used (normally the whole texture). | 44 // Region on the texture to be used (normally the whole texture). |
| 45 "varying vec2 v_texCoord;\n" | 45 "varying vec2 v_texCoord;\n" |
| 46 "uniform sampler2D u_texture;\n" | 46 "uniform sampler2D u_texture;\n" |
| 47 "uniform float u_alpha_multiplier;\n" |
| 47 "void main() {\n" | 48 "void main() {\n" |
| 48 " gl_FragColor = texture2D(u_texture, v_texCoord);\n" | 49 " gl_FragColor = texture2D(u_texture, v_texCoord);\n" |
| 50 " gl_FragColor.a *= u_alpha_multiplier;\n" |
| 49 "}"; | 51 "}"; |
| 50 | 52 |
| 51 } // namespace | 53 } // namespace |
| 52 | 54 |
| 53 namespace remoting { | 55 namespace remoting { |
| 54 | 56 |
| 55 GlCanvas::GlCanvas(int gl_version) : gl_version_(gl_version) { | 57 GlCanvas::GlCanvas(int gl_version) : gl_version_(gl_version) { |
| 56 vertex_shader_ = CompileShader(GL_VERTEX_SHADER, kTexCoordToViewVert); | 58 vertex_shader_ = CompileShader(GL_VERTEX_SHADER, kTexCoordToViewVert); |
| 57 fragment_shader_ = CompileShader(GL_FRAGMENT_SHADER, kDrawTexFrag); | 59 fragment_shader_ = CompileShader(GL_FRAGMENT_SHADER, kDrawTexFrag); |
| 58 program_ = CreateProgram(vertex_shader_, fragment_shader_); | 60 program_ = CreateProgram(vertex_shader_, fragment_shader_); |
| 59 glUseProgram(program_); | 61 glUseProgram(program_); |
| 60 | 62 |
| 61 transform_location_ = glGetUniformLocation(program_, "u_transform"); | 63 transform_location_ = glGetUniformLocation(program_, "u_transform"); |
| 62 texture_location_ = glGetUniformLocation(program_, "u_texture"); | 64 texture_location_ = glGetUniformLocation(program_, "u_texture"); |
| 65 alpha_multiplier_location_ = |
| 66 glGetUniformLocation(program_, "u_alpha_multiplier"); |
| 63 position_location_ = glGetAttribLocation(program_, "a_position"); | 67 position_location_ = glGetAttribLocation(program_, "a_position"); |
| 64 tex_cord_location_ = glGetAttribLocation(program_, "a_texCoord"); | 68 tex_cord_location_ = glGetAttribLocation(program_, "a_texCoord"); |
| 65 glEnableVertexAttribArray(position_location_); | 69 glEnableVertexAttribArray(position_location_); |
| 66 glEnableVertexAttribArray(tex_cord_location_); | 70 glEnableVertexAttribArray(tex_cord_location_); |
| 67 glEnable(GL_BLEND); | 71 glEnable(GL_BLEND); |
| 68 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | 72 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 69 } | 73 } |
| 70 | 74 |
| 71 GlCanvas::~GlCanvas() { | 75 GlCanvas::~GlCanvas() { |
| 72 DCHECK(thread_checker_.CalledOnValidThread()); | 76 DCHECK(thread_checker_.CalledOnValidThread()); |
| 73 glDisable(GL_BLEND); | 77 glDisable(GL_BLEND); |
| 74 glDisableVertexAttribArray(tex_cord_location_); | 78 glDisableVertexAttribArray(tex_cord_location_); |
| 75 glDisableVertexAttribArray(position_location_); | 79 glDisableVertexAttribArray(position_location_); |
| 76 glDeleteProgram(program_); | 80 glDeleteProgram(program_); |
| 77 glDeleteShader(vertex_shader_); | 81 glDeleteShader(vertex_shader_); |
| 78 glDeleteShader(fragment_shader_); | 82 glDeleteShader(fragment_shader_); |
| 79 } | 83 } |
| 80 | 84 |
| 81 void GlCanvas::SetNormalizedTransformation(const std::array<float, 9>& matrix) { | 85 void GlCanvas::SetNormalizedTransformation(const std::array<float, 9>& matrix) { |
| 82 DCHECK(thread_checker_.CalledOnValidThread()); | 86 DCHECK(thread_checker_.CalledOnValidThread()); |
| 83 glUniformMatrix3fv(transform_location_, 1, GL_TRUE, matrix.data()); | 87 glUniformMatrix3fv(transform_location_, 1, GL_TRUE, matrix.data()); |
| 84 transformation_set_ = true; | 88 transformation_set_ = true; |
| 85 } | 89 } |
| 86 | 90 |
| 87 void GlCanvas::DrawTexture(int texture_id, | 91 void GlCanvas::DrawTexture(int texture_id, |
| 88 GLuint texture_handle, | 92 GLuint texture_handle, |
| 89 GLuint vertex_buffer) { | 93 GLuint vertex_buffer, |
| 94 float alpha_multiplier) { |
| 90 DCHECK(thread_checker_.CalledOnValidThread()); | 95 DCHECK(thread_checker_.CalledOnValidThread()); |
| 91 if (!transformation_set_) { | 96 if (!transformation_set_) { |
| 92 return; | 97 return; |
| 93 } | 98 } |
| 94 glActiveTexture(GL_TEXTURE0 + texture_id); | 99 glActiveTexture(GL_TEXTURE0 + texture_id); |
| 95 glBindTexture(GL_TEXTURE_2D, texture_handle); | 100 glBindTexture(GL_TEXTURE_2D, texture_handle); |
| 96 glUniform1i(texture_location_, texture_id); | 101 glUniform1i(texture_location_, texture_id); |
| 102 glUniform1f(alpha_multiplier_location_, alpha_multiplier); |
| 97 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); | 103 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); |
| 98 | 104 |
| 99 glVertexAttribPointer(position_location_, kVertexSize, GL_FLOAT, GL_FALSE, 0, | 105 glVertexAttribPointer(position_location_, kVertexSize, GL_FLOAT, GL_FALSE, 0, |
| 100 0); | 106 0); |
| 101 glVertexAttribPointer(tex_cord_location_, kVertexSize, GL_FLOAT, GL_FALSE, 0, | 107 glVertexAttribPointer(tex_cord_location_, kVertexSize, GL_FLOAT, GL_FALSE, 0, |
| 102 static_cast<float*>(0) + kVertexSize * kVertexCount); | 108 static_cast<float*>(0) + kVertexSize * kVertexCount); |
| 103 | 109 |
| 104 glDrawArrays(GL_TRIANGLE_STRIP, 0, kVertexCount); | 110 glDrawArrays(GL_TRIANGLE_STRIP, 0, kVertexCount); |
| 105 glBindBuffer(GL_ARRAY_BUFFER, 0); | 111 glBindBuffer(GL_ARRAY_BUFFER, 0); |
| 106 glBindTexture(GL_TEXTURE_2D, 0); | 112 glBindTexture(GL_TEXTURE_2D, 0); |
| 107 } | 113 } |
| 108 | 114 |
| 109 int GlCanvas::GetGlVersion() const { | 115 int GlCanvas::GetGlVersion() const { |
| 110 return gl_version_; | 116 return gl_version_; |
| 111 } | 117 } |
| 112 | 118 |
| 113 } // namespace remoting | 119 } // namespace remoting |
| OLD | NEW |