blob: d6979c8a58f2b9c7308b92c0e50cec0f8502e87e [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
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000011#include "webrtc/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
pbos@webrtc.org29d58392013-05-16 12:08:03 +000019#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
20
21namespace webrtc {
22namespace test {
23
24GlxRenderer::GlxRenderer(size_t width, size_t height)
pbos@webrtc.org28556f52013-05-24 10:54:56 +000025 : width_(width),
pbos@webrtc.org29d58392013-05-16 12:08:03 +000026 height_(height),
27 display_(NULL),
28 context_(NULL) {
29 assert(width > 0);
30 assert(height > 0);
31}
32
pbos@webrtc.org28556f52013-05-24 10:54:56 +000033GlxRenderer::~GlxRenderer() { Destroy(); }
34
pbos@webrtc.org29d58392013-05-16 12:08:03 +000035bool 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
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
109GlxRenderer* 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.org29d58392013-05-16 12:08:03 +0000120void 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.org6998c8e2013-06-04 11:56:06 +0000140
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.org29d58392013-05-16 12:08:03 +0000145}
146
nisseeb83a1a2016-03-21 01:27:56 -0700147void GlxRenderer::OnFrame(const webrtc::VideoFrame& frame) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000148 if (static_cast<size_t>(frame.width()) != width_ ||
149 static_cast<size_t>(frame.height()) != height_) {
150 Resize(static_cast<size_t>(frame.width()),
151 static_cast<size_t>(frame.height()));
152 }
153
154 XEvent event;
155 if (!glXMakeCurrent(display_, window_, context_)) {
156 abort();
157 }
158 while (XPending(display_)) {
159 XNextEvent(display_, &event);
160 switch (event.type) {
161 case ConfigureNotify:
162 GlRenderer::ResizeViewport(event.xconfigure.width,
163 event.xconfigure.height);
164 break;
165 default:
166 break;
167 }
168 }
169
nisseeb83a1a2016-03-21 01:27:56 -0700170 GlRenderer::OnFrame(frame);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000171 glXSwapBuffers(display_, window_);
172
173 if (!glXMakeCurrent(display_, None, NULL)) {
174 abort();
175 }
176}
177} // test
178} // webrtc