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 #include "remoting/client/gl_math.h" | 9 #include "remoting/client/gl_math.h" |
10 | 10 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
58 "uniform float u_alpha_multiplier;\n" | 58 "uniform float u_alpha_multiplier;\n" |
59 "void main() {\n" | 59 "void main() {\n" |
60 " gl_FragColor = texture2D(u_texture, v_texCoord);\n" | 60 " gl_FragColor = texture2D(u_texture, v_texCoord);\n" |
61 " gl_FragColor.a *= u_alpha_multiplier;\n" | 61 " gl_FragColor.a *= u_alpha_multiplier;\n" |
62 "}"; | 62 "}"; |
63 | 63 |
64 } // namespace | 64 } // namespace |
65 | 65 |
66 namespace remoting { | 66 namespace remoting { |
67 | 67 |
68 GlCanvas::GlCanvas() {} | |
69 | |
68 GlCanvas::GlCanvas(int gl_version) : gl_version_(gl_version) { | 70 GlCanvas::GlCanvas(int gl_version) : gl_version_(gl_version) { |
71 #ifndef NDEBUG | |
72 // Set the background clear color to bright green for debugging purposes. | |
73 glClearColor(0.0f, 1.0f, 0.0f, 1.0f); | |
74 #else | |
75 // Set the background clear color to black. | |
76 glClearColor(0.0f, 0.0f, 0.0f, 1.0f); | |
77 #endif | |
joedow
2016/12/22 00:29:02
Can you reuse the Clear() method after everything
nicholss
2017/01/09 18:50:23
removing the clear function
| |
78 | |
69 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_texture_size_); | 79 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_texture_size_); |
70 | 80 |
71 vertex_shader_ = CompileShader(GL_VERTEX_SHADER, kTexCoordToViewVert); | 81 vertex_shader_ = CompileShader(GL_VERTEX_SHADER, kTexCoordToViewVert); |
72 fragment_shader_ = CompileShader(GL_FRAGMENT_SHADER, kDrawTexFrag); | 82 fragment_shader_ = CompileShader(GL_FRAGMENT_SHADER, kDrawTexFrag); |
73 program_ = CreateProgram(vertex_shader_, fragment_shader_); | 83 program_ = CreateProgram(vertex_shader_, fragment_shader_); |
74 glUseProgram(program_); | 84 glUseProgram(program_); |
75 | 85 |
76 transform_location_ = glGetUniformLocation(program_, "u_transform"); | 86 transform_location_ = glGetUniformLocation(program_, "u_transform"); |
77 view_size_location_ = glGetUniformLocation(program_, "u_viewSize"); | 87 view_size_location_ = glGetUniformLocation(program_, "u_viewSize"); |
78 texture_location_ = glGetUniformLocation(program_, "u_texture"); | 88 texture_location_ = glGetUniformLocation(program_, "u_texture"); |
79 alpha_multiplier_location_ = | 89 alpha_multiplier_location_ = |
80 glGetUniformLocation(program_, "u_alpha_multiplier"); | 90 glGetUniformLocation(program_, "u_alpha_multiplier"); |
81 position_location_ = glGetAttribLocation(program_, "a_position"); | 91 position_location_ = glGetAttribLocation(program_, "a_position"); |
82 tex_cord_location_ = glGetAttribLocation(program_, "a_texCoord"); | 92 tex_cord_location_ = glGetAttribLocation(program_, "a_texCoord"); |
83 glEnableVertexAttribArray(position_location_); | 93 glEnableVertexAttribArray(position_location_); |
84 glEnableVertexAttribArray(tex_cord_location_); | 94 glEnableVertexAttribArray(tex_cord_location_); |
85 glEnable(GL_BLEND); | 95 glEnable(GL_BLEND); |
86 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | 96 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
97 gl_constructed_ = true; | |
87 } | 98 } |
88 | 99 |
89 GlCanvas::~GlCanvas() { | 100 GlCanvas::~GlCanvas() { |
90 DCHECK(thread_checker_.CalledOnValidThread()); | 101 if (gl_constructed_) { |
Yuwei
2016/12/21 23:41:58
If you have a separate GlCanvas interface then I d
joedow
2016/12/22 00:29:02
This feels a bit odd. Is there a way to guarantee
nicholss
2016/12/22 16:21:41
It is odd because this is not dependency injection
joedow
2016/12/22 19:18:09
I don't think DI is required to prevent having tes
Sergey Ulanov
2016/12/22 22:38:45
In this case I don't think we need any of it. It s
nicholss
2017/01/09 18:50:23
I would prefer to not test OpenGL and the renderer
nicholss
2017/01/09 18:50:24
I added an interface and moved to a more dependenc
Yuwei
2017/01/09 20:28:51
The previous tests work based on these preconditio
| |
91 glDisable(GL_BLEND); | 102 DCHECK(thread_checker_.CalledOnValidThread()); |
92 glDisableVertexAttribArray(tex_cord_location_); | 103 glDisable(GL_BLEND); |
93 glDisableVertexAttribArray(position_location_); | 104 glDisableVertexAttribArray(tex_cord_location_); |
94 glDeleteProgram(program_); | 105 glDisableVertexAttribArray(position_location_); |
95 glDeleteShader(vertex_shader_); | 106 glDeleteProgram(program_); |
96 glDeleteShader(fragment_shader_); | 107 glDeleteShader(vertex_shader_); |
108 glDeleteShader(fragment_shader_); | |
109 } | |
97 } | 110 } |
98 | 111 |
99 void GlCanvas::SetTransformationMatrix(const std::array<float, 9>& matrix) { | 112 void GlCanvas::SetTransformationMatrix(const std::array<float, 9>& matrix) { |
100 DCHECK(thread_checker_.CalledOnValidThread()); | 113 DCHECK(thread_checker_.CalledOnValidThread()); |
101 std::array<float, 9> transposed_matrix = matrix; | 114 std::array<float, 9> transposed_matrix = matrix; |
102 TransposeTransformationMatrix(&transposed_matrix); | 115 TransposeTransformationMatrix(&transposed_matrix); |
103 glUniformMatrix3fv(transform_location_, 1, GL_FALSE, | 116 glUniformMatrix3fv(transform_location_, 1, GL_FALSE, |
104 transposed_matrix.data()); | 117 transposed_matrix.data()); |
105 transformation_set_ = true; | 118 transformation_set_ = true; |
106 } | 119 } |
107 | 120 |
108 void GlCanvas::SetViewSize(int width, int height) { | 121 void GlCanvas::SetViewSize(int width, int height) { |
109 DCHECK(width > 0 && height > 0); | 122 DCHECK(width > 0 && height > 0); |
110 glViewport(0, 0, width, height); | 123 glViewport(0, 0, width, height); |
111 float view_size[2] {width, height}; | 124 float view_size[2] {width, height}; |
112 glUniform2fv(view_size_location_, 1, view_size); | 125 glUniform2fv(view_size_location_, 1, view_size); |
113 view_size_set_ = true; | 126 view_size_set_ = true; |
114 } | 127 } |
115 | 128 |
129 void GlCanvas::Clear() { | |
130 glClear(GL_COLOR_BUFFER_BIT); | |
Yuwei
2016/12/21 23:41:58
glClearColor only specifies the color and glClear
nicholss
2017/01/09 18:50:24
Fixed.
| |
131 | |
132 #ifndef NDEBUG | |
133 // Set the background clear color to bright green for debugging purposes. | |
134 glClearColor(0.0f, 1.0f, 0.0f, 1.0f); | |
135 #endif | |
Yuwei
2016/12/21 23:41:58
Why is the NDEBUG case not here?
| |
136 } | |
137 | |
116 void GlCanvas::DrawTexture(int texture_id, | 138 void GlCanvas::DrawTexture(int texture_id, |
117 GLuint texture_handle, | 139 GLuint texture_handle, |
118 GLuint vertex_buffer, | 140 GLuint vertex_buffer, |
119 float alpha_multiplier) { | 141 float alpha_multiplier) { |
120 DCHECK(thread_checker_.CalledOnValidThread()); | 142 DCHECK(thread_checker_.CalledOnValidThread()); |
121 if (!view_size_set_ || !transformation_set_) { | 143 if (!view_size_set_ || !transformation_set_) { |
122 return; | 144 return; |
123 } | 145 } |
124 glActiveTexture(GL_TEXTURE0 + texture_id); | 146 glActiveTexture(GL_TEXTURE0 + texture_id); |
125 glBindTexture(GL_TEXTURE_2D, texture_handle); | 147 glBindTexture(GL_TEXTURE_2D, texture_handle); |
(...skipping 12 matching lines...) Expand all Loading... | |
138 } | 160 } |
139 | 161 |
140 int GlCanvas::GetGlVersion() const { | 162 int GlCanvas::GetGlVersion() const { |
141 return gl_version_; | 163 return gl_version_; |
142 } | 164 } |
143 | 165 |
144 int GlCanvas::GetMaxTextureSize() const { | 166 int GlCanvas::GetMaxTextureSize() const { |
145 return max_texture_size_; | 167 return max_texture_size_; |
146 } | 168 } |
147 | 169 |
170 GlCanvas* GlCanvas::CreateGlCanvas(int gl_version) { | |
171 if (gl_version > 0) { | |
172 return new GlCanvas(gl_version); | |
173 } else { | |
174 return new FakeGlCanvas(); | |
joedow
2016/12/22 00:29:01
I don't think this logic should be included in pro
nicholss
2017/01/09 18:50:23
Removing.
| |
175 } | |
176 } | |
177 | |
148 } // namespace remoting | 178 } // namespace remoting |
OLD | NEW |