Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2010 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 "app/gfx/gl/gl_context.h" | |
| 6 #include "media/tools/shader_bench/gl_painter.h" | |
| 7 | |
| 8 // Matrix used for the YUV to RGB conversion. | |
| 9 static const float kYUV2RGB[9] = { | |
| 10 1.f, 0.f, 1.403f, | |
| 11 1.f, -.344f, -.714f, | |
| 12 1.f, 1.772f, 0.f, | |
| 13 }; | |
| 14 | |
| 15 static const float kYUV2RGB_TRANS[9] = { | |
| 16 1.f, 1.f, 1.f, | |
| 17 0.f, -.344f, 1.772f, | |
| 18 1.403f, -.714f, 0.f, | |
| 19 }; | |
| 20 | |
| 21 // Pass-through vertex shader. | |
| 22 static const char kVertexShader[] = | |
| 23 "precision highp float;\n" | |
| 24 "precision highp int;\n" | |
| 25 "varying vec2 interp_tc;\n" | |
| 26 "\n" | |
| 27 "attribute vec4 in_pos;\n" | |
| 28 "attribute vec2 in_tc;\n" | |
| 29 "\n" | |
| 30 "void main() {\n" | |
| 31 " interp_tc = in_tc;\n" | |
| 32 " gl_Position = in_pos;\n" | |
| 33 "}\n"; | |
| 34 | |
| 35 // YUV to RGB pixel shader. Loads a pixel from each plane and pass through the | |
| 36 // matrix. | |
| 37 static const char kFragmentShader[] = | |
| 38 "precision mediump float;\n" | |
| 39 "precision mediump int;\n" | |
| 40 "varying vec2 interp_tc;\n" | |
| 41 "\n" | |
| 42 "uniform sampler2D y_tex;\n" | |
| 43 "uniform sampler2D u_tex;\n" | |
| 44 "uniform sampler2D v_tex;\n" | |
| 45 "uniform mat3 yuv2rgb;\n" | |
| 46 "\n" | |
| 47 "void main() {\n" | |
| 48 " float y = texture2D(y_tex, interp_tc).x;\n" | |
| 49 " float u = texture2D(u_tex, interp_tc).r - .5;\n" | |
| 50 " float v = texture2D(v_tex, interp_tc).r - .5;\n" | |
| 51 " vec3 rgb = yuv2rgb * vec3(y, u, v);\n" | |
| 52 " gl_FragColor = vec4(rgb, 1);\n" | |
| 53 "}\n"; | |
| 54 | |
| 55 GLPainter::GLPainter() | |
| 56 : program_id_(-1) { | |
| 57 } | |
| 58 | |
| 59 GLPainter::~GLPainter() { | |
| 60 if (program_id_) { | |
| 61 glDeleteProgram(program_id_); | |
| 62 glDeleteTextures(media::VideoFrame::kNumYUVPlanes, textures_); | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 void GLPainter::Initialize(int width, int height) { | |
| 67 //TODO(vrk): assert context_; | |
| 68 // Create 3 textures, one for each plane, and bind them to different | |
| 69 // texture units. | |
| 70 glGenTextures(media::VideoFrame::kNumYUVPlanes, textures_); | |
| 71 | |
| 72 for (unsigned int i = 0; i < media::VideoFrame::kNumYUVPlanes; ++i) { | |
| 73 unsigned int texture_width = (i == media::VideoFrame::kYPlane) ? | |
| 74 width : width / 2; | |
| 75 unsigned int texture_height = (i == media::VideoFrame::kYPlane) ? | |
| 76 height : height / 2; | |
| 77 glActiveTexture(GL_TEXTURE0 + i); | |
| 78 glBindTexture(GL_TEXTURE_2D, textures_[i]); | |
| 79 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | |
| 80 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | |
| 81 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
| 82 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
| 83 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, texture_width, texture_height, 0, | |
|
Alpha Left Google
2010/11/15 20:43:51
nit: 80 chars.
| |
| 84 GL_LUMINANCE, GL_UNSIGNED_BYTE, 0); | |
| 85 } | |
| 86 | |
| 87 GLuint program = CreateShaderProgram(context_, kVertexShader, kFragmentShader) ; | |
| 88 | |
| 89 // Bind parameters. | |
| 90 glUniform1i(glGetUniformLocation(program, "y_tex"), 0); | |
| 91 glUniform1i(glGetUniformLocation(program, "u_tex"), 1); | |
| 92 glUniform1i(glGetUniformLocation(program, "v_tex"), 2); | |
| 93 int yuv2rgb_location = glGetUniformLocation(program, "yuv2rgb"); | |
| 94 | |
| 95 // DesktopGL supports transpose matrices. | |
| 96 if (gfx::GetGLImplementation() == gfx::kGLImplementationDesktopGL) | |
| 97 glUniformMatrix3fv(yuv2rgb_location, 1, GL_TRUE, kYUV2RGB); | |
| 98 else | |
| 99 glUniformMatrix3fv(yuv2rgb_location, 1, GL_FALSE, kYUV2RGB_TRANS); | |
| 100 | |
| 101 program_id_ = program; | |
| 102 } | |
| 103 | |
| 104 void GLPainter::Paint(scoped_refptr<media::VideoFrame> video_frame) { | |
| 105 //TODO(vrk): assert context_; | |
| 106 for (unsigned int i = 0; i < media::VideoFrame::kNumYUVPlanes; ++i) { | |
| 107 unsigned int width = (i == media::VideoFrame::kYPlane) ? | |
| 108 video_frame->width() : video_frame->width() / 2; | |
| 109 unsigned int height = (i == media::VideoFrame::kYPlane) ? | |
| 110 video_frame->height() : video_frame->height() / 2; | |
| 111 glBindTexture(GL_TEXTURE_2D, textures_[i]); | |
|
fbarchard
2010/11/12 17:49:17
glBindTexture is slow. It will cause the glDrawAr
| |
| 112 glPixelStorei(GL_UNPACK_ROW_LENGTH, video_frame->stride(i)); | |
| 113 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, | |
| 114 GL_LUMINANCE, GL_UNSIGNED_BYTE, video_frame->data(i)); | |
|
Alpha Left Google
2010/11/15 20:43:51
nit: indentation.
| |
| 115 } | |
| 116 | |
| 117 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); | |
| 118 context_->SwapBuffers(); | |
|
fbarchard
2010/11/12 17:49:17
SwapBuffers is slow. Are you sure you want to ben
| |
| 119 } | |
| 120 | |
| 121 void GLPainter::SetGLContext(gfx::GLContext* context) { | |
| 122 context_ = context; | |
| 123 } | |
| OLD | NEW |