1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2013 Intel Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21bf215546Sopenharmony_ci * DEALINGS IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci#include "glxclient.h"
27bf215546Sopenharmony_ci#include "glx_error.h"
28bf215546Sopenharmony_ci#include "GL/internal/dri_interface.h"
29bf215546Sopenharmony_ci#include "dri2_priv.h"
30bf215546Sopenharmony_ci#if defined(HAVE_DRI3)
31bf215546Sopenharmony_ci#include "dri3_priv.h"
32bf215546Sopenharmony_ci#endif
33bf215546Sopenharmony_ci#include "drisw_priv.h"
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci#define __RENDERER(attrib) \
36bf215546Sopenharmony_ci    { GLX_RENDERER_##attrib##_MESA, __DRI2_RENDERER_##attrib }
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_cistatic const struct {
39bf215546Sopenharmony_ci   unsigned int glx_attrib, dri2_attrib;
40bf215546Sopenharmony_ci} query_renderer_map[] = {
41bf215546Sopenharmony_ci  __RENDERER(VENDOR_ID),
42bf215546Sopenharmony_ci  __RENDERER(DEVICE_ID),
43bf215546Sopenharmony_ci  __RENDERER(VERSION),
44bf215546Sopenharmony_ci  __RENDERER(ACCELERATED),
45bf215546Sopenharmony_ci  __RENDERER(VIDEO_MEMORY),
46bf215546Sopenharmony_ci  __RENDERER(UNIFIED_MEMORY_ARCHITECTURE),
47bf215546Sopenharmony_ci  __RENDERER(PREFERRED_PROFILE),
48bf215546Sopenharmony_ci  __RENDERER(OPENGL_CORE_PROFILE_VERSION),
49bf215546Sopenharmony_ci  __RENDERER(OPENGL_COMPATIBILITY_PROFILE_VERSION),
50bf215546Sopenharmony_ci  __RENDERER(OPENGL_ES_PROFILE_VERSION),
51bf215546Sopenharmony_ci  __RENDERER(OPENGL_ES2_PROFILE_VERSION),
52bf215546Sopenharmony_ci};
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_ci#undef __RENDERER
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_cistatic int
57bf215546Sopenharmony_cidri2_convert_glx_query_renderer_attribs(int attribute)
58bf215546Sopenharmony_ci{
59bf215546Sopenharmony_ci   unsigned i;
60bf215546Sopenharmony_ci
61bf215546Sopenharmony_ci   for (i = 0; i < ARRAY_SIZE(query_renderer_map); i++)
62bf215546Sopenharmony_ci      if (query_renderer_map[i].glx_attrib == attribute)
63bf215546Sopenharmony_ci         return query_renderer_map[i].dri2_attrib;
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_ci   return -1;
66bf215546Sopenharmony_ci}
67bf215546Sopenharmony_ci
68bf215546Sopenharmony_ci/* Convert internal dri context profile bits into GLX context profile bits */
69bf215546Sopenharmony_cistatic inline void
70bf215546Sopenharmony_cidri_convert_context_profile_bits(int attribute, unsigned int *value)
71bf215546Sopenharmony_ci{
72bf215546Sopenharmony_ci   if (attribute == GLX_RENDERER_PREFERRED_PROFILE_MESA) {
73bf215546Sopenharmony_ci      if (value[0] == (1U << __DRI_API_OPENGL_CORE))
74bf215546Sopenharmony_ci         value[0] = GLX_CONTEXT_CORE_PROFILE_BIT_ARB;
75bf215546Sopenharmony_ci      else if (value[0] == (1U << __DRI_API_OPENGL))
76bf215546Sopenharmony_ci         value[0] = GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB;
77bf215546Sopenharmony_ci   }
78bf215546Sopenharmony_ci}
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ci_X_HIDDEN int
81bf215546Sopenharmony_cidri2_query_renderer_integer(struct glx_screen *base, int attribute,
82bf215546Sopenharmony_ci                            unsigned int *value)
83bf215546Sopenharmony_ci{
84bf215546Sopenharmony_ci   int ret;
85bf215546Sopenharmony_ci   struct dri2_screen *const psc = (struct dri2_screen *) base;
86bf215546Sopenharmony_ci
87bf215546Sopenharmony_ci   /* Even though there are invalid values (and
88bf215546Sopenharmony_ci    * dri2_convert_glx_query_renderer_attribs may return -1), the higher level
89bf215546Sopenharmony_ci    * GLX code is required to perform the filtering.  Assume that we got a
90bf215546Sopenharmony_ci    * good value.
91bf215546Sopenharmony_ci    */
92bf215546Sopenharmony_ci   const int dri_attribute = dri2_convert_glx_query_renderer_attribs(attribute);
93bf215546Sopenharmony_ci
94bf215546Sopenharmony_ci   if (psc->rendererQuery == NULL)
95bf215546Sopenharmony_ci      return -1;
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_ci   ret = psc->rendererQuery->queryInteger(psc->driScreen, dri_attribute,
98bf215546Sopenharmony_ci                                          value);
99bf215546Sopenharmony_ci   dri_convert_context_profile_bits(attribute, value);
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_ci   return ret;
102bf215546Sopenharmony_ci}
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_ci_X_HIDDEN int
105bf215546Sopenharmony_cidri2_query_renderer_string(struct glx_screen *base, int attribute,
106bf215546Sopenharmony_ci                           const char **value)
107bf215546Sopenharmony_ci{
108bf215546Sopenharmony_ci   struct dri2_screen *const psc = (struct dri2_screen *) base;
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_ci   /* Even though queryString only accepts a subset of the possible GLX
111bf215546Sopenharmony_ci    * queries, the higher level GLX code is required to perform the filtering.
112bf215546Sopenharmony_ci    * Assume that we got a good value.
113bf215546Sopenharmony_ci    */
114bf215546Sopenharmony_ci   const int dri_attribute = dri2_convert_glx_query_renderer_attribs(attribute);
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_ci   if (psc->rendererQuery == NULL)
117bf215546Sopenharmony_ci      return -1;
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_ci   return psc->rendererQuery->queryString(psc->driScreen, dri_attribute, value);
120bf215546Sopenharmony_ci}
121bf215546Sopenharmony_ci
122bf215546Sopenharmony_ci#if defined(HAVE_DRI3)
123bf215546Sopenharmony_ci_X_HIDDEN int
124bf215546Sopenharmony_cidri3_query_renderer_integer(struct glx_screen *base, int attribute,
125bf215546Sopenharmony_ci                            unsigned int *value)
126bf215546Sopenharmony_ci{
127bf215546Sopenharmony_ci   int ret;
128bf215546Sopenharmony_ci   struct dri3_screen *const psc = (struct dri3_screen *) base;
129bf215546Sopenharmony_ci
130bf215546Sopenharmony_ci   /* Even though there are invalid values (and
131bf215546Sopenharmony_ci    * dri2_convert_glx_query_renderer_attribs may return -1), the higher level
132bf215546Sopenharmony_ci    * GLX code is required to perform the filtering.  Assume that we got a
133bf215546Sopenharmony_ci    * good value.
134bf215546Sopenharmony_ci    */
135bf215546Sopenharmony_ci   const int dri_attribute = dri2_convert_glx_query_renderer_attribs(attribute);
136bf215546Sopenharmony_ci
137bf215546Sopenharmony_ci   if (psc->rendererQuery == NULL)
138bf215546Sopenharmony_ci      return -1;
139bf215546Sopenharmony_ci
140bf215546Sopenharmony_ci   ret = psc->rendererQuery->queryInteger(psc->driScreen, dri_attribute,
141bf215546Sopenharmony_ci                                          value);
142bf215546Sopenharmony_ci   dri_convert_context_profile_bits(attribute, value);
143bf215546Sopenharmony_ci
144bf215546Sopenharmony_ci   return ret;
145bf215546Sopenharmony_ci}
146bf215546Sopenharmony_ci
147bf215546Sopenharmony_ci_X_HIDDEN int
148bf215546Sopenharmony_cidri3_query_renderer_string(struct glx_screen *base, int attribute,
149bf215546Sopenharmony_ci                           const char **value)
150bf215546Sopenharmony_ci{
151bf215546Sopenharmony_ci   struct dri3_screen *const psc = (struct dri3_screen *) base;
152bf215546Sopenharmony_ci
153bf215546Sopenharmony_ci   /* Even though queryString only accepts a subset of the possible GLX
154bf215546Sopenharmony_ci    * queries, the higher level GLX code is required to perform the filtering.
155bf215546Sopenharmony_ci    * Assume that we got a good value.
156bf215546Sopenharmony_ci    */
157bf215546Sopenharmony_ci   const int dri_attribute = dri2_convert_glx_query_renderer_attribs(attribute);
158bf215546Sopenharmony_ci
159bf215546Sopenharmony_ci   if (psc->rendererQuery == NULL)
160bf215546Sopenharmony_ci      return -1;
161bf215546Sopenharmony_ci
162bf215546Sopenharmony_ci   return psc->rendererQuery->queryString(psc->driScreen, dri_attribute, value);
163bf215546Sopenharmony_ci}
164bf215546Sopenharmony_ci#endif /* HAVE_DRI3 */
165bf215546Sopenharmony_ci
166bf215546Sopenharmony_ci_X_HIDDEN int
167bf215546Sopenharmony_cidrisw_query_renderer_integer(struct glx_screen *base, int attribute,
168bf215546Sopenharmony_ci                             unsigned int *value)
169bf215546Sopenharmony_ci{
170bf215546Sopenharmony_ci   int ret;
171bf215546Sopenharmony_ci   struct drisw_screen *const psc = (struct drisw_screen *) base;
172bf215546Sopenharmony_ci
173bf215546Sopenharmony_ci   /* Even though there are invalid values (and
174bf215546Sopenharmony_ci    * dri2_convert_glx_query_renderer_attribs may return -1), the higher level
175bf215546Sopenharmony_ci    * GLX code is required to perform the filtering.  Assume that we got a
176bf215546Sopenharmony_ci    * good value.
177bf215546Sopenharmony_ci    */
178bf215546Sopenharmony_ci   const int dri_attribute = dri2_convert_glx_query_renderer_attribs(attribute);
179bf215546Sopenharmony_ci
180bf215546Sopenharmony_ci   if (psc->rendererQuery == NULL)
181bf215546Sopenharmony_ci      return -1;
182bf215546Sopenharmony_ci
183bf215546Sopenharmony_ci   ret = psc->rendererQuery->queryInteger(psc->driScreen, dri_attribute,
184bf215546Sopenharmony_ci                                          value);
185bf215546Sopenharmony_ci   dri_convert_context_profile_bits(attribute, value);
186bf215546Sopenharmony_ci
187bf215546Sopenharmony_ci   return ret;
188bf215546Sopenharmony_ci}
189bf215546Sopenharmony_ci
190bf215546Sopenharmony_ci_X_HIDDEN int
191bf215546Sopenharmony_cidrisw_query_renderer_string(struct glx_screen *base, int attribute,
192bf215546Sopenharmony_ci                            const char **value)
193bf215546Sopenharmony_ci{
194bf215546Sopenharmony_ci   struct drisw_screen *const psc = (struct drisw_screen *) base;
195bf215546Sopenharmony_ci
196bf215546Sopenharmony_ci   /* Even though queryString only accepts a subset of the possible GLX
197bf215546Sopenharmony_ci    * queries, the higher level GLX code is required to perform the filtering.
198bf215546Sopenharmony_ci    * Assume that we got a good value.
199bf215546Sopenharmony_ci    */
200bf215546Sopenharmony_ci   const int dri_attribute = dri2_convert_glx_query_renderer_attribs(attribute);
201bf215546Sopenharmony_ci
202bf215546Sopenharmony_ci   if (psc->rendererQuery == NULL)
203bf215546Sopenharmony_ci      return -1;
204bf215546Sopenharmony_ci
205bf215546Sopenharmony_ci   return psc->rendererQuery->queryString(psc->driScreen, dri_attribute, value);
206bf215546Sopenharmony_ci}
207bf215546Sopenharmony_ci
208bf215546Sopenharmony_ci
209bf215546Sopenharmony_ci#endif /* GLX_DIRECT_RENDERING */
210