DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (777e60ca8853)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
/*
 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

// This files is mostly copied from
// webrtc/modules/video_render/android/video_render_opengles20.h

// TODO(sjlee): unify this copy with the android one.
#include "webrtc/modules/video_render/ios/open_gles20.h"
#include "webrtc/system_wrappers/interface/trace.h"

using namespace webrtc;

const char OpenGles20::indices_[] = {0, 3, 2, 0, 2, 1};

const char OpenGles20::vertext_shader_[] = {
    "attribute vec4 aPosition;\n"
    "attribute vec2 aTextureCoord;\n"
    "varying vec2 vTextureCoord;\n"
    "void main() {\n"
    "  gl_Position = aPosition;\n"
    "  vTextureCoord = aTextureCoord;\n"
    "}\n"};

// The fragment shader.
// Do YUV to RGB565 conversion.
const char OpenGles20::fragment_shader_[] = {
    "precision mediump float;\n"
    "uniform sampler2D Ytex;\n"
    "uniform sampler2D Utex,Vtex;\n"
    "varying vec2 vTextureCoord;\n"
    "void main(void) {\n"
    "  float nx,ny,r,g,b,y,u,v;\n"
    "  mediump vec4 txl,ux,vx;"
    "  nx=vTextureCoord[0];\n"
    "  ny=vTextureCoord[1];\n"
    "  y=texture2D(Ytex,vec2(nx,ny)).r;\n"
    "  u=texture2D(Utex,vec2(nx,ny)).r;\n"
    "  v=texture2D(Vtex,vec2(nx,ny)).r;\n"
    "  y=1.1643*(y-0.0625);\n"
    "  u=u-0.5;\n"
    "  v=v-0.5;\n"
    "  r=y+1.5958*v;\n"
    "  g=y-0.39173*u-0.81290*v;\n"
    "  b=y+2.017*u;\n"
    "  gl_FragColor=vec4(r,g,b,1.0);\n"
    "}\n"};

OpenGles20::OpenGles20() : texture_width_(-1), texture_height_(-1) {
  texture_ids_[0] = 0;
  texture_ids_[1] = 0;
  texture_ids_[2] = 0;

  program_ = 0;

  const GLfloat vertices[20] = {
      // X, Y, Z, U, V
      -1, -1, 0, 0, 1,   // Bottom Left
      1,  -1, 0, 1, 1,   // Bottom Right
      1,  1,  0, 1, 0,   // Top Right
      -1, 1,  0, 0, 0};  // Top Left

  memcpy(vertices_, vertices, sizeof(vertices_));
}

OpenGles20::~OpenGles20() {
  if (program_) {
    glDeleteTextures(3, texture_ids_);
    glDeleteProgram(program_);
  }
}

bool OpenGles20::Setup(int32_t width, int32_t height) {
  program_ = CreateProgram(vertext_shader_, fragment_shader_);
  if (!program_) {
    return false;
  }

  int position_handle = glGetAttribLocation(program_, "aPosition");
  int texture_handle = glGetAttribLocation(program_, "aTextureCoord");

  // set the vertices array in the shader
  // vertices_ contains 4 vertices with 5 coordinates.
  // 3 for (xyz) for the vertices and 2 for the texture
  glVertexAttribPointer(
      position_handle, 3, GL_FLOAT, false, 5 * sizeof(GLfloat), vertices_);

  glEnableVertexAttribArray(position_handle);

  // set the texture coordinate array in the shader
  // vertices_ contains 4 vertices with 5 coordinates.
  // 3 for (xyz) for the vertices and 2 for the texture
  glVertexAttribPointer(
      texture_handle, 2, GL_FLOAT, false, 5 * sizeof(GLfloat), &vertices_[3]);
  glEnableVertexAttribArray(texture_handle);

  glUseProgram(program_);
  int i = glGetUniformLocation(program_, "Ytex");
  glUniform1i(i, 0); /* Bind Ytex to texture unit 0 */

  i = glGetUniformLocation(program_, "Utex");
  glUniform1i(i, 1); /* Bind Utex to texture unit 1 */

  i = glGetUniformLocation(program_, "Vtex");
  glUniform1i(i, 2); /* Bind Vtex to texture unit 2 */

  glViewport(0, 0, width, height);
  return true;
}

bool OpenGles20::SetCoordinates(const float z_order,
                                const float left,
                                const float top,
                                const float right,
                                const float bottom) {
  if (top > 1 || top < 0 || right > 1 || right < 0 || bottom > 1 ||
      bottom < 0 || left > 1 || left < 0) {
    return false;
  }

  // Bottom Left
  vertices_[0] = (left * 2) - 1;
  vertices_[1] = -1 * (2 * bottom) + 1;
  vertices_[2] = z_order;

  // Bottom Right
  vertices_[5] = (right * 2) - 1;
  vertices_[6] = -1 * (2 * bottom) + 1;
  vertices_[7] = z_order;

  // Top Right
  vertices_[10] = (right * 2) - 1;
  vertices_[11] = -1 * (2 * top) + 1;
  vertices_[12] = z_order;

  // Top Left
  vertices_[15] = (left * 2) - 1;
  vertices_[16] = -1 * (2 * top) + 1;
  vertices_[17] = z_order;

  return true;
}

bool OpenGles20::Render(const I420VideoFrame& frame) {
  if (texture_width_ != (GLsizei)frame.width() ||
      texture_height_ != (GLsizei)frame.height()) {
    SetupTextures(frame);
  }
  UpdateTextures(frame);

  glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices_);

  return true;
}

