Home | History | Annotate | Download | only in player_x11
      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 // Pass-through vertex shader.
     71 static const char kVertexShader[] =
     72     "varying vec2 interp_tc;\n"
     73     "\n"
     74     "attribute vec4 in_pos;\n"
     75     "attribute vec2 in_tc;\n"
     76     "\n"
     77     "void main() {\n"
     78     "  interp_tc = in_tc;\n"
     79     "  gl_Position = in_pos;\n"
     80     "}\n";
     81 
     82 // YUV to RGB pixel shader. Loads a pixel from each plane and pass through the
     83 // matrix.
     84 static const char kFragmentShader[] =
     85     "varying vec2 interp_tc;\n"
     86     "\n"
     87     "uniform sampler2D y_tex;\n"
     88     "uniform sampler2D u_tex;\n"
     89     "uniform sampler2D v_tex;\n"
     90     "uniform mat3 yuv2rgb;\n"
     91     "\n"
     92     "void main() {\n"
     93     "  float y = texture2D(y_tex, interp_tc).x;\n"
     94     "  float u = texture2D(u_tex, interp_tc).r - .5;\n"
     95     "  float v = texture2D(v_tex, interp_tc).r - .5;\n"
     96     "  vec3 rgb = yuv2rgb * vec3(y, u, v);\n"
     97     "  gl_FragColor = vec4(rgb, 1);\n"
     98     "}\n";
     99 
    100 // Buffer size for compile errors.
    101 static const unsigned int kErrorSize = 4096;
    102 
    103 GlVideoRenderer::GlVideoRenderer(Display* display, Window window)
    104     : display_(display),
    105       window_(window),
    106       gl_context_(NULL) {
    107 }
    108 
    109 GlVideoRenderer::~GlVideoRenderer() {
    110   glXMakeCurrent(display_, 0, NULL);
    111   glXDestroyContext(display_, gl_context_);
    112 }
    113 
    114 void GlVideoRenderer::Paint(media::VideoFrame* video_frame) {
    115   if (!gl_context_)
    116     Initialize(video_frame->coded_size(), video_frame->visible_rect());
    117 
    118   // Convert YUV frame to RGB.
    119   DCHECK(video_frame->format() == media::VideoFrame::YV12 ||
    120          video_frame->format() == media::VideoFrame::YV16);
    121   DCHECK(video_frame->stride(media::VideoFrame::kUPlane) ==
    122          video_frame->stride(media::VideoFrame::kVPlane));
    123 
    124   if (glXGetCurrentContext() != gl_context_ ||
    125       glXGetCurrentDrawable() != window_) {
    126     glXMakeCurrent(display_, window_, gl_context_);
    127   }
    128   for (unsigned int i = 0; i < kNumYUVPlanes; ++i) {
    129     unsigned int width = video_frame->stride(i);
    130     unsigned int height = video_frame->rows(i);
    131     glActiveTexture(GL_TEXTURE0 + i);
    132     glPixelStorei(GL_UNPACK_ROW_LENGTH, video_frame->stride(i));
    133     glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, width, height, 0,
    134                  GL_LUMINANCE, GL_UNSIGNED_BYTE, video_frame->data(i));
    135   }
    136 
    137   glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    138   glXSwapBuffers(display_, window_);
    139 }
    140 
    141 void GlVideoRenderer::Initialize(gfx::Size coded_size, gfx::Rect visible_rect) {
    142   CHECK(!gl_context_);
    143   VLOG(0) << "Initializing GL Renderer...";
    144 
    145   // Resize the window to fit that of the video.
    146   XResizeWindow(display_, window_, visible_rect.width(), visible_rect.height());
    147 
    148   gl_context_ = InitGLContext(display_, window_);
    149   CHECK(gl_context_) << "Failed to initialize GL context";
    150 
    151   // Create 3 textures, one for each plane, and bind them to different
    152   // texture units.
    153   glGenTextures(3, textures_);
    154   glActiveTexture(GL_TEXTURE0);
    155   glBindTexture(GL_TEXTURE_2D, textures_[0]);
    156   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    157   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    158   glEnable(GL_TEXTURE_2D);
    159 
    160   glActiveTexture(GL_TEXTURE1);
    161   glBindTexture(GL_TEXTURE_2D, textures_[1]);
    162   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    163   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    164   glEnable(GL_TEXTURE_2D);
    165 
    166   glActiveTexture(GL_TEXTURE2);
    167   glBindTexture(GL_TEXTURE_2D, textures_[2]);
    168   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    169   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    170   glEnable(GL_TEXTURE_2D);
    171 
    172   GLuint program = glCreateProgram();
    173 
    174   // Create our YUV->RGB shader.
    175   GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
    176   const char* vs_source = kVertexShader;
    177   int vs_size = sizeof(kVertexShader);
    178   glShaderSource(vertex_shader, 1, &vs_source, &vs_size);
    179   glCompileShader(vertex_shader);
    180   int result = GL_FALSE;
    181   glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &result);
    182   if (!result) {
    183     char log[kErrorSize];
    184     int len = 0;
    185     glGetShaderInfoLog(vertex_shader, kErrorSize - 1, &len, log);
    186     log[kErrorSize - 1] = 0;
    187     LOG(FATAL) << log;
    188   }
    189   glAttachShader(program, vertex_shader);
    190   glDeleteShader(vertex_shader);
    191 
    192   GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
    193   const char* ps_source = kFragmentShader;
    194   int ps_size = sizeof(kFragmentShader);
    195   glShaderSource(fragment_shader, 1, &ps_source, &ps_size);
    196   glCompileShader(fragment_shader);
    197   result = GL_FALSE;
    198   glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &result);
    199   if (!result) {
    200     char log[kErrorSize];
    201     int len = 0;
    202     glGetShaderInfoLog(fragment_shader, kErrorSize - 1, &len, log);
    203     log[kErrorSize - 1] = 0;
    204     LOG(FATAL) << log;
    205   }
    206   glAttachShader(program, fragment_shader);
    207   glDeleteShader(fragment_shader);
    208 
    209   glLinkProgram(program);
    210   result = GL_FALSE;
    211   glGetProgramiv(program, GL_LINK_STATUS, &result);
    212   if (!result) {
    213     char log[kErrorSize];
    214     int len = 0;
    215     glGetProgramInfoLog(program, kErrorSize - 1, &len, log);
    216     log[kErrorSize - 1] = 0;
    217     LOG(FATAL) << log;
    218   }
    219   glUseProgram(program);
    220   glDeleteProgram(program);
    221 
    222   // Bind parameters.
    223   glUniform1i(glGetUniformLocation(program, "y_tex"), 0);
    224   glUniform1i(glGetUniformLocation(program, "u_tex"), 1);
    225   glUniform1i(glGetUniformLocation(program, "v_tex"), 2);
    226   int yuv2rgb_location = glGetUniformLocation(program, "yuv2rgb");
    227   glUniformMatrix3fv(yuv2rgb_location, 1, GL_TRUE, kYUV2RGB);
    228 
    229   int pos_location = glGetAttribLocation(program, "in_pos");
    230   glEnableVertexAttribArray(pos_location);
    231   glVertexAttribPointer(pos_location, 2, GL_FLOAT, GL_FALSE, 0, kVertices);
    232 
    233   int tc_location = glGetAttribLocation(program, "in_tc");
    234   glEnableVertexAttribArray(tc_location);
    235   float verts[8];
    236   float x0 = static_cast<float>(visible_rect.x()) / coded_size.width();
    237   float y0 = static_cast<float>(visible_rect.y()) / coded_size.height();
    238   float x1 = static_cast<float>(visible_rect.right()) / coded_size.width();
    239   float y1 = static_cast<float>(visible_rect.bottom()) / coded_size.height();
    240   verts[0] = x0; verts[1] = y0;
    241   verts[2] = x0; verts[3] = y1;
    242   verts[4] = x1; verts[5] = y0;
    243   verts[6] = x1; verts[7] = y1;
    244   glVertexAttribPointer(tc_location, 2, GL_FLOAT, GL_FALSE, 0, verts);
    245 
    246   // We are getting called on a thread. Release the context so that it can be
    247   // made current on the main thread.
    248   glXMakeCurrent(display_, 0, NULL);
    249 }
    250