blob: ef7ab9c1262ba872e6abda31e2712ec3252ee517 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "common_video/libyuv/include/webrtc_libyuv.h"
16#include "rtc_base/checks.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000017
18namespace webrtc {
19namespace test {
20
21GlRenderer::GlRenderer()
22 : is_init_(false), buffer_(NULL), width_(0), height_(0) {}
23
24void GlRenderer::Init() {
kwibergb890c95c2016-11-29 05:30:40 -080025 RTC_DCHECK(!is_init_);
pbos@webrtc.org29d58392013-05-16 12:08:03 +000026 is_init_ = true;
27
28 glGenTextures(1, &texture_);
29}
30
31void 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
44void 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
55void GlRenderer::ResizeVideo(size_t width, size_t height) {
kwibergb890c95c2016-11-29 05:30:40 -080056 RTC_DCHECK(is_init_);
pbos@webrtc.org29d58392013-05-16 12:08:03 +000057 width_ = width;
58 height_ = height;
59
60 buffer_size_ = width * height * 4; // BGRA
61
62 delete[] buffer_;
63 buffer_ = new uint8_t[buffer_size_];
kwibergb890c95c2016-11-29 05:30:40 -080064 RTC_DCHECK(buffer_);
pbos@webrtc.org29d58392013-05-16 12:08:03 +000065 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
nisseeb83a1a2016-03-21 01:27:56 -070073void GlRenderer::OnFrame(const webrtc::VideoFrame& frame) {
kwibergb890c95c2016-11-29 05:30:40 -080074 RTC_DCHECK(is_init_);
pbos@webrtc.org29d58392013-05-16 12:08:03 +000075
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
nisseeb44b392017-04-28 07:18:05 -070081 webrtc::ConvertFromI420(frame, VideoType::kBGRA, 0, buffer_);
pbos@webrtc.org29d58392013-05-16 12:08:03 +000082
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