• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "webrtc/test/gl/gl_renderer.h"
12 
13 #include <string.h>
14 
15 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
16 
17 namespace webrtc {
18 namespace test {
19 
GlRenderer()20 GlRenderer::GlRenderer()
21     : is_init_(false), buffer_(NULL), width_(0), height_(0) {}
22 
Init()23 void GlRenderer::Init() {
24   assert(!is_init_);
25   is_init_ = true;
26 
27   glGenTextures(1, &texture_);
28 }
29 
Destroy()30 void GlRenderer::Destroy() {
31   if (!is_init_) {
32     return;
33   }
34 
35   is_init_ = false;
36 
37   delete[] buffer_;
38   buffer_ = NULL;
39 
40   glDeleteTextures(1, &texture_);
41 }
42 
ResizeViewport(size_t width,size_t height)43 void GlRenderer::ResizeViewport(size_t width, size_t height) {
44   // TODO(pbos): Aspect ratio, letterbox the video.
45   glViewport(0, 0, width, height);
46 
47   glMatrixMode(GL_PROJECTION);
48   glLoadIdentity();
49   glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
50   glOrtho(0.0f, 1.0f, 1.0f, 0.0f, -1.0f, 1.0f);
51   glMatrixMode(GL_MODELVIEW);
52 }
53 
ResizeVideo(size_t width,size_t height)54 void GlRenderer::ResizeVideo(size_t width, size_t height) {
55   assert(is_init_);
56   width_ = width;
57   height_ = height;
58 
59   buffer_size_ = width * height * 4;  // BGRA
60 
61   delete[] buffer_;
62   buffer_ = new uint8_t[buffer_size_];
63   assert(buffer_ != NULL);
64   memset(buffer_, 0, buffer_size_);
65   glBindTexture(GL_TEXTURE_2D, texture_);
66   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
67   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
68   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGRA,
69                GL_UNSIGNED_INT_8_8_8_8, static_cast<GLvoid*>(buffer_));
70 }
71 
RenderFrame(const webrtc::VideoFrame & frame,int)72 void GlRenderer::RenderFrame(const webrtc::VideoFrame& frame,
73                              int /*render_delay_ms*/) {
74   assert(is_init_);
75 
76   if (static_cast<size_t>(frame.width()) != width_ ||
77       static_cast<size_t>(frame.height()) != height_) {
78     ResizeVideo(frame.width(), frame.height());
79   }
80 
81   webrtc::ConvertFromI420(frame, kBGRA, 0, buffer_);
82 
83   glEnable(GL_TEXTURE_2D);
84   glBindTexture(GL_TEXTURE_2D, texture_);
85   glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width_, height_, GL_BGRA,
86                   GL_UNSIGNED_INT_8_8_8_8, buffer_);
87 
88   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
89 
90   glLoadIdentity();
91 
92   glBegin(GL_QUADS);
93   {
94     glTexCoord2f(0.0f, 0.0f);
95     glVertex3f(0.0f, 0.0f, 0.0f);
96 
97     glTexCoord2f(0.0f, 1.0f);
98     glVertex3f(0.0f, 1.0f, 0.0f);
99 
100     glTexCoord2f(1.0f, 1.0f);
101     glVertex3f(1.0f, 1.0f, 0.0f);
102 
103     glTexCoord2f(1.0f, 0.0f);
104     glVertex3f(1.0f, 0.0f, 0.0f);
105   }
106   glEnd();
107 
108   glBindTexture(GL_TEXTURE_2D, 0);
109   glFlush();
110 }
111 }  // test
112 }  // webrtc
113