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 #include "remoting/client/gl_math.h" | |
10 | |
11 namespace { | |
12 | |
13 const int kVertexSize = 2; | |
14 const int kVertexCount = 4; | |
15 | |
16 const char kTexCoordToViewVert[] = | |
17 // Region of the texture to be used (normally the whole texture). | |
18 "varying vec2 v_texCoord;\n" | |
19 "attribute vec2 a_texCoord;\n" | |
20 | |
21 // Position to draw the texture on the canvas. | |
22 "attribute vec2 a_position;\n" | |
23 | |
24 // Defines the zoom and pan configurations of the canvas. | |
25 "uniform mat3 u_transform;\n" | |
26 | |
27 // Size of the view in pixel. | |
28 "uniform vec2 u_viewSize;\n" | |
29 | |
30 // This matrix translates normalized texture coordinates | |
31 // ([0, 1] starting at upper-left corner) to the view coordinates | |
32 // ([-1, 1] starting at the center of the screen). | |
33 // Note that the matrix is defined in column-major order. | |
34 "const mat3 tex_to_view = mat3(2, 0, 0,\n" | |
35 " 0, -2, 0,\n" | |
36 " -1, 1, 0);\n" | |
37 "void main() {\n" | |
38 " v_texCoord = a_texCoord;\n" | |
39 | |
40 // Transforms coordinates related to the canvas to coordinates | |
41 // related to the view. | |
42 " vec3 trans_position = u_transform * vec3(a_position, 1.0);\n" | |
43 | |
44 // Normalize the position by the size of the view. | |
45 " trans_position.xy /= u_viewSize;\n" | |
46 | |
47 // Transforms texture coordinates to view coordinates and adds | |
48 // projection component 1. | |
49 " gl_Position = vec4(tex_to_view * trans_position, 1.0);\n" | |
50 "}"; | |
51 | |
52 const char kDrawTexFrag[] = | |
53 "precision mediump float;\n" | |
54 | |
55 // Region on the texture to be used (normally the whole texture). | |
56 "varying vec2 v_texCoord;\n" | |
57 "uniform sampler2D u_texture;\n" | |
58 "uniform float u_alpha_multiplier;\n" | |
59 "void main() {\n" | |
60 " gl_FragColor = texture2D(u_texture, v_texCoord);\n" | |
61 " gl_FragColor.a *= u_alpha_multiplier;\n" | |
62 "}"; | |
63 | |
64 } // namespace | |
65 | |
66 namespace remoting { | |
67 | |
68 GlCanvas::GlCanvas(int gl_version) : gl_version_(gl_version) { | |
69 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_texture_size_); | |
70 | |
71 vertex_shader_ = CompileShader(GL_VERTEX_SHADER, kTexCoordToViewVert); | |
72 fragment_shader_ = CompileShader(GL_FRAGMENT_SHADER, kDrawTexFrag); | |
73 program_ = CreateProgram(vertex_shader_, fragment_shader_); | |
74 glUseProgram(program_); | |
75 | |
76 transform_location_ = glGetUniformLocation(program_, "u_transform"); | |
77 view_size_location_ = glGetUniformLocation(program_, "u_viewSize"); | |
78 texture_location_ = glGetUniformLocation(program_, "u_texture"); | |
79 alpha_multiplier_location_ = | |
80 glGetUniformLocation(program_, "u_alpha_multiplier"); | |
81 position_location_ = glGetAttribLocation(program_, "a_position"); | |
82 tex_cord_location_ = glGetAttribLocation(program_, "a_texCoord"); | |
83 glEnableVertexAttribArray(position_location_); | |
84 glEnableVertexAttribArray(tex_cord_location_); | |
85 glEnable(GL_BLEND); | |
86 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | |
87 } | |
88 | |
89 GlCanvas::~GlCanvas() { | |
90 DCHECK(thread_checker_.CalledOnValidThread()); | |
91 glDisable(GL_BLEND); | |
92 glDisableVertexAttribArray(tex_cord_location_); | |
93 glDisableVertexAttribArray(position_location_); | |
94 glDeleteProgram(program_); | |
95 glDeleteShader(vertex_shader_); | |
96 glDeleteShader(fragment_shader_); | |
97 } | |
98 | |
99 void GlCanvas::SetTransformationMatrix(const std::array<float, 9>& matrix) { | |
100 DCHECK(thread_checker_.CalledOnValidThread()); | |
101 std::array<float, 9> transposed_matrix = matrix; | |
102 TransposeTransformationMatrix(&transposed_matrix); | |
103 glUniformMatrix3fv(transform_location_, 1, GL_FALSE, | |
104 transposed_matrix.data()); | |
105 transformation_set_ = true; | |
106 } | |
107 | |
108 void GlCanvas::SetViewSize(int width, int height) { | |
109 DCHECK(width > 0 && height > 0); | |
110 glViewport(0, 0, width, height); | |
111 float view_size[2] {width, height}; | |
112 glUniform2fv(view_size_location_, 1, view_size); | |
113 view_size_set_ = true; | |
114 } | |
115 | |
116 void GlCanvas::DrawTexture(int texture_id, | |
117 GLuint texture_handle, | |
118 GLuint vertex_buffer, | |
119 float alpha_multiplier) { | |
120 DCHECK(thread_checker_.CalledOnValidThread()); | |
121 if (!view_size_set_ || !transformation_set_) { | |
122 return; | |
123 } | |
124 glActiveTexture(GL_TEXTURE0 + texture_id); | |
125 glBindTexture(GL_TEXTURE_2D, texture_handle); | |
126 glUniform1i(texture_location_, texture_id); | |
127 glUniform1f(alpha_multiplier_location_, alpha_multiplier); | |
128 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); | |
129 | |
130 glVertexAttribPointer(position_location_, kVertexSize, GL_FLOAT, GL_FALSE, 0, | |
131 0); | |
132 glVertexAttribPointer(tex_cord_location_, kVertexSize, GL_FLOAT, GL_FALSE, 0, | |
133 static_cast<float*>(0) + kVertexSize * kVertexCount); | |
134 | |
135 glDrawArrays(GL_TRIANGLE_STRIP, 0, kVertexCount); | |
136 glBindBuffer(GL_ARRAY_BUFFER, 0); | |
137 glBindTexture(GL_TEXTURE_2D, 0); | |
138 } | |
139 | |
140 int GlCanvas::GetGlVersion() const { | |
141 return gl_version_; | |
142 } | |
143 | |
144 int GlCanvas::GetMaxTextureSize() const { | |
145 return max_texture_size_; | |
146 } | |
147 | |
148 } // namespace remoting | |
OLD | NEW |