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

Unified Diff: media/tools/shader_bench/gl_painter.cc

Issue 4873002: Benchmark tool for GPU-accelerated video rendering (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Cleaned up Window/Painter interface a little Created 10 years, 1 month 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 side-by-side diff with in-line comments
Download patch
Index: media/tools/shader_bench/gl_painter.cc
diff --git a/media/tools/shader_bench/gl_painter.cc b/media/tools/shader_bench/gl_painter.cc
new file mode 100644
index 0000000000000000000000000000000000000000..b31908fa3d221c6a605aefd10c9a0e291b93250a
--- /dev/null
+++ b/media/tools/shader_bench/gl_painter.cc
@@ -0,0 +1,123 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "app/gfx/gl/gl_context.h"
+#include "media/tools/shader_bench/gl_painter.h"
+
+// Matrix used for the YUV to RGB conversion.
+static const float kYUV2RGB[9] = {
+ 1.f, 0.f, 1.403f,
+ 1.f, -.344f, -.714f,
+ 1.f, 1.772f, 0.f,
+};
+
+static const float kYUV2RGB_TRANS[9] = {
+ 1.f, 1.f, 1.f,
+ 0.f, -.344f, 1.772f,
+ 1.403f, -.714f, 0.f,
+};
+
+// Pass-through vertex shader.
+static const char kVertexShader[] =
+ "precision highp float;\n"
+ "precision highp int;\n"
+ "varying vec2 interp_tc;\n"
+ "\n"
+ "attribute vec4 in_pos;\n"
+ "attribute vec2 in_tc;\n"
+ "\n"
+ "void main() {\n"
+ " interp_tc = in_tc;\n"
+ " gl_Position = in_pos;\n"
+ "}\n";
+
+// YUV to RGB pixel shader. Loads a pixel from each plane and pass through the
+// matrix.
+static const char kFragmentShader[] =
+ "precision mediump float;\n"
+ "precision mediump int;\n"
+ "varying vec2 interp_tc;\n"
+ "\n"
+ "uniform sampler2D y_tex;\n"
+ "uniform sampler2D u_tex;\n"
+ "uniform sampler2D v_tex;\n"
+ "uniform mat3 yuv2rgb;\n"
+ "\n"
+ "void main() {\n"
+ " float y = texture2D(y_tex, interp_tc).x;\n"
+ " float u = texture2D(u_tex, interp_tc).r - .5;\n"
+ " float v = texture2D(v_tex, interp_tc).r - .5;\n"
+ " vec3 rgb = yuv2rgb * vec3(y, u, v);\n"
+ " gl_FragColor = vec4(rgb, 1);\n"
+ "}\n";
+
+GLPainter::GLPainter()
+ : program_id_(-1) {
+}
+
+GLPainter::~GLPainter() {
+ if (program_id_) {
+ glDeleteProgram(program_id_);
+ glDeleteTextures(media::VideoFrame::kNumYUVPlanes, textures_);
+ }
+}
+
+void GLPainter::Initialize(int width, int height) {
+ //TODO(vrk): assert context_;
+ // Create 3 textures, one for each plane, and bind them to different
+ // texture units.
+ glGenTextures(media::VideoFrame::kNumYUVPlanes, textures_);
+
+ for (unsigned int i = 0; i < media::VideoFrame::kNumYUVPlanes; ++i) {
+ unsigned int texture_width = (i == media::VideoFrame::kYPlane) ?
+ width : width / 2;
+ unsigned int texture_height = (i == media::VideoFrame::kYPlane) ?
+ height : height / 2;
+ glActiveTexture(GL_TEXTURE0 + i);
+ glBindTexture(GL_TEXTURE_2D, textures_[i]);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, texture_width, texture_height, 0,
Alpha Left Google 2010/11/15 20:43:51 nit: 80 chars.
+ GL_LUMINANCE, GL_UNSIGNED_BYTE, 0);
+ }
+
+ GLuint program = CreateShaderProgram(context_, kVertexShader, kFragmentShader);
+
+ // Bind parameters.
+ glUniform1i(glGetUniformLocation(program, "y_tex"), 0);
+ glUniform1i(glGetUniformLocation(program, "u_tex"), 1);
+ glUniform1i(glGetUniformLocation(program, "v_tex"), 2);
+ int yuv2rgb_location = glGetUniformLocation(program, "yuv2rgb");
+
+ // DesktopGL supports transpose matrices.
+ if (gfx::GetGLImplementation() == gfx::kGLImplementationDesktopGL)
+ glUniformMatrix3fv(yuv2rgb_location, 1, GL_TRUE, kYUV2RGB);
+ else
+ glUniformMatrix3fv(yuv2rgb_location, 1, GL_FALSE, kYUV2RGB_TRANS);
+
+ program_id_ = program;
+}
+
+void GLPainter::Paint(scoped_refptr<media::VideoFrame> video_frame) {
+ //TODO(vrk): assert context_;
+ for (unsigned int i = 0; i < media::VideoFrame::kNumYUVPlanes; ++i) {
+ unsigned int width = (i == media::VideoFrame::kYPlane) ?
+ video_frame->width() : video_frame->width() / 2;
+ unsigned int height = (i == media::VideoFrame::kYPlane) ?
+ video_frame->height() : video_frame->height() / 2;
+ glBindTexture(GL_TEXTURE_2D, textures_[i]);
fbarchard 2010/11/12 17:49:17 glBindTexture is slow. It will cause the glDrawAr
+ glPixelStorei(GL_UNPACK_ROW_LENGTH, video_frame->stride(i));
+ glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height,
+ GL_LUMINANCE, GL_UNSIGNED_BYTE, video_frame->data(i));
Alpha Left Google 2010/11/15 20:43:51 nit: indentation.
+ }
+
+ glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
+ context_->SwapBuffers();
fbarchard 2010/11/12 17:49:17 SwapBuffers is slow. Are you sure you want to ben
+}
+
+void GLPainter::SetGLContext(gfx::GLContext* context) {
+ context_ = context;
+}

Powered by Google App Engine
This is Rietveld 408576698