GLuint OpenGles20::LoadShader(GLenum shader_type, const char* shader_source) {
  GLuint shader = glCreateShader(shader_type);
  if (shader) {
    glShaderSource(shader, 1, &shader_source, NULL);
    glCompileShader(shader);

    GLint compiled = 0;
    glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
    if (!compiled) {
      GLint info_len = 0;
      glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &info_len);
      if (info_len) {
        char* buf = (char*)malloc(info_len);
        glGetShaderInfoLog(shader, info_len, NULL, buf);
        WEBRTC_TRACE(kTraceError,
                     kTraceVideoRenderer,
                     0,
                     "%s: Could not compile shader %d: %s",
                     __FUNCTION__,
                     shader_type,
                     buf);
        free(buf);
      }
      glDeleteShader(shader);
      shader = 0;
    }
  }
  return shader;
}

GLuint OpenGles20::CreateProgram(const char* vertex_source,
                                 const char* fragment_source) {
  GLuint vertex_shader = LoadShader(GL_VERTEX_SHADER, vertex_source);
  if (!vertex_shader) {
    return -1;
  }

  GLuint fragment_shader = LoadShader(GL_FRAGMENT_SHADER, fragment_source);
  if (!fragment_shader) {
    return -1;
  }

  GLuint program = glCreateProgram();
  if (program) {
    glAttachShader(program, vertex_shader);
    glAttachShader(program, fragment_shader);
    glLinkProgram(program);
    GLint link_status = GL_FALSE;
    glGetProgramiv(program, GL_LINK_STATUS, &link_status);
    if (link_status != GL_TRUE) {
      GLint info_len = 0;
      glGetProgramiv(program, GL_INFO_LOG_LENGTH, &info_len);
      if (info_len) {
        char* buf = (char*)malloc(info_len);
        glGetProgramInfoLog(program, info_len, NULL, buf);
        WEBRTC_TRACE(kTraceError,
                     kTraceVideoRenderer,
                     0,
                     "%s: Could not link program: %s",
                     __FUNCTION__,
                     buf);
        free(buf);
      }
      glDeleteProgram(program);
      program = 0;
    }
  }

  if (vertex_shader) {
    glDeleteShader(vertex_shader);
  }

  if (fragment_shader) {
    glDeleteShader(fragment_shader);
  }

  return program;
}

static void InitializeTexture(int name, int id, int width, int height) {
  glActiveTexture(name);
  glBindTexture(GL_TEXTURE_2D, id);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  glTexImage2D(GL_TEXTURE_2D,
               0,
               GL_LUMINANCE,
               width,
               height,
               0,
               GL_LUMINANCE,
               GL_UNSIGNED_BYTE,
               NULL);
}

void OpenGles20::SetupTextures(const I420VideoFrame& frame) {
  const GLsizei width = frame.width();
  const GLsizei height = frame.height();

  if (!texture_ids_[0]) {
    glGenTextures(3, texture_ids_);  // Generate  the Y, U and V texture
  }

  InitializeTexture(GL_TEXTURE0, texture_ids_[0], width, height);
  InitializeTexture(GL_TEXTURE1, texture_ids_[1], width / 2, height / 2);
  InitializeTexture(GL_TEXTURE2, texture_ids_[2], width / 2, height / 2);

  texture_width_ = width;
  texture_height_ = height;
}

// Uploads a plane of pixel data, accounting for stride != width*bpp.
static void GlTexSubImage2D(GLsizei width,
                            GLsizei height,
                            int stride,
                            const uint8_t* plane) {
  if (stride == width) {
    // Yay!  We can upload the entire plane in a single GL call.
    glTexSubImage2D(GL_TEXTURE_2D,
                    0,
                    0,
                    0,
                    width,
                    height,
                    GL_LUMINANCE,
                    GL_UNSIGNED_BYTE,
                    static_cast<const GLvoid*>(plane));
  } else {
    // Boo!  Since GLES2 doesn't have GL_UNPACK_ROW_LENGTH and iOS doesn't
    // have GL_EXT_unpack_subimage we have to upload a row at a time.  Ick.
    for (int row = 0; row < height; ++row) {
      glTexSubImage2D(GL_TEXTURE_2D,
                      0,
                      0,
                      row,
                      width,
                      1,
                      GL_LUMINANCE,
                      GL_UNSIGNED_BYTE,
                      static_cast<const GLvoid*>(plane + (row * stride)));
    }
  }
}

void OpenGles20::UpdateTextures(const I420VideoFrame& frame) {
  const GLsizei width = frame.width();
  const GLsizei height = frame.height();

  glActiveTexture(GL_TEXTURE0);
  glBindTexture(GL_TEXTURE_2D, texture_ids_[0]);
  GlTexSubImage2D(width, height, frame.stride(kYPlane), frame.buffer(kYPlane));

  glActiveTexture(GL_TEXTURE1);
  glBindTexture(GL_TEXTURE_2D, texture_ids_[1]);
  GlTexSubImage2D(
      width / 2, height / 2, frame.stride(kUPlane), frame.buffer(kUPlane));

  glActiveTexture(GL_TEXTURE2);
  glBindTexture(GL_TEXTURE_2D, texture_ids_[2]);
  GlTexSubImage2D(
      width / 2, height / 2, frame.stride(kVPlane), frame.buffer(kVPlane));
}