1 // Copyright (c) 2012 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 "media/tools/player_x11/gl_video_renderer.h" 6 7 #include <X11/Xutil.h> 8 9 #include "base/bind.h" 10 #include "base/message_loop/message_loop.h" 11 #include "media/base/buffers.h" 12 #include "media/base/video_frame.h" 13 #include "media/base/yuv_convert.h" 14 #include "ui/gl/gl_implementation.h" 15 16 enum { kNumYUVPlanes = 3 }; 17 18 static GLXContext InitGLContext(Display* display, Window window) { 19 // Some versions of NVIDIA's GL libGL.so include a broken version of 20 // dlopen/dlsym, and so linking it into chrome breaks it. So we dynamically 21 // load it, and use glew to dynamically resolve symbols. 22 // See http://code.google.com/p/chromium/issues/detail?id=16800 23 if (!InitializeGLBindings(gfx::kGLImplementationDesktopGL)) { 24 LOG(ERROR) << "InitializeGLBindings failed"; 25 return NULL; 26 } 27 28 XWindowAttributes attributes; 29 XGetWindowAttributes(display, window, &attributes); 30 XVisualInfo visual_info_template; 31 visual_info_template.visualid = XVisualIDFromVisual(attributes.visual); 32 int visual_info_count = 0; 33 XVisualInfo* visual_info_list = XGetVisualInfo(display, VisualIDMask, 34 &visual_info_template, 35 &visual_info_count); 36 GLXContext context = NULL; 37 for (int i = 0; i < visual_info_count && !context; ++i) { 38 context = glXCreateContext(display, visual_info_list + i, 0, 39 True /* Direct rendering */); 40 } 41 42 XFree(visual_info_list); 43 if (!context) { 44 return NULL; 45 } 46 47 if (!glXMakeCurrent(display, window, context)) { 48 glXDestroyContext(display, context); 49 return NULL; 50 } 51 52 return context; 53 } 54 55 // Matrix used for the YUV to RGB conversion. 56 static const float kYUV2RGB[9] = { 57 1.f, 0.f, 1.403f, 58 1.f, -.344f, -.714f, 59 1.f, 1.772f, 0.f, 60 }; 61 62 // Vertices for a full screen quad. 63 static const float kVertices[8] = { 64 -1.f, 1.f, 65 -1.f, -1.f, 66 1.f, 1.f, 67 1.f, -1.f, 68 }; 69 70 // Texture Coordinates mapping the entire texture. 71 static const float kTextureCoords[8] = { 72 0, 0, 73 0, 1, 74 1, 0, 75 1, 1, 76 }; 77 78 // Pass-through vertex shader. 79 static const char kVertexShader[] = 80 "varying vec2 interp_tc;\n" 81 "\n" 82 "attribute vec4 in_pos;\n" 83 "attribute vec2 in_tc;\n" 84 "\n" 85 "void main() {\n" 86 " interp_tc = in_tc;\n" 87 " gl_Position = in_pos;\n" 88 "}\n"; 89 90 // YUV to RGB pixel shader. Loads a pixel from each plane and pass through the 91 // matrix. 92 static const char kFragmentShader[] = 93 "varying vec2 interp_tc;\n" 94 "\n" 95 "uniform sampler2D y_tex;\n" 96 "uniform sampler2D u_tex;\n" 97 "uniform sampler2D v_tex;\n" 98 "uniform mat3 yuv2rgb;\n" 99 "\n" 100 "void main() {\n" 101 " float y = texture2D(y_tex, interp_tc).x;\n" 102 " float u = texture2D(u_tex, interp_tc).r - .5;\n" 103 " float v = texture2D(v_tex, interp_tc).r - .5;\n" 104 " vec3 rgb = yuv2rgb * vec3(y, u, v);\n" 105 " gl_FragColor = vec4(rgb, 1);\n" 106 "}\n"; 107 108 // Buffer size for compile errors. 109 static const unsigned int kErrorSize = 4096; 110 111 GlVideoRenderer::GlVideoRenderer(Display* display, Window window) 112 : display_(display), 113 window_(window), 114 gl_context_(NULL) { 115 } 116 117 GlVideoRenderer::~GlVideoRenderer() { 118 glXMakeCurrent(display_, 0, NULL); 119 glXDestroyContext(display_, gl_context_); 120 } 121 122 void GlVideoRenderer::Paint(media::VideoFrame* video_frame) { 123 if (!gl_context_) 124 Initialize(video_frame->coded_size(), video_frame->visible_rect()); 125 126 // Convert YUV frame to RGB. 127 DCHECK(video_frame->format() == media::VideoFrame::YV12 || 128 video_frame->format() == media::VideoFrame::YV16); 129 DCHECK(video_frame->stride(media::VideoFrame::kUPlane) == 130 video_frame->stride(media::VideoFrame::kVPlane)); 131 132 if (glXGetCurrentContext() != gl_context_ || 133 glXGetCurrentDrawable() != window_) { 134 glXMakeCurrent(display_, window_, gl_context_); 135 } 136 for (unsigned int i = 0; i < kNumYUVPlanes; ++i) { 137 unsigned int width = video_frame->stride(i); 138 unsigned int height = video_frame->rows(i); 139 glActiveTexture(GL_TEXTURE0 + i); 140 glPixelStorei(GL_UNPACK_ROW_LENGTH, video_frame->stride(i)); 141 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, width, height, 0, 142 GL_LUMINANCE, GL_UNSIGNED_BYTE, video_frame->data(i)); 143 } 144 145 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); 146 glXSwapBuffers(display_, window_); 147 } 148 149 void GlVideoRenderer::Initialize(gfx::Size coded_size, gfx::Rect visible_rect) { 150 CHECK(!gl_context_); 151 LOG(INFO) << "Initializing GL Renderer..."; 152 153 // Resize the window to fit that of the video. 154 XResizeWindow(display_, window_, visible_rect.width(), visible_rect.height()); 155 156 gl_context_ = InitGLContext(display_, window_); 157 CHECK(gl_context_) << "Failed to initialize GL context"; 158 159 // Create 3 textures, one for each plane, and bind them to different 160 // texture units. 161 glGenTextures(3, textures_); 162 glActiveTexture(GL_TEXTURE0); 163 glBindTexture(GL_TEXTURE_2D, textures_[0]); 164 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 165 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 166 glEnable(GL_TEXTURE_2D); 167 168 glActiveTexture(GL_TEXTURE1); 169 glBindTexture(GL_TEXTURE_2D, textures_[1]); 170 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 171 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 172 glEnable(GL_TEXTURE_2D); 173 174 glActiveTexture(GL_TEXTURE2); 175 glBindTexture(GL_TEXTURE_2D, textures_[2]); 176 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 177 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 178 glEnable(GL_TEXTURE_2D); 179 180 GLuint program = glCreateProgram(); 181 182 // Create our YUV->RGB shader. 183 GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER); 184 const char* vs_source = kVertexShader; 185 int vs_size = sizeof(kVertexShader); 186 glShaderSource(vertex_shader, 1, &vs_source, &vs_size); 187 glCompileShader(vertex_shader); 188 int result = GL_FALSE; 189 glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &result); 190 if (!result) { 191 char log[kErrorSize]; 192 int len = 0; 193 glGetShaderInfoLog(vertex_shader, kErrorSize - 1, &len, log); 194 log[kErrorSize - 1] = 0; 195 LOG(FATAL) << log; 196 } 197 glAttachShader(program, vertex_shader); 198 glDeleteShader(vertex_shader); 199 200 GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); 201 const char* ps_source = kFragmentShader; 202 int ps_size = sizeof(kFragmentShader); 203 glShaderSource(fragment_shader, 1, &ps_source, &ps_size); 204 glCompileShader(fragment_shader); 205 result = GL_FALSE; 206 glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &result); 207 if (!result) { 208 char log[kErrorSize]; 209 int len = 0; 210 glGetShaderInfoLog(fragment_shader, kErrorSize - 1, &len, log); 211 log[kErrorSize - 1] = 0; 212 LOG(FATAL) << log; 213 } 214 glAttachShader(program, fragment_shader); 215 glDeleteShader(fragment_shader); 216 217 glLinkProgram(program); 218 result = GL_FALSE; 219 glGetProgramiv(program, GL_LINK_STATUS, &result); 220 if (!result) { 221 char log[kErrorSize]; 222 int len = 0; 223 glGetProgramInfoLog(program, kErrorSize - 1, &len, log); 224 log[kErrorSize - 1] = 0; 225 LOG(FATAL) << log; 226 } 227 glUseProgram(program); 228 glDeleteProgram(program); 229 230 // Bind parameters. 231 glUniform1i(glGetUniformLocation(program, "y_tex"), 0); 232 glUniform1i(glGetUniformLocation(program, "u_tex"), 1); 233 glUniform1i(glGetUniformLocation(program, "v_tex"), 2); 234 int yuv2rgb_location = glGetUniformLocation(program, "yuv2rgb"); 235 glUniformMatrix3fv(yuv2rgb_location, 1, GL_TRUE, kYUV2RGB); 236 237 int pos_location = glGetAttribLocation(program, "in_pos"); 238 glEnableVertexAttribArray(pos_location); 239 glVertexAttribPointer(pos_location, 2, GL_FLOAT, GL_FALSE, 0, kVertices); 240 241 int tc_location = glGetAttribLocation(program, "in_tc"); 242 glEnableVertexAttribArray(tc_location); 243 float verts[8]; 244 float x0 = static_cast<float>(visible_rect.x()) / coded_size.width(); 245 float y0 = static_cast<float>(visible_rect.y()) / coded_size.height(); 246 float x1 = static_cast<float>(visible_rect.right()) / coded_size.width(); 247 float y1 = static_cast<float>(visible_rect.bottom()) / coded_size.height(); 248 verts[0] = x0; verts[1] = y0; 249 verts[2] = x0; verts[3] = y1; 250 verts[4] = x1; verts[5] = y0; 251 verts[6] = x1; verts[7] = y1; 252 glVertexAttribPointer(tc_location, 2, GL_FLOAT, GL_FALSE, 0, verts); 253 254 // We are getting called on a thread. Release the context so that it can be 255 // made current on the main thread. 256 glXMakeCurrent(display_, 0, NULL); 257 } 258