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