1 /**************************************************************************
2  *
3  * Copyright (C) 2014 Red Hat Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included
13  * in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  *
23  **************************************************************************/
24 
25 #include "vrend_winsys.h"
26 
27 #ifdef HAVE_EPOXY_GLX_H
28 #include "vrend_winsys_glx.h"
29 #endif
30 
31 #include <stddef.h>
32 
33 enum {
34    CONTEXT_NONE,
35    CONTEXT_EGL,
36    CONTEXT_GLX
37 };
38 
39 static int use_context = CONTEXT_NONE;
40 
41 #ifdef HAVE_EPOXY_EGL_H
42 struct virgl_egl *egl = NULL;
43 struct virgl_gbm *gbm = NULL;
44 #endif
45 
46 #ifdef HAVE_EPOXY_GLX_H
47 static struct virgl_glx *glx_info = NULL;
48 #endif
49 
vrend_winsys_init(uint32_t flags,int preferred_fd)50 int vrend_winsys_init(uint32_t flags, int preferred_fd)
51 {
52    if (flags & VIRGL_RENDERER_USE_EGL) {
53 #ifdef HAVE_EPOXY_EGL_H
54       /*
55        * If the user specifies a preferred DRM fd and we can't use it, fail. If the user doesn't
56        * specify an fd, it's possible to initialize EGL without one.
57        */
58       gbm = virgl_gbm_init(preferred_fd);
59       if (preferred_fd > 0 && !gbm)
60          return -1;
61 
62       egl = virgl_egl_init(gbm, flags & VIRGL_RENDERER_USE_SURFACELESS,
63                            flags & VIRGL_RENDERER_USE_GLES);
64       if (!egl) {
65          if (gbm) {
66             virgl_gbm_fini(gbm);
67             gbm = NULL;
68          }
69 
70          return -1;
71       }
72 
73       use_context = CONTEXT_EGL;
74 #else
75       vrend_printf( "EGL is not supported on this platform\n");
76       return -1;
77 #endif
78    } else if (flags & VIRGL_RENDERER_USE_GLX) {
79 #ifdef HAVE_EPOXY_GLX_H
80       glx_info = virgl_glx_init();
81       if (!glx_info)
82          return -1;
83       use_context = CONTEXT_GLX;
84 #else
85       vrend_printf( "GLX is not supported on this platform\n");
86       return -1;
87 #endif
88    }
89 
90    return 0;
91 }
92 
vrend_winsys_cleanup(void)93 void vrend_winsys_cleanup(void)
94 {
95 #ifdef HAVE_EPOXY_EGL_H
96    if (use_context == CONTEXT_EGL) {
97       virgl_egl_destroy(egl);
98       egl = NULL;
99       use_context = CONTEXT_NONE;
100       if (gbm) {
101          virgl_gbm_fini(gbm);
102          gbm = NULL;
103       }
104    }
105 #endif
106 #ifdef HAVE_EPOXY_GLX_H
107    if (use_context == CONTEXT_GLX) {
108       virgl_glx_destroy(glx_info);
109       glx_info = NULL;
110       use_context = CONTEXT_NONE;
111    }
112 #endif
113 }
114 
vrend_winsys_create_context(struct virgl_gl_ctx_param * param)115 virgl_renderer_gl_context vrend_winsys_create_context(struct virgl_gl_ctx_param *param)
116 {
117 #ifdef HAVE_EPOXY_EGL_H
118    if (use_context == CONTEXT_EGL)
119       return virgl_egl_create_context(egl, param);
120 #endif
121 #ifdef HAVE_EPOXY_GLX_H
122    if (use_context == CONTEXT_GLX)
123       return virgl_glx_create_context(glx_info, param);
124 #endif
125    return NULL;
126 }
127 
vrend_winsys_destroy_context(virgl_renderer_gl_context ctx)128 void vrend_winsys_destroy_context(virgl_renderer_gl_context ctx)
129 {
130 #ifdef HAVE_EPOXY_EGL_H
131    if (use_context == CONTEXT_EGL) {
132       virgl_egl_destroy_context(egl, ctx);
133       return;
134    }
135 #endif
136 #ifdef HAVE_EPOXY_GLX_H
137    if (use_context == CONTEXT_GLX) {
138       virgl_glx_destroy_context(glx_info, ctx);
139       return;
140    }
141 #endif
142 }
143 
vrend_winsys_make_context_current(virgl_renderer_gl_context ctx)144 int vrend_winsys_make_context_current(virgl_renderer_gl_context ctx)
145 {
146 #ifdef HAVE_EPOXY_EGL_H
147    if (use_context == CONTEXT_EGL)
148       return virgl_egl_make_context_current(egl, ctx);
149 #endif
150 #ifdef HAVE_EPOXY_GLX_H
151    if (use_context == CONTEXT_GLX)
152       return virgl_glx_make_context_current(glx_info, ctx);
153 #endif
154    return -1;
155 }
156 
vrend_winsys_has_gl_colorspace(void)157 int vrend_winsys_has_gl_colorspace(void)
158 {
159    bool egl_colorspace = false;
160 #ifdef HAVE_EPOXY_EGL_H
161    if (egl)
162       egl_colorspace = virgl_has_egl_khr_gl_colorspace(egl);
163 #endif
164    return use_context == CONTEXT_NONE ||
165          use_context == CONTEXT_GLX ||
166          (use_context == CONTEXT_EGL && egl_colorspace);
167 }
168 
vrend_winsys_get_fourcc_for_texture(uint32_t tex_id,uint32_t format,int * fourcc)169 int vrend_winsys_get_fourcc_for_texture(uint32_t tex_id, uint32_t format, int *fourcc)
170 {
171 #ifdef HAVE_EPOXY_EGL_H
172    if (use_context == CONTEXT_EGL)
173       return virgl_egl_get_fourcc_for_texture(egl, tex_id, format, fourcc);
174 #endif
175    return 0;
176 }
177 
vrend_winsys_get_fd_for_texture(uint32_t tex_id,int * fd)178 int vrend_winsys_get_fd_for_texture(uint32_t tex_id, int *fd)
179 {
180 #ifdef HAVE_EPOXY_EGL_H
181    if (!egl)
182       return -1;
183 
184    return virgl_egl_get_fd_for_texture(egl, tex_id, fd);
185 #else
186    return -1;
187 #endif
188 }
189 
vrend_winsys_get_fd_for_texture2(uint32_t tex_id,int * fd,int * stride,int * offset)190 int vrend_winsys_get_fd_for_texture2(uint32_t tex_id, int *fd, int *stride, int *offset)
191 {
192 #ifdef HAVE_EPOXY_EGL_H
193    if (!egl)
194       return -1;
195 
196    return virgl_egl_get_fd_for_texture2(egl, tex_id, fd, stride, offset);
197 #else
198    return -1;
199 #endif
200 }
201 
vrend_winsys_query_video_memory(void)202 uint32_t vrend_winsys_query_video_memory(void)
203 {
204 #ifdef HAVE_EPOXY_GLX_H
205    return virgl_glx_query_video_memory(glx_info);
206 #else
207    return 0;
208 #endif
209 }