blob: aaff84bc8f570668cb80cdc77e5c0f6a7f22025c [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/linux/glx_renderer.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000012
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013#include <assert.h>
kwibergc891eb42016-03-02 03:41:34 -080014#include <stdlib.h>
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000015
pbos@webrtc.org29d58392013-05-16 12:08:03 +000016#include <X11/Xatom.h>
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000017#include <X11/Xlib.h>
pbos@webrtc.org29d58392013-05-16 12:08:03 +000018
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "common_video/libyuv/include/webrtc_libyuv.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000020
21namespace webrtc {
22namespace test {
23
24GlxRenderer::GlxRenderer(size_t width, size_t height)
Yves Gerey665174f2018-06-19 15:03:05 +020025 : width_(width), height_(height), display_(NULL), context_(NULL) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +000026 assert(width > 0);
27 assert(height > 0);
28}
29
Yves Gerey665174f2018-06-19 15:03:05 +020030GlxRenderer::~GlxRenderer() {
31 Destroy();
32}
pbos@webrtc.org28556f52013-05-24 10:54:56 +000033
pbos@webrtc.org29d58392013-05-16 12:08:03 +000034bool GlxRenderer::Init(const char* window_title) {
35 if ((display_ = XOpenDisplay(NULL)) == NULL) {
36 Destroy();
37 return false;
38 }
39
40 int screen = DefaultScreen(display_);
41
42 XVisualInfo* vi;
Yves Gerey665174f2018-06-19 15:03:05 +020043 int attr_list[] = {
44 GLX_DOUBLEBUFFER, GLX_RGBA, GLX_RED_SIZE, 4, GLX_GREEN_SIZE, 4,
45 GLX_BLUE_SIZE, 4, GLX_DEPTH_SIZE, 16, None,
46 };
pbos@webrtc.org29d58392013-05-16 12:08:03 +000047
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
94void GlxRenderer::Destroy() {
pbos@webrtc.org29d58392013-05-16 12:08:03 +000095 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
Yves Gerey665174f2018-06-19 15:03:05 +0200109GlxRenderer* GlxRenderer::Create(const char* window_title,
110 size_t width,
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000111 size_t height) {
112 GlxRenderer* glx_renderer = new GlxRenderer(width, height);
113 if (!glx_renderer->Init(window_title)) {
114 // TODO(pbos): Add GLX-failed warning here?
115 delete glx_renderer;
116 return NULL;
117 }
118 return glx_renderer;
119}
120
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000121void GlxRenderer::Resize(size_t width, size_t height) {
122 width_ = width;
123 height_ = height;
124 if (!glXMakeCurrent(display_, window_, context_)) {
125 abort();
126 }
127 GlRenderer::ResizeViewport(width_, height_);
128 if (!glXMakeCurrent(display_, None, NULL)) {
129 abort();
130 }
131
132 XSizeHints* size_hints = XAllocSizeHints();
133 if (size_hints == NULL) {
134 abort();
135 }
136 size_hints->flags = PAspect;
137 size_hints->min_aspect.x = size_hints->max_aspect.x = width_;
138 size_hints->min_aspect.y = size_hints->max_aspect.y = height_;
139 XSetWMNormalHints(display_, window_, size_hints);
140 XFree(size_hints);
pbos@webrtc.org6998c8e2013-06-04 11:56:06 +0000141
142 XWindowChanges wc;
143 wc.width = static_cast<int>(width);
144 wc.height = static_cast<int>(height);
145 XConfigureWindow(display_, window_, CWWidth | CWHeight, &wc);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000146}
147
nisseeb83a1a2016-03-21 01:27:56 -0700148void GlxRenderer::OnFrame(const webrtc::VideoFrame& frame) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000149 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
nisseeb83a1a2016-03-21 01:27:56 -0700171 GlRenderer::OnFrame(frame);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000172 glXSwapBuffers(display_, window_);
173
174 if (!glXMakeCurrent(display_, None, NULL)) {
175 abort();
176 }
177}
Yves Gerey665174f2018-06-19 15:03:05 +0200178} // namespace test
179} // namespace webrtc