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.

176 lines
4.7 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/linux/glx_renderer.h"
  11. #include <X11/Xlib.h>
  12. #include <X11/Xutil.h>
  13. #include <assert.h>
  14. #include <stdlib.h>
  15. namespace webrtc {
  16. namespace test {
  17. GlxRenderer::GlxRenderer(size_t width, size_t height)
  18. : width_(width), height_(height), display_(NULL), context_(NULL) {
  19. assert(width > 0);
  20. assert(height > 0);
  21. }
  22. GlxRenderer::~GlxRenderer() {
  23. Destroy();
  24. }
  25. bool GlxRenderer::Init(const char* window_title) {
  26. if ((display_ = XOpenDisplay(NULL)) == NULL) {
  27. Destroy();
  28. return false;
  29. }
  30. int screen = DefaultScreen(display_);
  31. XVisualInfo* vi;
  32. int attr_list[] = {
  33. GLX_DOUBLEBUFFER, GLX_RGBA, GLX_RED_SIZE, 4, GLX_GREEN_SIZE, 4,
  34. GLX_BLUE_SIZE, 4, GLX_DEPTH_SIZE, 16, None,
  35. };
  36. if ((vi = glXChooseVisual(display_, screen, attr_list)) == NULL) {
  37. Destroy();
  38. return false;
  39. }
  40. context_ = glXCreateContext(display_, vi, 0, true);
  41. if (context_ == NULL) {
  42. Destroy();
  43. return false;
  44. }
  45. XSetWindowAttributes window_attributes;
  46. window_attributes.colormap = XCreateColormap(
  47. display_, RootWindow(display_, vi->screen), vi->visual, AllocNone);
  48. window_attributes.border_pixel = 0;
  49. window_attributes.event_mask = StructureNotifyMask | ExposureMask;
  50. window_ = XCreateWindow(display_, RootWindow(display_, vi->screen), 0, 0,
  51. width_, height_, 0, vi->depth, InputOutput,
  52. vi->visual, CWBorderPixel | CWColormap | CWEventMask,
  53. &window_attributes);
  54. XFree(vi);
  55. XSetStandardProperties(display_, window_, window_title, window_title, None,
  56. NULL, 0, NULL);
  57. Atom wm_delete = XInternAtom(display_, "WM_DELETE_WINDOW", True);
  58. if (wm_delete != None) {
  59. XSetWMProtocols(display_, window_, &wm_delete, 1);
  60. }
  61. XMapRaised(display_, window_);
  62. if (!glXMakeCurrent(display_, window_, context_)) {
  63. Destroy();
  64. return false;
  65. }
  66. GlRenderer::Init();
  67. if (!glXMakeCurrent(display_, None, NULL)) {
  68. Destroy();
  69. return false;
  70. }
  71. Resize(width_, height_);
  72. return true;
  73. }
  74. void GlxRenderer::Destroy() {
  75. if (context_ != NULL) {
  76. glXMakeCurrent(display_, window_, context_);
  77. GlRenderer::Destroy();
  78. glXMakeCurrent(display_, None, NULL);
  79. glXDestroyContext(display_, context_);
  80. context_ = NULL;
  81. }
  82. if (display_ != NULL) {
  83. XCloseDisplay(display_);
  84. display_ = NULL;
  85. }
  86. }
  87. GlxRenderer* GlxRenderer::Create(const char* window_title,
  88. size_t width,
  89. size_t height) {
  90. GlxRenderer* glx_renderer = new GlxRenderer(width, height);
  91. if (!glx_renderer->Init(window_title)) {
  92. // TODO(pbos): Add GLX-failed warning here?
  93. delete glx_renderer;
  94. return NULL;
  95. }
  96. return glx_renderer;
  97. }
  98. void GlxRenderer::Resize(size_t width, size_t height) {
  99. width_ = width;
  100. height_ = height;
  101. if (!glXMakeCurrent(display_, window_, context_)) {
  102. abort();
  103. }
  104. GlRenderer::ResizeViewport(width_, height_);
  105. if (!glXMakeCurrent(display_, None, NULL)) {
  106. abort();
  107. }
  108. XSizeHints* size_hints = XAllocSizeHints();
  109. if (size_hints == NULL) {
  110. abort();
  111. }
  112. size_hints->flags = PAspect;
  113. size_hints->min_aspect.x = size_hints->max_aspect.x = width_;
  114. size_hints->min_aspect.y = size_hints->max_aspect.y = height_;
  115. XSetWMNormalHints(display_, window_, size_hints);
  116. XFree(size_hints);
  117. XWindowChanges wc;
  118. wc.width = static_cast<int>(width);
  119. wc.height = static_cast<int>(height);
  120. XConfigureWindow(display_, window_, CWWidth | CWHeight, &wc);
  121. }
  122. void GlxRenderer::OnFrame(const webrtc::VideoFrame& frame) {
  123. if (static_cast<size_t>(frame.width()) != width_ ||
  124. static_cast<size_t>(frame.height()) != height_) {
  125. Resize(static_cast<size_t>(frame.width()),
  126. static_cast<size_t>(frame.height()));
  127. }
  128. XEvent event;
  129. if (!glXMakeCurrent(display_, window_, context_)) {
  130. abort();
  131. }
  132. while (XPending(display_)) {
  133. XNextEvent(display_, &event);
  134. switch (event.type) {
  135. case ConfigureNotify:
  136. GlRenderer::ResizeViewport(event.xconfigure.width,
  137. event.xconfigure.height);
  138. break;
  139. default:
  140. break;
  141. }
  142. }
  143. GlRenderer::OnFrame(frame);
  144. glXSwapBuffers(display_, window_);
  145. if (!glXMakeCurrent(display_, None, NULL)) {
  146. abort();
  147. }
  148. }
  149. } // namespace test
  150. } // namespace webrtc