1bf215546Sopenharmony_ci#include "dri_query_renderer.h"
2bf215546Sopenharmony_ci
3bf215546Sopenharmony_ci#include "util/u_inlines.h"
4bf215546Sopenharmony_ci#include "frontend/drm_driver.h"
5bf215546Sopenharmony_ci
6bf215546Sopenharmony_ci#include "dri_screen.h"
7bf215546Sopenharmony_ci#include "dri_query_renderer.h"
8bf215546Sopenharmony_ci#include "pipe-loader/pipe_loader.h"
9bf215546Sopenharmony_ci
10bf215546Sopenharmony_ci/**
11bf215546Sopenharmony_ci * Implement queries for values that are common across all Mesa drivers
12bf215546Sopenharmony_ci *
13bf215546Sopenharmony_ci * Currently only the following queries are supported by this function:
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci *     - \c __DRI2_RENDERER_VERSION
16bf215546Sopenharmony_ci *     - \c __DRI2_RENDERER_PREFERRED_PROFILE
17bf215546Sopenharmony_ci *     - \c __DRI2_RENDERER_OPENGL_CORE_PROFILE_VERSION
18bf215546Sopenharmony_ci *     - \c __DRI2_RENDERER_OPENGL_COMPATIBLITY_PROFILE_VERSION
19bf215546Sopenharmony_ci *     - \c __DRI2_RENDERER_ES_PROFILE_VERSION
20bf215546Sopenharmony_ci *     - \c __DRI2_RENDERER_ES2_PROFILE_VERSION
21bf215546Sopenharmony_ci *     - \c __DRI2_RENDERER_HAS_NO_ERROR_CONTEXT
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci * \returns
24bf215546Sopenharmony_ci * Zero if a recognized value of \c param is supplied, -1 otherwise.
25bf215546Sopenharmony_ci */
26bf215546Sopenharmony_cistatic int
27bf215546Sopenharmony_cidriQueryRendererIntegerCommon(__DRIscreen *psp, int param, unsigned int *value)
28bf215546Sopenharmony_ci{
29bf215546Sopenharmony_ci   switch (param) {
30bf215546Sopenharmony_ci   case __DRI2_RENDERER_VERSION: {
31bf215546Sopenharmony_ci      static const char *const ver = PACKAGE_VERSION;
32bf215546Sopenharmony_ci      char *endptr;
33bf215546Sopenharmony_ci      int v[3];
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci      v[0] = strtol(ver, &endptr, 10);
36bf215546Sopenharmony_ci      assert(endptr[0] == '.');
37bf215546Sopenharmony_ci      if (endptr[0] != '.')
38bf215546Sopenharmony_ci         return -1;
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_ci      v[1] = strtol(endptr + 1, &endptr, 10);
41bf215546Sopenharmony_ci      assert(endptr[0] == '.');
42bf215546Sopenharmony_ci      if (endptr[0] != '.')
43bf215546Sopenharmony_ci         return -1;
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_ci      v[2] = strtol(endptr + 1, &endptr, 10);
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_ci      value[0] = v[0];
48bf215546Sopenharmony_ci      value[1] = v[1];
49bf215546Sopenharmony_ci      value[2] = v[2];
50bf215546Sopenharmony_ci      return 0;
51bf215546Sopenharmony_ci   }
52bf215546Sopenharmony_ci   case __DRI2_RENDERER_PREFERRED_PROFILE:
53bf215546Sopenharmony_ci      value[0] = (psp->max_gl_core_version != 0)
54bf215546Sopenharmony_ci         ? (1U << __DRI_API_OPENGL_CORE) : (1U << __DRI_API_OPENGL);
55bf215546Sopenharmony_ci      return 0;
56bf215546Sopenharmony_ci   case __DRI2_RENDERER_OPENGL_CORE_PROFILE_VERSION:
57bf215546Sopenharmony_ci      value[0] = psp->max_gl_core_version / 10;
58bf215546Sopenharmony_ci      value[1] = psp->max_gl_core_version % 10;
59bf215546Sopenharmony_ci      return 0;
60bf215546Sopenharmony_ci   case __DRI2_RENDERER_OPENGL_COMPATIBILITY_PROFILE_VERSION:
61bf215546Sopenharmony_ci      value[0] = psp->max_gl_compat_version / 10;
62bf215546Sopenharmony_ci      value[1] = psp->max_gl_compat_version % 10;
63bf215546Sopenharmony_ci      return 0;
64bf215546Sopenharmony_ci   case __DRI2_RENDERER_OPENGL_ES_PROFILE_VERSION:
65bf215546Sopenharmony_ci      value[0] = psp->max_gl_es1_version / 10;
66bf215546Sopenharmony_ci      value[1] = psp->max_gl_es1_version % 10;
67bf215546Sopenharmony_ci      return 0;
68bf215546Sopenharmony_ci   case __DRI2_RENDERER_OPENGL_ES2_PROFILE_VERSION:
69bf215546Sopenharmony_ci      value[0] = psp->max_gl_es2_version / 10;
70bf215546Sopenharmony_ci      value[1] = psp->max_gl_es2_version % 10;
71bf215546Sopenharmony_ci      return 0;
72bf215546Sopenharmony_ci   case __DRI2_RENDERER_HAS_NO_ERROR_CONTEXT:
73bf215546Sopenharmony_ci      value[0] = GL_TRUE;
74bf215546Sopenharmony_ci      return 0;
75bf215546Sopenharmony_ci   default:
76bf215546Sopenharmony_ci      break;
77bf215546Sopenharmony_ci   }
78bf215546Sopenharmony_ci
79bf215546Sopenharmony_ci   return -1;
80bf215546Sopenharmony_ci}
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_cistatic int
83bf215546Sopenharmony_cidri2_query_renderer_integer(__DRIscreen *_screen, int param,
84bf215546Sopenharmony_ci                            unsigned int *value)
85bf215546Sopenharmony_ci{
86bf215546Sopenharmony_ci   struct dri_screen *screen = dri_screen(_screen);
87bf215546Sopenharmony_ci
88bf215546Sopenharmony_ci   switch (param) {
89bf215546Sopenharmony_ci   case __DRI2_RENDERER_VENDOR_ID:
90bf215546Sopenharmony_ci      value[0] =
91bf215546Sopenharmony_ci         (unsigned int)screen->base.screen->get_param(screen->base.screen,
92bf215546Sopenharmony_ci                                                      PIPE_CAP_VENDOR_ID);
93bf215546Sopenharmony_ci      return 0;
94bf215546Sopenharmony_ci   case __DRI2_RENDERER_DEVICE_ID:
95bf215546Sopenharmony_ci      value[0] =
96bf215546Sopenharmony_ci         (unsigned int)screen->base.screen->get_param(screen->base.screen,
97bf215546Sopenharmony_ci                                                      PIPE_CAP_DEVICE_ID);
98bf215546Sopenharmony_ci      return 0;
99bf215546Sopenharmony_ci   case __DRI2_RENDERER_ACCELERATED:
100bf215546Sopenharmony_ci      value[0] =
101bf215546Sopenharmony_ci         (unsigned int)!!screen->base.screen->get_param(screen->base.screen,
102bf215546Sopenharmony_ci                                                        PIPE_CAP_ACCELERATED);
103bf215546Sopenharmony_ci      return 0;
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ci   case __DRI2_RENDERER_VIDEO_MEMORY: {
106bf215546Sopenharmony_ci      int ov = driQueryOptioni(&screen->dev->option_cache, "override_vram_size");
107bf215546Sopenharmony_ci      value[0] =
108bf215546Sopenharmony_ci         (unsigned int)screen->base.screen->get_param(screen->base.screen,
109bf215546Sopenharmony_ci                                                      PIPE_CAP_VIDEO_MEMORY);
110bf215546Sopenharmony_ci      if (ov >= 0)
111bf215546Sopenharmony_ci         value[0] = MIN2(ov, value[0]);
112bf215546Sopenharmony_ci      return 0;
113bf215546Sopenharmony_ci   }
114bf215546Sopenharmony_ci
115bf215546Sopenharmony_ci   case __DRI2_RENDERER_UNIFIED_MEMORY_ARCHITECTURE:
116bf215546Sopenharmony_ci      value[0] =
117bf215546Sopenharmony_ci         (unsigned int)screen->base.screen->get_param(screen->base.screen,
118bf215546Sopenharmony_ci                                                      PIPE_CAP_UMA);
119bf215546Sopenharmony_ci      return 0;
120bf215546Sopenharmony_ci
121bf215546Sopenharmony_ci   case __DRI2_RENDERER_HAS_TEXTURE_3D:
122bf215546Sopenharmony_ci      value[0] =
123bf215546Sopenharmony_ci         screen->base.screen->get_param(screen->base.screen,
124bf215546Sopenharmony_ci                                        PIPE_CAP_MAX_TEXTURE_3D_LEVELS) != 0;
125bf215546Sopenharmony_ci      return 0;
126bf215546Sopenharmony_ci
127bf215546Sopenharmony_ci   case __DRI2_RENDERER_HAS_FRAMEBUFFER_SRGB:
128bf215546Sopenharmony_ci      value[0] =
129bf215546Sopenharmony_ci         screen->base.screen->is_format_supported(screen->base.screen,
130bf215546Sopenharmony_ci                                                  PIPE_FORMAT_B8G8R8A8_SRGB,
131bf215546Sopenharmony_ci                                                  PIPE_TEXTURE_2D, 0, 0,
132bf215546Sopenharmony_ci                                                  PIPE_BIND_RENDER_TARGET);
133bf215546Sopenharmony_ci      return 0;
134bf215546Sopenharmony_ci   case __DRI2_RENDERER_HAS_CONTEXT_PRIORITY:
135bf215546Sopenharmony_ci      value[0] =
136bf215546Sopenharmony_ci         screen->base.screen->get_param(screen->base.screen,
137bf215546Sopenharmony_ci                                        PIPE_CAP_CONTEXT_PRIORITY_MASK);
138bf215546Sopenharmony_ci      if (!value[0])
139bf215546Sopenharmony_ci         return -1;
140bf215546Sopenharmony_ci      return 0;
141bf215546Sopenharmony_ci   case __DRI2_RENDERER_HAS_PROTECTED_CONTENT:
142bf215546Sopenharmony_ci      value[0] =
143bf215546Sopenharmony_ci         screen->base.screen->get_param(screen->base.screen,
144bf215546Sopenharmony_ci                                        PIPE_CAP_DEVICE_PROTECTED_CONTENT);
145bf215546Sopenharmony_ci      if (!value[0])
146bf215546Sopenharmony_ci         return -1;
147bf215546Sopenharmony_ci      return 0;
148bf215546Sopenharmony_ci   case __DRI2_RENDERER_PREFER_BACK_BUFFER_REUSE:
149bf215546Sopenharmony_ci      value[0] =
150bf215546Sopenharmony_ci         screen->base.screen->get_param(screen->base.screen,
151bf215546Sopenharmony_ci                                        PIPE_CAP_PREFER_BACK_BUFFER_REUSE);
152bf215546Sopenharmony_ci      return 0;
153bf215546Sopenharmony_ci   default:
154bf215546Sopenharmony_ci      return driQueryRendererIntegerCommon(_screen, param, value);
155bf215546Sopenharmony_ci   }
156bf215546Sopenharmony_ci}
157bf215546Sopenharmony_ci
158bf215546Sopenharmony_cistatic int
159bf215546Sopenharmony_cidri2_query_renderer_string(__DRIscreen *_screen, int param,
160bf215546Sopenharmony_ci                           const char **value)
161bf215546Sopenharmony_ci{
162bf215546Sopenharmony_ci   struct dri_screen *screen = dri_screen(_screen);
163bf215546Sopenharmony_ci
164bf215546Sopenharmony_ci   switch (param) {
165bf215546Sopenharmony_ci   case __DRI2_RENDERER_VENDOR_ID:
166bf215546Sopenharmony_ci      value[0] = screen->base.screen->get_vendor(screen->base.screen);
167bf215546Sopenharmony_ci      return 0;
168bf215546Sopenharmony_ci   case __DRI2_RENDERER_DEVICE_ID:
169bf215546Sopenharmony_ci      value[0] = screen->base.screen->get_name(screen->base.screen);
170bf215546Sopenharmony_ci      return 0;
171bf215546Sopenharmony_ci   default:
172bf215546Sopenharmony_ci      return -1;
173bf215546Sopenharmony_ci   }
174bf215546Sopenharmony_ci}
175bf215546Sopenharmony_ci
176bf215546Sopenharmony_ciconst __DRI2rendererQueryExtension dri2RendererQueryExtension = {
177bf215546Sopenharmony_ci    .base = { __DRI2_RENDERER_QUERY, 1 },
178bf215546Sopenharmony_ci
179bf215546Sopenharmony_ci    .queryInteger         = dri2_query_renderer_integer,
180bf215546Sopenharmony_ci    .queryString          = dri2_query_renderer_string
181bf215546Sopenharmony_ci};
182