• 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 "test/gl/gl_renderer.h"
12 
13 #include <string.h>
14 
15 #include "common_video/libyuv/include/webrtc_libyuv.h"
16 #include "rtc_base/checks.h"
17 
18 namespace webrtc {
19 namespace test {
20 
GlRenderer()21 GlRenderer::GlRenderer()
22     : is_init_(false), buffer_(NULL), width_(0), height_(0) {}
23 
Init()24 void GlRenderer::Init() {
25   RTC_DCHECK(!is_init_);
26   is_init_ = true;
27 
28   glGenTextures(1, &texture_);
29 }
30 
Destroy()31 void GlRenderer::Destroy() {
32   if (!is_init_) {
33     return;
34   }
35 
36   is_init_ = false;
37 
38   delete[] buffer_;
39   buffer_ = NULL;
40 
41   glDeleteTextures(1, &texture_);
42 }
43 
ResizeViewport(size_t width,size_t height)44 void GlRenderer::ResizeViewport(size_t width, size_t height) {
45   // TODO(pbos): Aspect ratio, letterbox the video.
46   glViewport(0, 0, width, height);
47 
48   glMatrixMode(GL_PROJECTION);
49   glLoadIdentity();
50   glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
51   glOrtho(0.0f, 1.0f, 1.0f, 0.0f, -1.0f, 1.0f);
52   glMatrixMode(GL_MODELVIEW);
53 }
54 
ResizeVideo(size_t width,size_t height)55 void GlRenderer::ResizeVideo(size_t width, size_t height) {
56   RTC_DCHECK(is_init_);
57   width_ = width;
58   height_ = height;
59 
60   buffer_size_ = width * height * 4;  // BGRA
61 
62   delete[] buffer_;
63   buffer_ = new uint8_t[buffer_size_];
64   RTC_DCHECK(buffer_);
65   memset(buffer_, 0, buffer_size_);
66   glBindTexture(GL_TEXTURE_2D, texture_);
67   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
68   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
69   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGRA,
70                GL_UNSIGNED_INT_8_8_8_8, static_cast<GLvoid*>(buffer_));
71 }
72 
OnFrame(const webrtc::VideoFrame & frame)73 void GlRenderer::OnFrame(const webrtc::VideoFrame& frame) {
74   RTC_DCHECK(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, VideoType::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 }  // namespace test
112 }  // namespace webrtc
113