blob: 8ce373b1c1b02eb59909d124b725bf7e401e3cd2 [file] [log] [blame]
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001/*
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
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000011#include "webrtc/test/gl/gl_renderer.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000012
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013#include <string.h>
pbos@webrtc.org29d58392013-05-16 12:08:03 +000014
15#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
16
17namespace webrtc {
18namespace test {
19
20GlRenderer::GlRenderer()
21 : is_init_(false), buffer_(NULL), width_(0), height_(0) {}
22
23void GlRenderer::Init() {
24 assert(!is_init_);
25 is_init_ = true;
26
27 glGenTextures(1, &texture_);
28}
29
30void 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
43void 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
54void 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
nisseeb83a1a2016-03-21 01:27:56 -070072void GlRenderer::OnFrame(const webrtc::VideoFrame& frame) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +000073 assert(is_init_);
74
75 if (static_cast<size_t>(frame.width()) != width_ ||
76 static_cast<size_t>(frame.height()) != height_) {
77 ResizeVideo(frame.width(), frame.height());
78 }
79
80 webrtc::ConvertFromI420(frame, kBGRA, 0, buffer_);
81
82 glEnable(GL_TEXTURE_2D);
83 glBindTexture(GL_TEXTURE_2D, texture_);
84 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width_, height_, GL_BGRA,
85 GL_UNSIGNED_INT_8_8_8_8, buffer_);
86
87 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
88
89 glLoadIdentity();
90
91 glBegin(GL_QUADS);
92 {
93 glTexCoord2f(0.0f, 0.0f);
94 glVertex3f(0.0f, 0.0f, 0.0f);
95
96 glTexCoord2f(0.0f, 1.0f);
97 glVertex3f(0.0f, 1.0f, 0.0f);
98
99 glTexCoord2f(1.0f, 1.0f);
100 glVertex3f(1.0f, 1.0f, 0.0f);
101
102 glTexCoord2f(1.0f, 0.0f);
103 glVertex3f(1.0f, 0.0f, 0.0f);
104 }
105 glEnd();
106
107 glBindTexture(GL_TEXTURE_2D, 0);
108 glFlush();
109}
110} // test
111} // webrtc