blob: f2c7acd6c0065030503b3492d61da8b8175108ef [file] [log] [blame]
pbos@webrtc.org2a9108f2013-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.org24e20892013-10-28 16:32:01 +000011#include "webrtc/test/gl/gl_renderer.h"
pbos@webrtc.org2a9108f2013-05-16 12:08:03 +000012
pbos@webrtc.org3f45c2e2013-08-05 16:22:53 +000013#include <string.h>
pbos@webrtc.org2a9108f2013-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
72void GlRenderer::RenderFrame(const webrtc::I420VideoFrame& 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