blob: 50f2a06a8e753835c96eeaf5b0f9cadb1948fe90 [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
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <X11/Xlib.h>
14#include <X11/Xutil.h>
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000015#include <assert.h>
kwibergc891eb42016-03-02 03:41:34 -080016#include <stdlib.h>
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000017
pbos@webrtc.org29d58392013-05-16 12:08:03 +000018namespace webrtc {
19namespace test {
20
21GlxRenderer::GlxRenderer(size_t width, size_t height)
Yves Gerey665174f2018-06-19 15:03:05 +020022 : width_(width), height_(height), display_(NULL), context_(NULL) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +000023 assert(width > 0);
24 assert(height > 0);
25}
26
Yves Gerey665174f2018-06-19 15:03:05 +020027GlxRenderer::~GlxRenderer() {
28 Destroy();
29}
pbos@webrtc.org28556f52013-05-24 10:54:56 +000030
pbos@webrtc.org29d58392013-05-16 12:08:03 +000031bool 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 Gerey665174f2018-06-19 15:03:05 +020040 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.org29d58392013-05-16 12:08:03 +000044
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
91void GlxRenderer::Destroy() {
pbos@webrtc.org29d58392013-05-16 12:08:03 +000092 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 Gerey665174f2018-06-19 15:03:05 +0200106GlxRenderer* GlxRenderer::Create(const char* window_title,
107 size_t width,
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000108 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.org29d58392013-05-16 12:08:03 +0000118void 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.org6998c8e2013-06-04 11:56:06 +0000138
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.org29d58392013-05-16 12:08:03 +0000143}
144
nisseeb83a1a2016-03-21 01:27:56 -0700145void GlxRenderer::OnFrame(const webrtc::VideoFrame& frame) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000146 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
nisseeb83a1a2016-03-21 01:27:56 -0700168 GlRenderer::OnFrame(frame);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000169 glXSwapBuffers(display_, window_);
170
171 if (!glXMakeCurrent(display_, None, NULL)) {
172 abort();
173 }
174}
Yves Gerey665174f2018-06-19 15:03:05 +0200175} // namespace test
176} // namespace webrtc