OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "remoting/client/gl_canvas.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "remoting/client/gl_helpers.h" |
| 9 |
| 10 namespace { |
| 11 |
| 12 const int kVertexSize = 2; |
| 13 const int kVertexCount = 4; |
| 14 |
| 15 const char kTexCoordToViewVert[] = |
| 16 // Region of the texture to be used (normally the whole texture). |
| 17 "varying vec2 v_texCoord;\n" |
| 18 "attribute vec2 a_texCoord;\n" |
| 19 |
| 20 // Positions to draw the texture on the texture coordinates. |
| 21 "attribute vec2 a_position;\n" |
| 22 "uniform mat3 u_transform;\n" |
| 23 |
| 24 // This matrix translates normalized texture coordinates |
| 25 // ([0, 1] starting at upper-left corner) to the view coordinates |
| 26 // ([-1, 1] starting at the center of the screen). |
| 27 // Note that the matrix is defined in column-major order. |
| 28 "const mat3 tex_to_view = mat3(2, 0, 0,\n" |
| 29 " 0, -2, 0,\n" |
| 30 " -1, 1, 0);\n" |
| 31 "void main() {\n" |
| 32 " v_texCoord = a_texCoord;\n" |
| 33 // Transforms coordinates related to the canvas to coordinates |
| 34 // related to the view. |
| 35 " vec3 trans_position = u_transform * vec3(a_position, 1.0);\n" |
| 36 // Transforms texture coordinates to view coordinates and adds |
| 37 // projection component 1. |
| 38 " gl_Position = vec4(tex_to_view * trans_position, 1.0);\n" |
| 39 "}"; |
| 40 |
| 41 const char kDrawTexFrag[] = |
| 42 "precision mediump float;\n" |
| 43 |
| 44 // Region on the texture to be used (normally the whole texture). |
| 45 "varying vec2 v_texCoord;\n" |
| 46 "uniform sampler2D u_texture;\n" |
| 47 "void main() {\n" |
| 48 " gl_FragColor = texture2D(u_texture, v_texCoord);\n" |
| 49 "}"; |
| 50 |
| 51 } // namespace |
| 52 |
| 53 namespace remoting { |
| 54 |
| 55 GlCanvas::GlCanvas(int gl_version) : gl_version_(gl_version) { |
| 56 vertex_shader_ = CompileShader(GL_VERTEX_SHADER, kTexCoordToViewVert); |
| 57 fragment_shader_ = CompileShader(GL_FRAGMENT_SHADER, kDrawTexFrag); |
| 58 program_ = CreateProgram(vertex_shader_, fragment_shader_); |
| 59 glUseProgram(program_); |
| 60 |
| 61 transform_location_ = glGetUniformLocation(program_, "u_transform"); |
| 62 texture_location_ = glGetUniformLocation(program_, "u_texture"); |
| 63 position_location_ = glGetAttribLocation(program_, "a_position"); |
| 64 tex_cord_location_ = glGetAttribLocation(program_, "a_texCoord"); |
| 65 glEnableVertexAttribArray(position_location_); |
| 66 glEnableVertexAttribArray(tex_cord_location_); |
| 67 glEnable(GL_BLEND); |
| 68 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 69 } |
| 70 |
| 71 GlCanvas::~GlCanvas() { |
| 72 DCHECK(thread_checker_.CalledOnValidThread()); |
| 73 glDisable(GL_BLEND); |
| 74 glDisableVertexAttribArray(tex_cord_location_); |
| 75 glDisableVertexAttribArray(position_location_); |
| 76 glDeleteProgram(program_); |
| 77 glDeleteShader(vertex_shader_); |
| 78 glDeleteShader(fragment_shader_); |
| 79 } |
| 80 |
| 81 void GlCanvas::SetNormalizedTransformation(const std::array<float, 9>& matrix) { |
| 82 DCHECK(thread_checker_.CalledOnValidThread()); |
| 83 glUniformMatrix3fv(transform_location_, 1, GL_TRUE, matrix.data()); |
| 84 transformation_set_ = true; |
| 85 } |
| 86 |
| 87 void GlCanvas::DrawTexture(int texture_id, |
| 88 GLuint texture_handle, |
| 89 GLuint vertex_buffer) { |
| 90 DCHECK(thread_checker_.CalledOnValidThread()); |
| 91 if (!transformation_set_) { |
| 92 return; |
| 93 } |
| 94 glActiveTexture(GL_TEXTURE0 + texture_id); |
| 95 glBindTexture(GL_TEXTURE_2D, texture_handle); |
| 96 glUniform1i(texture_location_, texture_id); |
| 97 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); |
| 98 |
| 99 glVertexAttribPointer(position_location_, kVertexSize, GL_FLOAT, GL_FALSE, 0, |
| 100 0); |
| 101 glVertexAttribPointer(tex_cord_location_, kVertexSize, GL_FLOAT, GL_FALSE, 0, |
| 102 static_cast<float*>(0) + kVertexSize * kVertexCount); |
| 103 |
| 104 glDrawArrays(GL_TRIANGLE_STRIP, 0, kVertexCount); |
| 105 glBindBuffer(GL_ARRAY_BUFFER, 0); |
| 106 glBindTexture(GL_TEXTURE_2D, 0); |
| 107 } |
| 108 |
| 109 int GlCanvas::GetGlVersion() const { |
| 110 return gl_version_; |
| 111 } |
| 112 |
| 113 } // namespace remoting |
OLD | NEW |