You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
2.8 KiB

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