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