Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(753)

Side by Side Diff: remoting/client/gl_canvas.cc

Issue 2045963004: [Remoting] OpenGL Rendering Layer (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Inlines Shaders & Fixes build files Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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).\n"
Sergey Ulanov 2016/07/09 01:01:20 nit: comments can be kept outside of the string. W
17 "varying vec2 v_texCoord;\n"
18 "attribute vec2 a_texCoord;\n"
19
20 "// Positions to draw the texture on the texture coordinates.\n"
21 "attribute vec2 a_position;\n"
22 "uniform mat3 u_transform;\n"
23
24 "// This matrix translates normalized texture coordinates\n"
25 "// ([0, 1] starting at upper-left corner) to the view coordinates\n"
26 "// ([-1, 1] starting at the center of the screen).\n"
27 "// Note that the matrix is defined in column-major order.\n"
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 \n"
34 " // related to the view.\n"
35 " vec3 trans_position = u_transform * vec3(a_position, 1.0);\n"
36 " // Transforms texture coordinates to view coordinates and adds\n"
37 " // projection component 1.\n"
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).\n"
45 "varying vec2 v_texCoord;\n"
46 "uniform sampler2D u_texture;\n"
47 "void main() {\n"
48 " // There is some issue with the RGBA decoder (see JniVideoRenderer) so\n"
49 " // we prefer BGRA. However, OpenGL ES doesn't seem to support GL_BGRA\n"
50 " // when uploading the texture, so we solve this by specifying GL_RGBA\n"
51 " // when uploading the pixel and swap b and r in the frag shader.\n"
Sergey Ulanov 2016/07/09 01:01:20 The decoder can generate frames in both formats, s
Yuwei 2016/07/11 22:38:23 Done.
52 " gl_FragColor = texture2D(u_texture, v_texCoord).bgra;\n"
53 "}";
54
55
56 } // namespace
57
58 namespace remoting {
59
60 GlCanvas::GlCanvas(int gl_version) : gl_version_(gl_version) {
61 vertex_shader_ = CompileShader(GL_VERTEX_SHADER, kTexCoordToViewVert);
62 fragment_shader_ = CompileShader(GL_FRAGMENT_SHADER, kDrawTexFrag);
63 program_ = CreateProgram(vertex_shader_, fragment_shader_);
64 glUseProgram(program_);
65
66 transform_location_ = glGetUniformLocation(program_, "u_transform");
67 texture_location_ = glGetUniformLocation(program_, "u_texture");
68 position_location_ = glGetAttribLocation(program_, "a_position");
69 tex_cord_location_ = glGetAttribLocation(program_, "a_texCoord");
70 glEnableVertexAttribArray(position_location_);
71 glEnableVertexAttribArray(tex_cord_location_);
72 glEnable(GL_BLEND);
73 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
74 }
75
76 GlCanvas::~GlCanvas() {
77 DCHECK(thread_checker_.CalledOnValidThread());
78 glDisable(GL_BLEND);
79 glDisableVertexAttribArray(tex_cord_location_);
80 glDisableVertexAttribArray(position_location_);
81 glDeleteProgram(program_);
82 glDeleteShader(vertex_shader_);
83 glDeleteShader(fragment_shader_);
84 }
85
86 void GlCanvas::SetNormalizedTransformation(const float* matrix) {
87 DCHECK(thread_checker_.CalledOnValidThread());
88 glUniformMatrix3fv(transform_location_, 1, GL_TRUE, matrix);
89 transformation_set_ = true;
90 }
91
92 void GlCanvas::DrawTexture(int texture_id,
93 GLuint texture_handle,
94 GLuint vertex_buffer) {
95 DCHECK(thread_checker_.CalledOnValidThread());
96 if (!transformation_set_) {
97 return;
98 }
99 glActiveTexture(GL_TEXTURE0 + texture_id);
100 glBindTexture(GL_TEXTURE_2D, texture_handle);
101 glUniform1i(texture_location_, texture_id);
102 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
103
104 glVertexAttribPointer(position_location_, kVertexSize, GL_FLOAT, GL_FALSE, 0,
105 0);
106 glVertexAttribPointer(tex_cord_location_, kVertexSize, GL_FLOAT, GL_FALSE, 0,
107 static_cast<float*>(0) + kVertexSize * kVertexCount);
108
109 glDrawArrays(GL_TRIANGLE_STRIP, 0, kVertexCount);
110 glBindBuffer(GL_ARRAY_BUFFER, 0);
111 glBindTexture(GL_TEXTURE_2D, 0);
112 }
113
114 int GlCanvas::GetGlVersion() const {
115 return gl_version_;
116 }
117
118 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698