gallium: change comments to remove 'state tracker'
[mesa.git] / src / gallium / auxiliary / hud / hud_context.c
1 /**************************************************************************
2 *
3 * Copyright 2013 Marek Olšák <maraeo@gmail.com>
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL THE AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /* This head-up display module can draw transparent graphs on top of what
29 * the app is rendering, visualizing various data like framerate, cpu load,
30 * performance counters, etc. It can be hook up into any gallium frontend.
31 *
32 * The HUD is controlled with the GALLIUM_HUD environment variable.
33 * Set GALLIUM_HUD=help for more info.
34 */
35
36 #include <inttypes.h>
37 #include <signal.h>
38 #include <stdio.h>
39
40 #include "hud/hud_context.h"
41 #include "hud/hud_private.h"
42
43 #include "cso_cache/cso_context.h"
44 #include "util/u_draw_quad.h"
45 #include "util/format/u_format.h"
46 #include "util/u_inlines.h"
47 #include "util/u_memory.h"
48 #include "util/u_math.h"
49 #include "util/u_sampler.h"
50 #include "util/u_simple_shaders.h"
51 #include "util/u_string.h"
52 #include "util/u_upload_mgr.h"
53 #include "tgsi/tgsi_text.h"
54 #include "tgsi/tgsi_dump.h"
55
56 /* Control the visibility of all HUD contexts */
57 static boolean huds_visible = TRUE;
58 static int hud_scale = 1;
59
60
61 #ifdef PIPE_OS_UNIX
62 static void
63 signal_visible_handler(int sig, siginfo_t *siginfo, void *context)
64 {
65 huds_visible = !huds_visible;
66 }
67 #endif
68
69 static void
70 hud_draw_colored_prims(struct hud_context *hud, unsigned prim,
71 float *buffer, unsigned num_vertices,
72 float r, float g, float b, float a,
73 int xoffset, int yoffset, float yscale)
74 {
75 struct cso_context *cso = hud->cso;
76 unsigned size = num_vertices * hud->color_prims.vbuf.stride;
77
78 /* If a recording context is inactive, don't draw anything. */
79 if (size > hud->color_prims.buffer_size)
80 return;
81
82 memcpy(hud->color_prims.vertices, buffer, size);
83
84 hud->constants.color[0] = r;
85 hud->constants.color[1] = g;
86 hud->constants.color[2] = b;
87 hud->constants.color[3] = a;
88 hud->constants.translate[0] = (float) (xoffset * hud_scale);
89 hud->constants.translate[1] = (float) (yoffset * hud_scale);
90 hud->constants.scale[0] = hud_scale;
91 hud->constants.scale[1] = yscale * hud_scale;
92 cso_set_constant_buffer(cso, PIPE_SHADER_VERTEX, 0, &hud->constbuf);
93
94 cso_set_vertex_buffers(cso, 0, 1, &hud->color_prims.vbuf);
95 cso_set_fragment_shader_handle(hud->cso, hud->fs_color);
96 cso_draw_arrays(cso, prim, 0, num_vertices);
97
98 hud->color_prims.vertices += size / sizeof(float);
99 hud->color_prims.vbuf.buffer_offset += size;
100 hud->color_prims.buffer_size -= size;
101 }
102
103 static void
104 hud_draw_colored_quad(struct hud_context *hud, unsigned prim,
105 unsigned x1, unsigned y1, unsigned x2, unsigned y2,
106 float r, float g, float b, float a)
107 {
108 float buffer[] = {
109 (float) x1, (float) y1,
110 (float) x1, (float) y2,
111 (float) x2, (float) y2,
112 (float) x2, (float) y1,
113 };
114
115 hud_draw_colored_prims(hud, prim, buffer, 4, r, g, b, a, 0, 0, 1);
116 }
117
118 static void
119 hud_draw_background_quad(struct hud_context *hud,
120 unsigned x1, unsigned y1, unsigned x2, unsigned y2)
121 {
122 float *vertices = hud->bg.vertices + hud->bg.num_vertices*2;
123 unsigned num = 0;
124
125 assert(hud->bg.num_vertices + 4 <= hud->bg.max_num_vertices);
126
127 vertices[num++] = (float) x1;
128 vertices[num++] = (float) y1;
129
130 vertices[num++] = (float) x1;
131 vertices[num++] = (float) y2;
132
133 vertices[num++] = (float) x2;
134 vertices[num++] = (float) y2;
135
136 vertices[num++] = (float) x2;
137 vertices[num++] = (float) y1;
138
139 hud->bg.num_vertices += num/2;
140 }
141
142 static void
143 hud_draw_string(struct hud_context *hud, unsigned x, unsigned y,
144 const char *str, ...)
145 {
146 char buf[256];
147 char *s = buf;
148 float *vertices = hud->text.vertices + hud->text.num_vertices*4;
149 unsigned num = 0;
150
151 va_list ap;
152 va_start(ap, str);
153 vsnprintf(buf, sizeof(buf), str, ap);
154 va_end(ap);
155
156 if (!*s)
157 return;
158
159 hud_draw_background_quad(hud,
160 x, y,
161 x + strlen(buf)*hud->font.glyph_width,
162 y + hud->font.glyph_height);
163
164 while (*s) {
165 unsigned x1 = x;
166 unsigned y1 = y;
167 unsigned x2 = x + hud->font.glyph_width;
168 unsigned y2 = y + hud->font.glyph_height;
169 unsigned tx1 = (*s % 16) * hud->font.glyph_width;
170 unsigned ty1 = (*s / 16) * hud->font.glyph_height;
171 unsigned tx2 = tx1 + hud->font.glyph_width;
172 unsigned ty2 = ty1 + hud->font.glyph_height;
173
174 if (*s == ' ') {
175 x += hud->font.glyph_width;
176 s++;
177 continue;
178 }
179
180 assert(hud->text.num_vertices + num/4 + 4 <= hud->text.max_num_vertices);
181
182 vertices[num++] = (float) x1;
183 vertices[num++] = (float) y1;
184 vertices[num++] = (float) tx1;
185 vertices[num++] = (float) ty1;
186
187 vertices[num++] = (float) x1;
188 vertices[num++] = (float) y2;
189 vertices[num++] = (float) tx1;
190 vertices[num++] = (float) ty2;
191
192 vertices[num++] = (float) x2;
193 vertices[num++] = (float) y2;
194 vertices[num++] = (float) tx2;
195 vertices[num++] = (float) ty2;
196
197 vertices[num++] = (float) x2;
198 vertices[num++] = (float) y1;
199 vertices[num++] = (float) tx2;
200 vertices[num++] = (float) ty1;
201
202 x += hud->font.glyph_width;
203 s++;
204 }
205
206 hud->text.num_vertices += num/4;
207 }
208
209 static void
210 number_to_human_readable(double num, enum pipe_driver_query_type type,
211 char *out)
212 {
213 static const char *byte_units[] =
214 {" B", " KB", " MB", " GB", " TB", " PB", " EB"};
215 static const char *metric_units[] =
216 {"", " k", " M", " G", " T", " P", " E"};
217 static const char *time_units[] =
218 {" us", " ms", " s"}; /* based on microseconds */
219 static const char *hz_units[] =
220 {" Hz", " KHz", " MHz", " GHz"};
221 static const char *percent_units[] = {"%"};
222 static const char *dbm_units[] = {" (-dBm)"};
223 static const char *temperature_units[] = {" C"};
224 static const char *volt_units[] = {" mV", " V"};
225 static const char *amp_units[] = {" mA", " A"};
226 static const char *watt_units[] = {" mW", " W"};
227 static const char *float_units[] = {""};
228
229 const char **units;
230 unsigned max_unit;
231 double divisor = (type == PIPE_DRIVER_QUERY_TYPE_BYTES) ? 1024 : 1000;
232 unsigned unit = 0;
233 double d = num;
234
235 switch (type) {
236 case PIPE_DRIVER_QUERY_TYPE_MICROSECONDS:
237 max_unit = ARRAY_SIZE(time_units)-1;
238 units = time_units;
239 break;
240 case PIPE_DRIVER_QUERY_TYPE_VOLTS:
241 max_unit = ARRAY_SIZE(volt_units)-1;
242 units = volt_units;
243 break;
244 case PIPE_DRIVER_QUERY_TYPE_AMPS:
245 max_unit = ARRAY_SIZE(amp_units)-1;
246 units = amp_units;
247 break;
248 case PIPE_DRIVER_QUERY_TYPE_DBM:
249 max_unit = ARRAY_SIZE(dbm_units)-1;
250 units = dbm_units;
251 break;
252 case PIPE_DRIVER_QUERY_TYPE_TEMPERATURE:
253 max_unit = ARRAY_SIZE(temperature_units)-1;
254 units = temperature_units;
255 break;
256 case PIPE_DRIVER_QUERY_TYPE_FLOAT:
257 max_unit = ARRAY_SIZE(float_units)-1;
258 units = float_units;
259 break;
260 case PIPE_DRIVER_QUERY_TYPE_PERCENTAGE:
261 max_unit = ARRAY_SIZE(percent_units)-1;
262 units = percent_units;
263 break;
264 case PIPE_DRIVER_QUERY_TYPE_BYTES:
265 max_unit = ARRAY_SIZE(byte_units)-1;
266 units = byte_units;
267 break;
268 case PIPE_DRIVER_QUERY_TYPE_HZ:
269 max_unit = ARRAY_SIZE(hz_units)-1;
270 units = hz_units;
271 break;
272 case PIPE_DRIVER_QUERY_TYPE_WATTS:
273 max_unit = ARRAY_SIZE(watt_units)-1;
274 units = watt_units;
275 break;
276 default:
277 max_unit = ARRAY_SIZE(metric_units)-1;
278 units = metric_units;
279 }
280
281 while (d > divisor && unit < max_unit) {
282 d /= divisor;
283 unit++;
284 }
285
286 /* Round to 3 decimal places so as not to print trailing zeros. */
287 if (d*1000 != (int)(d*1000))
288 d = round(d * 1000) / 1000;
289
290 /* Show at least 4 digits with at most 3 decimal places, but not zeros. */
291 if (d >= 1000 || d == (int)d)
292 sprintf(out, "%.0f%s", d, units[unit]);
293 else if (d >= 100 || d*10 == (int)(d*10))
294 sprintf(out, "%.1f%s", d, units[unit]);
295 else if (d >= 10 || d*100 == (int)(d*100))
296 sprintf(out, "%.2f%s", d, units[unit]);
297 else
298 sprintf(out, "%.3f%s", d, units[unit]);
299 }
300
301 static void
302 hud_draw_graph_line_strip(struct hud_context *hud, const struct hud_graph *gr,
303 unsigned xoffset, unsigned yoffset, float yscale)
304 {
305 if (gr->num_vertices <= 1)
306 return;
307
308 assert(gr->index <= gr->num_vertices);
309
310 hud_draw_colored_prims(hud, PIPE_PRIM_LINE_STRIP,
311 gr->vertices, gr->index,
312 gr->color[0], gr->color[1], gr->color[2], 1,
313 xoffset + (gr->pane->max_num_vertices - gr->index - 1) * 2 - 1,
314 yoffset, yscale);
315
316 if (gr->num_vertices <= gr->index)
317 return;
318
319 hud_draw_colored_prims(hud, PIPE_PRIM_LINE_STRIP,
320 gr->vertices + gr->index*2,
321 gr->num_vertices - gr->index,
322 gr->color[0], gr->color[1], gr->color[2], 1,
323 xoffset - gr->index*2 - 1, yoffset, yscale);
324 }
325
326 static void
327 hud_pane_accumulate_vertices(struct hud_context *hud,
328 const struct hud_pane *pane)
329 {
330 struct hud_graph *gr;
331 float *line_verts = hud->whitelines.vertices + hud->whitelines.num_vertices*2;
332 unsigned i, num = 0;
333 char str[32];
334 const unsigned last_line = pane->last_line;
335
336 /* draw background */
337 hud_draw_background_quad(hud,
338 pane->x1, pane->y1,
339 pane->x2, pane->y2);
340
341 /* draw numbers on the right-hand side */
342 for (i = 0; i <= last_line; i++) {
343 unsigned x = pane->x2 + 2;
344 unsigned y = pane->inner_y1 +
345 pane->inner_height * (last_line - i) / last_line -
346 hud->font.glyph_height / 2;
347
348 number_to_human_readable(pane->max_value * i / last_line,
349 pane->type, str);
350 hud_draw_string(hud, x, y, "%s", str);
351 }
352
353 /* draw info below the pane */
354 i = 0;
355 LIST_FOR_EACH_ENTRY(gr, &pane->graph_list, head) {
356 unsigned x = pane->x1 + 2;
357 unsigned y = pane->y2 + 2 + i*hud->font.glyph_height;
358
359 number_to_human_readable(gr->current_value, pane->type, str);
360 hud_draw_string(hud, x, y, " %s: %s", gr->name, str);
361 i++;
362 }
363
364 /* draw border */
365 assert(hud->whitelines.num_vertices + num/2 + 8 <= hud->whitelines.max_num_vertices);
366 line_verts[num++] = (float) pane->x1;
367 line_verts[num++] = (float) pane->y1;
368 line_verts[num++] = (float) pane->x2;
369 line_verts[num++] = (float) pane->y1;
370
371 line_verts[num++] = (float) pane->x2;
372 line_verts[num++] = (float) pane->y1;
373 line_verts[num++] = (float) pane->x2;
374 line_verts[num++] = (float) pane->y2;
375
376 line_verts[num++] = (float) pane->x1;
377 line_verts[num++] = (float) pane->y2;
378 line_verts[num++] = (float) pane->x2;
379 line_verts[num++] = (float) pane->y2;
380
381 line_verts[num++] = (float) pane->x1;
382 line_verts[num++] = (float) pane->y1;
383 line_verts[num++] = (float) pane->x1;
384 line_verts[num++] = (float) pane->y2;
385
386 /* draw horizontal lines inside the graph */
387 for (i = 0; i <= last_line; i++) {
388 float y = round((pane->max_value * i / (double)last_line) *
389 pane->yscale + pane->inner_y2);
390
391 assert(hud->whitelines.num_vertices + num/2 + 2 <= hud->whitelines.max_num_vertices);
392 line_verts[num++] = pane->x1;
393 line_verts[num++] = y;
394 line_verts[num++] = pane->x2;
395 line_verts[num++] = y;
396 }
397
398 hud->whitelines.num_vertices += num/2;
399 }
400
401 static void
402 hud_pane_accumulate_vertices_simple(struct hud_context *hud,
403 const struct hud_pane *pane)
404 {
405 struct hud_graph *gr;
406 unsigned i;
407 char str[32];
408
409 /* draw info below the pane */
410 i = 0;
411 LIST_FOR_EACH_ENTRY(gr, &pane->graph_list, head) {
412 unsigned x = pane->x1;
413 unsigned y = pane->y_simple + i*hud->font.glyph_height;
414
415 number_to_human_readable(gr->current_value, pane->type, str);
416 hud_draw_string(hud, x, y, "%s: %s", gr->name, str);
417 i++;
418 }
419 }
420
421 static void
422 hud_pane_draw_colored_objects(struct hud_context *hud,
423 const struct hud_pane *pane)
424 {
425 struct hud_graph *gr;
426 unsigned i;
427
428 /* draw colored quads below the pane */
429 i = 0;
430 LIST_FOR_EACH_ENTRY(gr, &pane->graph_list, head) {
431 unsigned x = pane->x1 + 2;
432 unsigned y = pane->y2 + 2 + i*hud->font.glyph_height;
433
434 hud_draw_colored_quad(hud, PIPE_PRIM_QUADS, x + 1, y + 1, x + 12, y + 13,
435 gr->color[0], gr->color[1], gr->color[2], 1);
436 i++;
437 }
438
439 /* draw the line strips */
440 LIST_FOR_EACH_ENTRY(gr, &pane->graph_list, head) {
441 hud_draw_graph_line_strip(hud, gr, pane->inner_x1, pane->inner_y2, pane->yscale);
442 }
443 }
444
445 static void
446 hud_prepare_vertices(struct hud_context *hud, struct vertex_queue *v,
447 unsigned num_vertices, unsigned stride)
448 {
449 v->num_vertices = 0;
450 v->max_num_vertices = num_vertices;
451 v->vbuf.stride = stride;
452 v->buffer_size = stride * num_vertices;
453 }
454
455 /**
456 * Draw the HUD to the texture \p tex.
457 * The texture is usually the back buffer being displayed.
458 */
459 static void
460 hud_draw_results(struct hud_context *hud, struct pipe_resource *tex)
461 {
462 struct cso_context *cso = hud->cso;
463 struct pipe_context *pipe = hud->pipe;
464 struct pipe_framebuffer_state fb;
465 struct pipe_surface surf_templ, *surf;
466 struct pipe_viewport_state viewport;
467 const struct pipe_sampler_state *sampler_states[] =
468 { &hud->font_sampler_state };
469 struct hud_pane *pane;
470
471 if (!huds_visible)
472 return;
473
474 hud->fb_width = tex->width0;
475 hud->fb_height = tex->height0;
476 hud->constants.two_div_fb_width = 2.0f / hud->fb_width;
477 hud->constants.two_div_fb_height = 2.0f / hud->fb_height;
478
479 cso_save_state(cso, (CSO_BIT_FRAMEBUFFER |
480 CSO_BIT_SAMPLE_MASK |
481 CSO_BIT_MIN_SAMPLES |
482 CSO_BIT_BLEND |
483 CSO_BIT_DEPTH_STENCIL_ALPHA |
484 CSO_BIT_FRAGMENT_SHADER |
485 CSO_BIT_FRAGMENT_SAMPLER_VIEWS |
486 CSO_BIT_FRAGMENT_SAMPLERS |
487 CSO_BIT_RASTERIZER |
488 CSO_BIT_VIEWPORT |
489 CSO_BIT_STREAM_OUTPUTS |
490 CSO_BIT_GEOMETRY_SHADER |
491 CSO_BIT_TESSCTRL_SHADER |
492 CSO_BIT_TESSEVAL_SHADER |
493 CSO_BIT_VERTEX_SHADER |
494 CSO_BIT_VERTEX_ELEMENTS |
495 CSO_BIT_AUX_VERTEX_BUFFER_SLOT |
496 CSO_BIT_PAUSE_QUERIES |
497 CSO_BIT_RENDER_CONDITION));
498 cso_save_constant_buffer_slot0(cso, PIPE_SHADER_VERTEX);
499
500 /* set states */
501 memset(&surf_templ, 0, sizeof(surf_templ));
502 surf_templ.format = tex->format;
503
504 /* Without this, AA lines look thinner if they are between 2 pixels
505 * because the alpha is 0.5 on both pixels. (it's ugly)
506 *
507 * sRGB makes the width of all AA lines look the same.
508 */
509 if (hud->has_srgb) {
510 enum pipe_format srgb_format = util_format_srgb(tex->format);
511
512 if (srgb_format != PIPE_FORMAT_NONE)
513 surf_templ.format = srgb_format;
514 }
515 surf = pipe->create_surface(pipe, tex, &surf_templ);
516
517 memset(&fb, 0, sizeof(fb));
518 fb.nr_cbufs = 1;
519 fb.cbufs[0] = surf;
520 fb.zsbuf = NULL;
521 fb.width = hud->fb_width;
522 fb.height = hud->fb_height;
523
524 viewport.scale[0] = 0.5f * hud->fb_width;
525 viewport.scale[1] = 0.5f * hud->fb_height;
526 viewport.scale[2] = 1.0f;
527 viewport.translate[0] = 0.5f * hud->fb_width;
528 viewport.translate[1] = 0.5f * hud->fb_height;
529 viewport.translate[2] = 0.0f;
530
531 cso_set_framebuffer(cso, &fb);
532 cso_set_sample_mask(cso, ~0);
533 cso_set_min_samples(cso, 1);
534 cso_set_depth_stencil_alpha(cso, &hud->dsa);
535 cso_set_rasterizer(cso, &hud->rasterizer);
536 cso_set_viewport(cso, &viewport);
537 cso_set_stream_outputs(cso, 0, NULL, NULL);
538 cso_set_tessctrl_shader_handle(cso, NULL);
539 cso_set_tesseval_shader_handle(cso, NULL);
540 cso_set_geometry_shader_handle(cso, NULL);
541 cso_set_vertex_shader_handle(cso, hud->vs);
542 cso_set_vertex_elements(cso, &hud->velems);
543 cso_set_render_condition(cso, NULL, FALSE, 0);
544 cso_set_sampler_views(cso, PIPE_SHADER_FRAGMENT, 1,
545 &hud->font_sampler_view);
546 cso_set_samplers(cso, PIPE_SHADER_FRAGMENT, 1, sampler_states);
547 cso_set_constant_buffer(cso, PIPE_SHADER_VERTEX, 0, &hud->constbuf);
548
549 /* draw accumulated vertices for background quads */
550 cso_set_blend(cso, &hud->alpha_blend);
551 cso_set_fragment_shader_handle(hud->cso, hud->fs_color);
552
553 if (hud->bg.num_vertices) {
554 hud->constants.color[0] = 0;
555 hud->constants.color[1] = 0;
556 hud->constants.color[2] = 0;
557 hud->constants.color[3] = 0.666f;
558 hud->constants.translate[0] = 0;
559 hud->constants.translate[1] = 0;
560 hud->constants.scale[0] = hud_scale;
561 hud->constants.scale[1] = hud_scale;
562
563 cso_set_constant_buffer(cso, PIPE_SHADER_VERTEX, 0, &hud->constbuf);
564
565 cso_set_vertex_buffers(cso, 0, 1, &hud->bg.vbuf);
566 cso_draw_arrays(cso, PIPE_PRIM_QUADS, 0, hud->bg.num_vertices);
567 }
568 pipe_resource_reference(&hud->bg.vbuf.buffer.resource, NULL);
569
570 /* draw accumulated vertices for text */
571 if (hud->text.num_vertices) {
572 cso_set_vertex_buffers(cso, 0, 1, &hud->text.vbuf);
573 cso_set_fragment_shader_handle(hud->cso, hud->fs_text);
574 cso_draw_arrays(cso, PIPE_PRIM_QUADS, 0, hud->text.num_vertices);
575 }
576 pipe_resource_reference(&hud->text.vbuf.buffer.resource, NULL);
577
578 if (hud->simple) {
579 cso_restore_state(cso);
580 cso_restore_constant_buffer_slot0(cso, PIPE_SHADER_VERTEX);
581
582 pipe_surface_reference(&surf, NULL);
583 return;
584 }
585
586 /* draw accumulated vertices for white lines */
587 cso_set_blend(cso, &hud->no_blend);
588
589 hud->constants.color[0] = 1;
590 hud->constants.color[1] = 1;
591 hud->constants.color[2] = 1;
592 hud->constants.color[3] = 1;
593 hud->constants.translate[0] = 0;
594 hud->constants.translate[1] = 0;
595 hud->constants.scale[0] = hud_scale;
596 hud->constants.scale[1] = hud_scale;
597 cso_set_constant_buffer(cso, PIPE_SHADER_VERTEX, 0, &hud->constbuf);
598
599 if (hud->whitelines.num_vertices) {
600 cso_set_vertex_buffers(cso, 0, 1, &hud->whitelines.vbuf);
601 cso_set_fragment_shader_handle(hud->cso, hud->fs_color);
602 cso_draw_arrays(cso, PIPE_PRIM_LINES, 0, hud->whitelines.num_vertices);
603 }
604 pipe_resource_reference(&hud->whitelines.vbuf.buffer.resource, NULL);
605
606 /* draw the rest */
607 cso_set_blend(cso, &hud->alpha_blend);
608 cso_set_rasterizer(cso, &hud->rasterizer_aa_lines);
609 LIST_FOR_EACH_ENTRY(pane, &hud->pane_list, head) {
610 if (pane)
611 hud_pane_draw_colored_objects(hud, pane);
612 }
613
614 cso_restore_state(cso);
615 cso_restore_constant_buffer_slot0(cso, PIPE_SHADER_VERTEX);
616
617 pipe_surface_reference(&surf, NULL);
618 }
619
620 static void
621 hud_start_queries(struct hud_context *hud, struct pipe_context *pipe)
622 {
623 struct hud_pane *pane;
624 struct hud_graph *gr;
625
626 /* Start queries. */
627 hud_batch_query_begin(hud->batch_query, pipe);
628
629 LIST_FOR_EACH_ENTRY(pane, &hud->pane_list, head) {
630 LIST_FOR_EACH_ENTRY(gr, &pane->graph_list, head) {
631 if (gr->begin_query)
632 gr->begin_query(gr, pipe);
633 }
634 }
635 }
636
637 /* Stop queries, query results, and record vertices for charts. */
638 static void
639 hud_stop_queries(struct hud_context *hud, struct pipe_context *pipe)
640 {
641 struct hud_pane *pane;
642 struct hud_graph *gr, *next;
643
644 /* prepare vertex buffers */
645 hud_prepare_vertices(hud, &hud->bg, 16 * 256, 2 * sizeof(float));
646 hud_prepare_vertices(hud, &hud->whitelines, 4 * 256, 2 * sizeof(float));
647 hud_prepare_vertices(hud, &hud->text, 16 * 1024, 4 * sizeof(float));
648 hud_prepare_vertices(hud, &hud->color_prims, 32 * 1024, 2 * sizeof(float));
649
650 /* Allocate everything once and divide the storage into 3 portions
651 * manually, because u_upload_alloc can unmap memory from previous calls.
652 */
653 u_upload_alloc(pipe->stream_uploader, 0,
654 hud->bg.buffer_size +
655 hud->whitelines.buffer_size +
656 hud->text.buffer_size +
657 hud->color_prims.buffer_size,
658 16, &hud->bg.vbuf.buffer_offset, &hud->bg.vbuf.buffer.resource,
659 (void**)&hud->bg.vertices);
660 if (!hud->bg.vertices)
661 return;
662
663 pipe_resource_reference(&hud->whitelines.vbuf.buffer.resource, hud->bg.vbuf.buffer.resource);
664 pipe_resource_reference(&hud->text.vbuf.buffer.resource, hud->bg.vbuf.buffer.resource);
665 pipe_resource_reference(&hud->color_prims.vbuf.buffer.resource, hud->bg.vbuf.buffer.resource);
666
667 hud->whitelines.vbuf.buffer_offset = hud->bg.vbuf.buffer_offset +
668 hud->bg.buffer_size;
669 hud->whitelines.vertices = hud->bg.vertices +
670 hud->bg.buffer_size / sizeof(float);
671
672 hud->text.vbuf.buffer_offset = hud->whitelines.vbuf.buffer_offset +
673 hud->whitelines.buffer_size;
674 hud->text.vertices = hud->whitelines.vertices +
675 hud->whitelines.buffer_size / sizeof(float);
676
677 hud->color_prims.vbuf.buffer_offset = hud->text.vbuf.buffer_offset +
678 hud->text.buffer_size;
679 hud->color_prims.vertices = hud->text.vertices +
680 hud->text.buffer_size / sizeof(float);
681
682 /* prepare all graphs */
683 hud_batch_query_update(hud->batch_query, pipe);
684
685 LIST_FOR_EACH_ENTRY(pane, &hud->pane_list, head) {
686 LIST_FOR_EACH_ENTRY(gr, &pane->graph_list, head) {
687 gr->query_new_value(gr, pipe);
688 }
689
690 if (pane->sort_items) {
691 LIST_FOR_EACH_ENTRY_SAFE(gr, next, &pane->graph_list, head) {
692 /* ignore the last one */
693 if (&gr->head == pane->graph_list.prev)
694 continue;
695
696 /* This is an incremental bubble sort, because we only do one pass
697 * per frame. It will eventually reach an equilibrium.
698 */
699 if (gr->current_value <
700 LIST_ENTRY(struct hud_graph, next, head)->current_value) {
701 list_del(&gr->head);
702 list_add(&gr->head, &next->head);
703 }
704 }
705 }
706
707 if (hud->simple)
708 hud_pane_accumulate_vertices_simple(hud, pane);
709 else
710 hud_pane_accumulate_vertices(hud, pane);
711 }
712
713 /* unmap the uploader's vertex buffer before drawing */
714 u_upload_unmap(pipe->stream_uploader);
715 }
716
717 /**
718 * Record queries and draw the HUD. The "cso" parameter acts as a filter.
719 * If "cso" is not the recording context, recording is skipped.
720 * If "cso" is not the drawing context, drawing is skipped.
721 * cso == NULL ignores the filter.
722 */
723 void
724 hud_run(struct hud_context *hud, struct cso_context *cso,
725 struct pipe_resource *tex)
726 {
727 struct pipe_context *pipe = cso ? cso_get_pipe_context(cso) : NULL;
728
729 /* If "cso" is the recording or drawing context or NULL, execute
730 * the operation. Otherwise, don't do anything.
731 */
732 if (hud->record_pipe && (!pipe || pipe == hud->record_pipe))
733 hud_stop_queries(hud, hud->record_pipe);
734
735 if (hud->cso && (!cso || cso == hud->cso))
736 hud_draw_results(hud, tex);
737
738 if (hud->record_pipe && (!pipe || pipe == hud->record_pipe))
739 hud_start_queries(hud, hud->record_pipe);
740 }
741
742 /**
743 * Record query results and assemble vertices if "pipe" is a recording but
744 * not drawing context.
745 */
746 void
747 hud_record_only(struct hud_context *hud, struct pipe_context *pipe)
748 {
749 assert(pipe);
750
751 /* If it's a drawing context, only hud_run() records query results. */
752 if (pipe == hud->pipe || pipe != hud->record_pipe)
753 return;
754
755 hud_stop_queries(hud, hud->record_pipe);
756 hud_start_queries(hud, hud->record_pipe);
757 }
758
759 static void
760 fixup_bytes(enum pipe_driver_query_type type, int position, uint64_t *exp10)
761 {
762 if (type == PIPE_DRIVER_QUERY_TYPE_BYTES && position % 3 == 0)
763 *exp10 = (*exp10 / 1000) * 1024;
764 }
765
766 /**
767 * Set the maximum value for the Y axis of the graph.
768 * This scales the graph accordingly.
769 */
770 void
771 hud_pane_set_max_value(struct hud_pane *pane, uint64_t value)
772 {
773 double leftmost_digit;
774 uint64_t exp10;
775 int i;
776
777 /* The following code determines the max_value in the graph as well as
778 * how many describing lines are drawn. The max_value is rounded up,
779 * so that all drawn numbers are rounded for readability.
780 * We want to print multiples of a simple number instead of multiples of
781 * hard-to-read numbers like 1.753.
782 */
783
784 /* Find the left-most digit. Make sure exp10 * 10 and fixup_bytes doesn't
785 * overflow. (11 is safe) */
786 exp10 = 1;
787 for (i = 0; exp10 <= UINT64_MAX / 11 && exp10 * 9 < value; i++) {
788 exp10 *= 10;
789 fixup_bytes(pane->type, i + 1, &exp10);
790 }
791
792 leftmost_digit = DIV_ROUND_UP(value, exp10);
793
794 /* Round 9 to 10. */
795 if (leftmost_digit == 9) {
796 leftmost_digit = 1;
797 exp10 *= 10;
798 fixup_bytes(pane->type, i + 1, &exp10);
799 }
800
801 switch ((unsigned)leftmost_digit) {
802 case 1:
803 pane->last_line = 5; /* lines in +1/5 increments */
804 break;
805 case 2:
806 pane->last_line = 8; /* lines in +1/4 increments. */
807 break;
808 case 3:
809 case 4:
810 pane->last_line = leftmost_digit * 2; /* lines in +1/2 increments */
811 break;
812 case 5:
813 case 6:
814 case 7:
815 case 8:
816 pane->last_line = leftmost_digit; /* lines in +1 increments */
817 break;
818 default:
819 assert(0);
820 }
821
822 /* Truncate {3,4} to {2.5, 3.5} if possible. */
823 for (i = 3; i <= 4; i++) {
824 if (leftmost_digit == i && value <= (i - 0.5) * exp10) {
825 leftmost_digit = i - 0.5;
826 pane->last_line = leftmost_digit * 2; /* lines in +1/2 increments. */
827 }
828 }
829
830 /* Truncate 2 to a multiple of 0.2 in (1, 1.6] if possible. */
831 if (leftmost_digit == 2) {
832 for (i = 1; i <= 3; i++) {
833 if (value <= (1 + i*0.2) * exp10) {
834 leftmost_digit = 1 + i*0.2;
835 pane->last_line = 5 + i; /* lines in +1/5 increments. */
836 break;
837 }
838 }
839 }
840
841 pane->max_value = leftmost_digit * exp10;
842 pane->yscale = -(int)pane->inner_height / (float)pane->max_value;
843 }
844
845 static void
846 hud_pane_update_dyn_ceiling(struct hud_graph *gr, struct hud_pane *pane)
847 {
848 unsigned i;
849 float tmp = 0.0f;
850
851 if (pane->dyn_ceil_last_ran != gr->index) {
852 LIST_FOR_EACH_ENTRY(gr, &pane->graph_list, head) {
853 for (i = 0; i < gr->num_vertices; ++i) {
854 tmp = gr->vertices[i * 2 + 1] > tmp ?
855 gr->vertices[i * 2 + 1] : tmp;
856 }
857 }
858
859 /* Avoid setting it lower than the initial starting height. */
860 tmp = tmp > pane->initial_max_value ? tmp : pane->initial_max_value;
861 hud_pane_set_max_value(pane, tmp);
862 }
863
864 /*
865 * Mark this adjustment run so we could avoid repeating a full update
866 * again needlessly in case the pane has more than one graph.
867 */
868 pane->dyn_ceil_last_ran = gr->index;
869 }
870
871 static struct hud_pane *
872 hud_pane_create(struct hud_context *hud,
873 unsigned x1, unsigned y1, unsigned x2, unsigned y2,
874 unsigned y_simple,
875 unsigned period, uint64_t max_value, uint64_t ceiling,
876 boolean dyn_ceiling, boolean sort_items)
877 {
878 struct hud_pane *pane = CALLOC_STRUCT(hud_pane);
879
880 if (!pane)
881 return NULL;
882
883 pane->hud = hud;
884 pane->x1 = x1;
885 pane->y1 = y1;
886 pane->x2 = x2;
887 pane->y2 = y2;
888 pane->y_simple = y_simple;
889 pane->inner_x1 = x1 + 1;
890 pane->inner_x2 = x2 - 1;
891 pane->inner_y1 = y1 + 1;
892 pane->inner_y2 = y2 - 1;
893 pane->inner_width = pane->inner_x2 - pane->inner_x1;
894 pane->inner_height = pane->inner_y2 - pane->inner_y1;
895 pane->period = period;
896 pane->max_num_vertices = (x2 - x1 + 2) / 2;
897 pane->ceiling = ceiling;
898 pane->dyn_ceiling = dyn_ceiling;
899 pane->dyn_ceil_last_ran = 0;
900 pane->sort_items = sort_items;
901 pane->initial_max_value = max_value;
902 hud_pane_set_max_value(pane, max_value);
903 list_inithead(&pane->graph_list);
904 return pane;
905 }
906
907 /* replace '-' with a space */
908 static void
909 strip_hyphens(char *s)
910 {
911 while (*s) {
912 if (*s == '-')
913 *s = ' ';
914 s++;
915 }
916 }
917
918 /**
919 * Add a graph to an existing pane.
920 * One pane can contain multiple graphs over each other.
921 */
922 void
923 hud_pane_add_graph(struct hud_pane *pane, struct hud_graph *gr)
924 {
925 static const float colors[][3] = {
926 {0, 1, 0},
927 {1, 0, 0},
928 {0, 1, 1},
929 {1, 0, 1},
930 {1, 1, 0},
931 {0.5, 1, 0.5},
932 {1, 0.5, 0.5},
933 {0.5, 1, 1},
934 {1, 0.5, 1},
935 {1, 1, 0.5},
936 {0, 0.5, 0},
937 {0.5, 0, 0},
938 {0, 0.5, 0.5},
939 {0.5, 0, 0.5},
940 {0.5, 0.5, 0},
941 };
942 unsigned color = pane->next_color % ARRAY_SIZE(colors);
943
944 strip_hyphens(gr->name);
945
946 gr->vertices = MALLOC(pane->max_num_vertices * sizeof(float) * 2);
947 gr->color[0] = colors[color][0];
948 gr->color[1] = colors[color][1];
949 gr->color[2] = colors[color][2];
950 gr->pane = pane;
951 list_addtail(&gr->head, &pane->graph_list);
952 pane->num_graphs++;
953 pane->next_color++;
954 }
955
956 void
957 hud_graph_add_value(struct hud_graph *gr, double value)
958 {
959 gr->current_value = value;
960 value = value > gr->pane->ceiling ? gr->pane->ceiling : value;
961
962 if (gr->fd) {
963 if (fabs(value - lround(value)) > FLT_EPSILON) {
964 fprintf(gr->fd, "%f\n", value);
965 }
966 else {
967 fprintf(gr->fd, "%" PRIu64 "\n", (uint64_t) lround(value));
968 }
969 }
970
971 if (gr->index == gr->pane->max_num_vertices) {
972 gr->vertices[0] = 0;
973 gr->vertices[1] = gr->vertices[(gr->index-1)*2+1];
974 gr->index = 1;
975 }
976 gr->vertices[(gr->index)*2+0] = (float) (gr->index * 2);
977 gr->vertices[(gr->index)*2+1] = (float) value;
978 gr->index++;
979
980 if (gr->num_vertices < gr->pane->max_num_vertices) {
981 gr->num_vertices++;
982 }
983
984 if (gr->pane->dyn_ceiling == true) {
985 hud_pane_update_dyn_ceiling(gr, gr->pane);
986 }
987 if (value > gr->pane->max_value) {
988 hud_pane_set_max_value(gr->pane, value);
989 }
990 }
991
992 static void
993 hud_graph_destroy(struct hud_graph *graph, struct pipe_context *pipe)
994 {
995 FREE(graph->vertices);
996 if (graph->free_query_data)
997 graph->free_query_data(graph->query_data, pipe);
998 if (graph->fd)
999 fclose(graph->fd);
1000 FREE(graph);
1001 }
1002
1003 static void strcat_without_spaces(char *dst, const char *src)
1004 {
1005 dst += strlen(dst);
1006 while (*src) {
1007 if (*src == ' ')
1008 *dst++ = '_';
1009 else
1010 *dst++ = *src;
1011 src++;
1012 }
1013 *dst = 0;
1014 }
1015
1016
1017 #ifdef PIPE_OS_WINDOWS
1018 #define W_OK 0
1019 static int
1020 access(const char *pathname, int mode)
1021 {
1022 /* no-op */
1023 return 0;
1024 }
1025
1026 #define PATH_SEP "\\"
1027
1028 #else
1029
1030 #define PATH_SEP "/"
1031
1032 #endif
1033
1034
1035 /**
1036 * If the GALLIUM_HUD_DUMP_DIR env var is set, we'll write the raw
1037 * HUD values to files at ${GALLIUM_HUD_DUMP_DIR}/<stat> where <stat>
1038 * is a HUD variable such as "fps", or "cpu"
1039 */
1040 static void
1041 hud_graph_set_dump_file(struct hud_graph *gr)
1042 {
1043 const char *hud_dump_dir = getenv("GALLIUM_HUD_DUMP_DIR");
1044
1045 if (hud_dump_dir && access(hud_dump_dir, W_OK) == 0) {
1046 char *dump_file = malloc(strlen(hud_dump_dir) + sizeof(PATH_SEP)
1047 + sizeof(gr->name));
1048 if (dump_file) {
1049 strcpy(dump_file, hud_dump_dir);
1050 strcat(dump_file, PATH_SEP);
1051 strcat_without_spaces(dump_file, gr->name);
1052 gr->fd = fopen(dump_file, "w+");
1053 if (gr->fd) {
1054 /* flush output after each line is written */
1055 setvbuf(gr->fd, NULL, _IOLBF, 0);
1056 }
1057 free(dump_file);
1058 }
1059 }
1060 }
1061
1062 /**
1063 * Read a string from the environment variable.
1064 * The separators "+", ",", ":", and ";" terminate the string.
1065 * Return the number of read characters.
1066 */
1067 static int
1068 parse_string(const char *s, char *out)
1069 {
1070 int i;
1071
1072 for (i = 0; *s && *s != '+' && *s != ',' && *s != ':' && *s != ';' && *s != '=';
1073 s++, out++, i++)
1074 *out = *s;
1075
1076 *out = 0;
1077
1078 if (*s && !i) {
1079 fprintf(stderr, "gallium_hud: syntax error: unexpected '%c' (%i) while "
1080 "parsing a string\n", *s, *s);
1081 fflush(stderr);
1082 }
1083
1084 return i;
1085 }
1086
1087 static char *
1088 read_pane_settings(char *str, unsigned * const x, unsigned * const y,
1089 unsigned * const width, unsigned * const height,
1090 uint64_t * const ceiling, boolean * const dyn_ceiling,
1091 boolean *reset_colors, boolean *sort_items)
1092 {
1093 char *ret = str;
1094 unsigned tmp;
1095
1096 while (*str == '.') {
1097 ++str;
1098 switch (*str) {
1099 case 'x':
1100 ++str;
1101 *x = strtoul(str, &ret, 10);
1102 str = ret;
1103 break;
1104
1105 case 'y':
1106 ++str;
1107 *y = strtoul(str, &ret, 10);
1108 str = ret;
1109 break;
1110
1111 case 'w':
1112 ++str;
1113 tmp = strtoul(str, &ret, 10);
1114 *width = tmp > 80 ? tmp : 80; /* 80 is chosen arbitrarily */
1115 str = ret;
1116 break;
1117
1118 /*
1119 * Prevent setting height to less than 50. If the height is set to less,
1120 * the text of the Y axis labels on the graph will start overlapping.
1121 */
1122 case 'h':
1123 ++str;
1124 tmp = strtoul(str, &ret, 10);
1125 *height = tmp > 50 ? tmp : 50;
1126 str = ret;
1127 break;
1128
1129 case 'c':
1130 ++str;
1131 tmp = strtoul(str, &ret, 10);
1132 *ceiling = tmp > 10 ? tmp : 10;
1133 str = ret;
1134 break;
1135
1136 case 'd':
1137 ++str;
1138 ret = str;
1139 *dyn_ceiling = true;
1140 break;
1141
1142 case 'r':
1143 ++str;
1144 ret = str;
1145 *reset_colors = true;
1146 break;
1147
1148 case 's':
1149 ++str;
1150 ret = str;
1151 *sort_items = true;
1152 break;
1153
1154 default:
1155 fprintf(stderr, "gallium_hud: syntax error: unexpected '%c'\n", *str);
1156 fflush(stderr);
1157 }
1158
1159 }
1160
1161 return ret;
1162 }
1163
1164 static boolean
1165 has_occlusion_query(struct pipe_screen *screen)
1166 {
1167 return screen->get_param(screen, PIPE_CAP_OCCLUSION_QUERY) != 0;
1168 }
1169
1170 static boolean
1171 has_streamout(struct pipe_screen *screen)
1172 {
1173 return screen->get_param(screen, PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS) != 0;
1174 }
1175
1176 static boolean
1177 has_pipeline_stats_query(struct pipe_screen *screen)
1178 {
1179 return screen->get_param(screen, PIPE_CAP_QUERY_PIPELINE_STATISTICS) != 0;
1180 }
1181
1182 static void
1183 hud_parse_env_var(struct hud_context *hud, struct pipe_screen *screen,
1184 const char *env)
1185 {
1186 unsigned num, i;
1187 char name_a[256], s[256];
1188 char *name;
1189 struct hud_pane *pane = NULL;
1190 unsigned x = 10, y = 10, y_simple = 10;
1191 unsigned width = 251, height = 100;
1192 unsigned period = 500 * 1000; /* default period (1/2 second) */
1193 uint64_t ceiling = UINT64_MAX;
1194 unsigned column_width = 251;
1195 boolean dyn_ceiling = false;
1196 boolean reset_colors = false;
1197 boolean sort_items = false;
1198 const char *period_env;
1199
1200 if (strncmp(env, "simple,", 7) == 0) {
1201 hud->simple = true;
1202 env += 7;
1203 }
1204
1205 /*
1206 * The GALLIUM_HUD_PERIOD env var sets the graph update rate.
1207 * The env var is in seconds (a float).
1208 * Zero means update after every frame.
1209 */
1210 period_env = getenv("GALLIUM_HUD_PERIOD");
1211 if (period_env) {
1212 float p = (float) atof(period_env);
1213 if (p >= 0.0f) {
1214 period = (unsigned) (p * 1000 * 1000);
1215 }
1216 }
1217
1218 while ((num = parse_string(env, name_a)) != 0) {
1219 bool added = true;
1220
1221 env += num;
1222
1223 /* check for explicit location, size and etc. settings */
1224 name = read_pane_settings(name_a, &x, &y, &width, &height, &ceiling,
1225 &dyn_ceiling, &reset_colors, &sort_items);
1226
1227 /*
1228 * Keep track of overall column width to avoid pane overlapping in case
1229 * later we create a new column while the bottom pane in the current
1230 * column is less wide than the rest of the panes in it.
1231 */
1232 column_width = width > column_width ? width : column_width;
1233
1234 if (!pane) {
1235 pane = hud_pane_create(hud, x, y, x + width, y + height, y_simple,
1236 period, 10, ceiling, dyn_ceiling, sort_items);
1237 if (!pane)
1238 return;
1239 }
1240
1241 if (reset_colors) {
1242 pane->next_color = 0;
1243 reset_colors = false;
1244 }
1245
1246 /* Add a graph. */
1247 #if defined(HAVE_GALLIUM_EXTRA_HUD) || defined(HAVE_LIBSENSORS)
1248 char arg_name[64];
1249 #endif
1250 /* IF YOU CHANGE THIS, UPDATE print_help! */
1251 if (strcmp(name, "fps") == 0) {
1252 hud_fps_graph_install(pane);
1253 }
1254 else if (strcmp(name, "frametime") == 0) {
1255 hud_frametime_graph_install(pane);
1256 }
1257 else if (strcmp(name, "cpu") == 0) {
1258 hud_cpu_graph_install(pane, ALL_CPUS);
1259 }
1260 else if (sscanf(name, "cpu%u%s", &i, s) == 1) {
1261 hud_cpu_graph_install(pane, i);
1262 }
1263 else if (strcmp(name, "API-thread-busy") == 0) {
1264 hud_thread_busy_install(pane, name, false);
1265 }
1266 else if (strcmp(name, "API-thread-offloaded-slots") == 0) {
1267 hud_thread_counter_install(pane, name, HUD_COUNTER_OFFLOADED);
1268 }
1269 else if (strcmp(name, "API-thread-direct-slots") == 0) {
1270 hud_thread_counter_install(pane, name, HUD_COUNTER_DIRECT);
1271 }
1272 else if (strcmp(name, "API-thread-num-syncs") == 0) {
1273 hud_thread_counter_install(pane, name, HUD_COUNTER_SYNCS);
1274 }
1275 else if (strcmp(name, "main-thread-busy") == 0) {
1276 hud_thread_busy_install(pane, name, true);
1277 }
1278 #ifdef HAVE_GALLIUM_EXTRA_HUD
1279 else if (sscanf(name, "nic-rx-%s", arg_name) == 1) {
1280 hud_nic_graph_install(pane, arg_name, NIC_DIRECTION_RX);
1281 }
1282 else if (sscanf(name, "nic-tx-%s", arg_name) == 1) {
1283 hud_nic_graph_install(pane, arg_name, NIC_DIRECTION_TX);
1284 }
1285 else if (sscanf(name, "nic-rssi-%s", arg_name) == 1) {
1286 hud_nic_graph_install(pane, arg_name, NIC_RSSI_DBM);
1287 pane->type = PIPE_DRIVER_QUERY_TYPE_DBM;
1288 }
1289 else if (sscanf(name, "diskstat-rd-%s", arg_name) == 1) {
1290 hud_diskstat_graph_install(pane, arg_name, DISKSTAT_RD);
1291 pane->type = PIPE_DRIVER_QUERY_TYPE_BYTES;
1292 }
1293 else if (sscanf(name, "diskstat-wr-%s", arg_name) == 1) {
1294 hud_diskstat_graph_install(pane, arg_name, DISKSTAT_WR);
1295 pane->type = PIPE_DRIVER_QUERY_TYPE_BYTES;
1296 }
1297 else if (sscanf(name, "cpufreq-min-cpu%u", &i) == 1) {
1298 hud_cpufreq_graph_install(pane, i, CPUFREQ_MINIMUM);
1299 pane->type = PIPE_DRIVER_QUERY_TYPE_HZ;
1300 }
1301 else if (sscanf(name, "cpufreq-cur-cpu%u", &i) == 1) {
1302 hud_cpufreq_graph_install(pane, i, CPUFREQ_CURRENT);
1303 pane->type = PIPE_DRIVER_QUERY_TYPE_HZ;
1304 }
1305 else if (sscanf(name, "cpufreq-max-cpu%u", &i) == 1) {
1306 hud_cpufreq_graph_install(pane, i, CPUFREQ_MAXIMUM);
1307 pane->type = PIPE_DRIVER_QUERY_TYPE_HZ;
1308 }
1309 #endif
1310 #ifdef HAVE_LIBSENSORS
1311 else if (sscanf(name, "sensors_temp_cu-%s", arg_name) == 1) {
1312 hud_sensors_temp_graph_install(pane, arg_name,
1313 SENSORS_TEMP_CURRENT);
1314 pane->type = PIPE_DRIVER_QUERY_TYPE_TEMPERATURE;
1315 }
1316 else if (sscanf(name, "sensors_temp_cr-%s", arg_name) == 1) {
1317 hud_sensors_temp_graph_install(pane, arg_name,
1318 SENSORS_TEMP_CRITICAL);
1319 pane->type = PIPE_DRIVER_QUERY_TYPE_TEMPERATURE;
1320 }
1321 else if (sscanf(name, "sensors_volt_cu-%s", arg_name) == 1) {
1322 hud_sensors_temp_graph_install(pane, arg_name,
1323 SENSORS_VOLTAGE_CURRENT);
1324 pane->type = PIPE_DRIVER_QUERY_TYPE_VOLTS;
1325 }
1326 else if (sscanf(name, "sensors_curr_cu-%s", arg_name) == 1) {
1327 hud_sensors_temp_graph_install(pane, arg_name,
1328 SENSORS_CURRENT_CURRENT);
1329 pane->type = PIPE_DRIVER_QUERY_TYPE_AMPS;
1330 }
1331 else if (sscanf(name, "sensors_pow_cu-%s", arg_name) == 1) {
1332 hud_sensors_temp_graph_install(pane, arg_name,
1333 SENSORS_POWER_CURRENT);
1334 pane->type = PIPE_DRIVER_QUERY_TYPE_WATTS;
1335 }
1336 #endif
1337 else if (strcmp(name, "samples-passed") == 0 &&
1338 has_occlusion_query(screen)) {
1339 hud_pipe_query_install(&hud->batch_query, pane,
1340 "samples-passed",
1341 PIPE_QUERY_OCCLUSION_COUNTER, 0, 0,
1342 PIPE_DRIVER_QUERY_TYPE_UINT64,
1343 PIPE_DRIVER_QUERY_RESULT_TYPE_AVERAGE,
1344 0);
1345 }
1346 else if (strcmp(name, "primitives-generated") == 0 &&
1347 has_streamout(screen)) {
1348 hud_pipe_query_install(&hud->batch_query, pane,
1349 "primitives-generated",
1350 PIPE_QUERY_PRIMITIVES_GENERATED, 0, 0,
1351 PIPE_DRIVER_QUERY_TYPE_UINT64,
1352 PIPE_DRIVER_QUERY_RESULT_TYPE_AVERAGE,
1353 0);
1354 }
1355 else {
1356 boolean processed = FALSE;
1357
1358 /* pipeline statistics queries */
1359 if (has_pipeline_stats_query(screen)) {
1360 static const char *pipeline_statistics_names[] =
1361 {
1362 "ia-vertices",
1363 "ia-primitives",
1364 "vs-invocations",
1365 "gs-invocations",
1366 "gs-primitives",
1367 "clipper-invocations",
1368 "clipper-primitives-generated",
1369 "ps-invocations",
1370 "hs-invocations",
1371 "ds-invocations",
1372 "cs-invocations"
1373 };
1374 for (i = 0; i < ARRAY_SIZE(pipeline_statistics_names); ++i)
1375 if (strcmp(name, pipeline_statistics_names[i]) == 0)
1376 break;
1377 if (i < ARRAY_SIZE(pipeline_statistics_names)) {
1378 hud_pipe_query_install(&hud->batch_query, pane, name,
1379 PIPE_QUERY_PIPELINE_STATISTICS, i,
1380 0, PIPE_DRIVER_QUERY_TYPE_UINT64,
1381 PIPE_DRIVER_QUERY_RESULT_TYPE_AVERAGE,
1382 0);
1383 processed = TRUE;
1384 }
1385 }
1386
1387 /* driver queries */
1388 if (!processed) {
1389 if (!hud_driver_query_install(&hud->batch_query, pane,
1390 screen, name)) {
1391 fprintf(stderr, "gallium_hud: unknown driver query '%s'\n", name);
1392 fflush(stderr);
1393 added = false;
1394 }
1395 }
1396 }
1397
1398 if (*env == ':') {
1399 env++;
1400
1401 if (!pane) {
1402 fprintf(stderr, "gallium_hud: syntax error: unexpected ':', "
1403 "expected a name\n");
1404 fflush(stderr);
1405 break;
1406 }
1407
1408 num = parse_string(env, s);
1409 env += num;
1410
1411 if (num && sscanf(s, "%u", &i) == 1) {
1412 hud_pane_set_max_value(pane, i);
1413 pane->initial_max_value = i;
1414 }
1415 else {
1416 fprintf(stderr, "gallium_hud: syntax error: unexpected '%c' (%i) "
1417 "after ':'\n", *env, *env);
1418 fflush(stderr);
1419 }
1420 }
1421
1422 if (*env == '=') {
1423 env++;
1424
1425 if (!pane) {
1426 fprintf(stderr, "gallium_hud: syntax error: unexpected '=', "
1427 "expected a name\n");
1428 fflush(stderr);
1429 break;
1430 }
1431
1432 num = parse_string(env, s);
1433 env += num;
1434
1435 strip_hyphens(s);
1436 if (added && !list_is_empty(&pane->graph_list)) {
1437 struct hud_graph *graph;
1438 graph = LIST_ENTRY(struct hud_graph, pane->graph_list.prev, head);
1439 strncpy(graph->name, s, sizeof(graph->name)-1);
1440 graph->name[sizeof(graph->name)-1] = 0;
1441 }
1442 }
1443
1444 if (*env == 0)
1445 break;
1446
1447 /* parse a separator */
1448 switch (*env) {
1449 case '+':
1450 env++;
1451 break;
1452
1453 case ',':
1454 env++;
1455 if (!pane)
1456 break;
1457
1458 y += height + hud->font.glyph_height * (pane->num_graphs + 2);
1459 y_simple += hud->font.glyph_height * (pane->num_graphs + 1);
1460 height = 100;
1461
1462 if (pane && pane->num_graphs) {
1463 list_addtail(&pane->head, &hud->pane_list);
1464 pane = NULL;
1465 }
1466 break;
1467
1468 case ';':
1469 env++;
1470 y = 10;
1471 y_simple = 10;
1472 x += column_width + hud->font.glyph_width * 9;
1473 height = 100;
1474
1475 if (pane && pane->num_graphs) {
1476 list_addtail(&pane->head, &hud->pane_list);
1477 pane = NULL;
1478 }
1479
1480 /* Starting a new column; reset column width. */
1481 column_width = 251;
1482 break;
1483
1484 default:
1485 fprintf(stderr, "gallium_hud: syntax error: unexpected '%c'\n", *env);
1486 fflush(stderr);
1487 }
1488
1489 /* Reset to defaults for the next pane in case these were modified. */
1490 width = 251;
1491 ceiling = UINT64_MAX;
1492 dyn_ceiling = false;
1493 sort_items = false;
1494
1495 }
1496
1497 if (pane) {
1498 if (pane->num_graphs) {
1499 list_addtail(&pane->head, &hud->pane_list);
1500 }
1501 else {
1502 FREE(pane);
1503 }
1504 }
1505
1506 LIST_FOR_EACH_ENTRY(pane, &hud->pane_list, head) {
1507 struct hud_graph *gr;
1508
1509 LIST_FOR_EACH_ENTRY(gr, &pane->graph_list, head) {
1510 hud_graph_set_dump_file(gr);
1511 }
1512 }
1513 }
1514
1515 static void
1516 print_help(struct pipe_screen *screen)
1517 {
1518 int i, num_queries, num_cpus = hud_get_num_cpus();
1519
1520 puts("Syntax: GALLIUM_HUD=name1[+name2][...][:value1][,nameI...][;nameJ...]");
1521 puts("");
1522 puts(" Names are identifiers of data sources which will be drawn as graphs");
1523 puts(" in panes. Multiple graphs can be drawn in the same pane.");
1524 puts(" There can be multiple panes placed in rows and columns.");
1525 puts("");
1526 puts(" '+' separates names which will share a pane.");
1527 puts(" ':[value]' specifies the initial maximum value of the Y axis");
1528 puts(" for the given pane.");
1529 puts(" ',' creates a new pane below the last one.");
1530 puts(" ';' creates a new pane at the top of the next column.");
1531 puts(" '=' followed by a string, changes the name of the last data source");
1532 puts(" to that string");
1533 puts("");
1534 puts(" Example: GALLIUM_HUD=\"cpu,fps;primitives-generated\"");
1535 puts("");
1536 puts(" Additionally, by prepending '.[identifier][value]' modifiers to");
1537 puts(" a name, it is possible to explicitly set the location and size");
1538 puts(" of a pane, along with limiting overall maximum value of the");
1539 puts(" Y axis and activating dynamic readjustment of the Y axis.");
1540 puts(" Several modifiers may be applied to the same pane simultaneously.");
1541 puts("");
1542 puts(" 'x[value]' sets the location of the pane on the x axis relative");
1543 puts(" to the upper-left corner of the viewport, in pixels.");
1544 puts(" 'y[value]' sets the location of the pane on the y axis relative");
1545 puts(" to the upper-left corner of the viewport, in pixels.");
1546 puts(" 'w[value]' sets width of the graph pixels.");
1547 puts(" 'h[value]' sets height of the graph in pixels.");
1548 puts(" 'c[value]' sets the ceiling of the value of the Y axis.");
1549 puts(" If the graph needs to draw values higher than");
1550 puts(" the ceiling allows, the value is clamped.");
1551 puts(" 'd' activates dynamic Y axis readjustment to set the value of");
1552 puts(" the Y axis to match the highest value still visible in the graph.");
1553 puts(" 'r' resets the color counter (the next color will be green)");
1554 puts(" 's' sort items below graphs in descending order");
1555 puts("");
1556 puts(" If 'c' and 'd' modifiers are used simultaneously, both are in effect:");
1557 puts(" the Y axis does not go above the restriction imposed by 'c' while");
1558 puts(" still adjusting the value of the Y axis down when appropriate.");
1559 puts("");
1560 puts(" You can change behavior of the whole HUD by adding these options at");
1561 puts(" the beginning of the environment variable:");
1562 puts(" 'simple,' disables all the fancy stuff and only draws text.");
1563 puts("");
1564 puts(" Example: GALLIUM_HUD=\".w256.h64.x1600.y520.d.c1000fps+cpu,.datom-count\"");
1565 puts("");
1566 puts(" Available names:");
1567 puts(" fps");
1568 puts(" frametime");
1569 puts(" cpu");
1570
1571 for (i = 0; i < num_cpus; i++)
1572 printf(" cpu%i\n", i);
1573
1574 if (has_occlusion_query(screen))
1575 puts(" samples-passed");
1576 if (has_streamout(screen))
1577 puts(" primitives-generated");
1578
1579 if (has_pipeline_stats_query(screen)) {
1580 puts(" ia-vertices");
1581 puts(" ia-primitives");
1582 puts(" vs-invocations");
1583 puts(" gs-invocations");
1584 puts(" gs-primitives");
1585 puts(" clipper-invocations");
1586 puts(" clipper-primitives-generated");
1587 puts(" ps-invocations");
1588 puts(" hs-invocations");
1589 puts(" ds-invocations");
1590 puts(" cs-invocations");
1591 }
1592
1593 #ifdef HAVE_GALLIUM_EXTRA_HUD
1594 hud_get_num_disks(1);
1595 hud_get_num_nics(1);
1596 hud_get_num_cpufreq(1);
1597 #endif
1598 #ifdef HAVE_LIBSENSORS
1599 hud_get_num_sensors(1);
1600 #endif
1601
1602 if (screen->get_driver_query_info){
1603 boolean skipping = false;
1604 struct pipe_driver_query_info info;
1605 num_queries = screen->get_driver_query_info(screen, 0, NULL);
1606
1607 for (i = 0; i < num_queries; i++){
1608 screen->get_driver_query_info(screen, i, &info);
1609 if (info.flags & PIPE_DRIVER_QUERY_FLAG_DONT_LIST) {
1610 if (!skipping)
1611 puts(" ...");
1612 skipping = true;
1613 } else {
1614 printf(" %s\n", info.name);
1615 skipping = false;
1616 }
1617 }
1618 }
1619
1620 puts("");
1621 fflush(stdout);
1622 }
1623
1624 static void
1625 hud_unset_draw_context(struct hud_context *hud)
1626 {
1627 struct pipe_context *pipe = hud->pipe;
1628
1629 if (!pipe)
1630 return;
1631
1632 pipe_sampler_view_reference(&hud->font_sampler_view, NULL);
1633
1634 if (hud->fs_color) {
1635 pipe->delete_fs_state(pipe, hud->fs_color);
1636 hud->fs_color = NULL;
1637 }
1638 if (hud->fs_text) {
1639 pipe->delete_fs_state(pipe, hud->fs_text);
1640 hud->fs_text = NULL;
1641 }
1642 if (hud->vs) {
1643 pipe->delete_vs_state(pipe, hud->vs);
1644 hud->vs = NULL;
1645 }
1646
1647 hud->cso = NULL;
1648 hud->pipe = NULL;
1649 }
1650
1651 static bool
1652 hud_set_draw_context(struct hud_context *hud, struct cso_context *cso)
1653 {
1654 struct pipe_context *pipe = cso_get_pipe_context(cso);
1655
1656 assert(!hud->pipe);
1657 hud->pipe = pipe;
1658 hud->cso = cso;
1659
1660 struct pipe_sampler_view view_templ;
1661 u_sampler_view_default_template(
1662 &view_templ, hud->font.texture, hud->font.texture->format);
1663 hud->font_sampler_view = pipe->create_sampler_view(pipe, hud->font.texture,
1664 &view_templ);
1665 if (!hud->font_sampler_view)
1666 goto fail;
1667
1668 /* fragment shader */
1669 hud->fs_color =
1670 util_make_fragment_passthrough_shader(pipe,
1671 TGSI_SEMANTIC_COLOR,
1672 TGSI_INTERPOLATE_CONSTANT,
1673 TRUE);
1674
1675 {
1676 /* Read a texture and do .xxxx swizzling. */
1677 static const char *fragment_shader_text = {
1678 "FRAG\n"
1679 "DCL IN[0], GENERIC[0], LINEAR\n"
1680 "DCL SAMP[0]\n"
1681 "DCL SVIEW[0], RECT, FLOAT\n"
1682 "DCL OUT[0], COLOR[0]\n"
1683 "DCL TEMP[0]\n"
1684
1685 "TEX TEMP[0], IN[0], SAMP[0], RECT\n"
1686 "MOV OUT[0], TEMP[0].xxxx\n"
1687 "END\n"
1688 };
1689
1690 struct tgsi_token tokens[1000];
1691 struct pipe_shader_state state;
1692
1693 if (!tgsi_text_translate(fragment_shader_text, tokens, ARRAY_SIZE(tokens))) {
1694 assert(0);
1695 goto fail;
1696 }
1697 pipe_shader_state_from_tgsi(&state, tokens);
1698 hud->fs_text = pipe->create_fs_state(pipe, &state);
1699 }
1700
1701 /* vertex shader */
1702 {
1703 static const char *vertex_shader_text = {
1704 "VERT\n"
1705 "DCL IN[0..1]\n"
1706 "DCL OUT[0], POSITION\n"
1707 "DCL OUT[1], COLOR[0]\n" /* color */
1708 "DCL OUT[2], GENERIC[0]\n" /* texcoord */
1709 /* [0] = color,
1710 * [1] = (2/fb_width, 2/fb_height, xoffset, yoffset)
1711 * [2] = (xscale, yscale, 0, 0) */
1712 "DCL CONST[0][0..2]\n"
1713 "DCL TEMP[0]\n"
1714 "IMM[0] FLT32 { -1, 0, 0, 1 }\n"
1715
1716 /* v = in * (xscale, yscale) + (xoffset, yoffset) */
1717 "MAD TEMP[0].xy, IN[0], CONST[0][2].xyyy, CONST[0][1].zwww\n"
1718 /* pos = v * (2 / fb_width, 2 / fb_height) - (1, 1) */
1719 "MAD OUT[0].xy, TEMP[0], CONST[0][1].xyyy, IMM[0].xxxx\n"
1720 "MOV OUT[0].zw, IMM[0]\n"
1721
1722 "MOV OUT[1], CONST[0][0]\n"
1723 "MOV OUT[2], IN[1]\n"
1724 "END\n"
1725 };
1726
1727 struct tgsi_token tokens[1000];
1728 struct pipe_shader_state state;
1729 if (!tgsi_text_translate(vertex_shader_text, tokens, ARRAY_SIZE(tokens))) {
1730 assert(0);
1731 goto fail;
1732 }
1733 pipe_shader_state_from_tgsi(&state, tokens);
1734 hud->vs = pipe->create_vs_state(pipe, &state);
1735 }
1736
1737 return true;
1738
1739 fail:
1740 hud_unset_draw_context(hud);
1741 fprintf(stderr, "hud: failed to set a draw context");
1742 return false;
1743 }
1744
1745 static void
1746 hud_unset_record_context(struct hud_context *hud)
1747 {
1748 struct pipe_context *pipe = hud->record_pipe;
1749 struct hud_pane *pane, *pane_tmp;
1750 struct hud_graph *graph, *graph_tmp;
1751
1752 if (!pipe)
1753 return;
1754
1755 LIST_FOR_EACH_ENTRY_SAFE(pane, pane_tmp, &hud->pane_list, head) {
1756 LIST_FOR_EACH_ENTRY_SAFE(graph, graph_tmp, &pane->graph_list, head) {
1757 list_del(&graph->head);
1758 hud_graph_destroy(graph, pipe);
1759 }
1760 list_del(&pane->head);
1761 FREE(pane);
1762 }
1763
1764 hud_batch_query_cleanup(&hud->batch_query, pipe);
1765 hud->record_pipe = NULL;
1766 }
1767
1768 static void
1769 hud_set_record_context(struct hud_context *hud, struct pipe_context *pipe)
1770 {
1771 hud->record_pipe = pipe;
1772 }
1773
1774 /**
1775 * Create the HUD.
1776 *
1777 * If "share" is non-NULL and GALLIUM_HUD_SHARE=x,y is set, increment the
1778 * reference counter of "share", set "cso" as the recording or drawing context
1779 * according to the environment variable, and return "share".
1780 * This allows sharing the HUD instance within a multi-context share group,
1781 * record queries in one context and draw them in another.
1782 */
1783 struct hud_context *
1784 hud_create(struct cso_context *cso, struct hud_context *share)
1785 {
1786 const char *share_env = debug_get_option("GALLIUM_HUD_SHARE", NULL);
1787 unsigned record_ctx = 0, draw_ctx = 0;
1788
1789 if (share_env && sscanf(share_env, "%u,%u", &record_ctx, &draw_ctx) != 2)
1790 share_env = NULL;
1791
1792 if (share && share_env) {
1793 /* All contexts in a share group share the HUD instance.
1794 * Only one context can record queries and only one context
1795 * can draw the HUD.
1796 *
1797 * GALLIUM_HUD_SHARE=x,y determines the context indices.
1798 */
1799 int context_id = p_atomic_inc_return(&share->refcount) - 1;
1800
1801 if (context_id == record_ctx) {
1802 assert(!share->record_pipe);
1803 hud_set_record_context(share, cso_get_pipe_context(cso));
1804 }
1805
1806 if (context_id == draw_ctx) {
1807 assert(!share->pipe);
1808 hud_set_draw_context(share, cso);
1809 }
1810
1811 return share;
1812 }
1813
1814 struct pipe_screen *screen = cso_get_pipe_context(cso)->screen;
1815 struct hud_context *hud;
1816 unsigned i;
1817 const char *env = debug_get_option("GALLIUM_HUD", NULL);
1818 #ifdef PIPE_OS_UNIX
1819 unsigned signo = debug_get_num_option("GALLIUM_HUD_TOGGLE_SIGNAL", 0);
1820 static boolean sig_handled = FALSE;
1821 struct sigaction action;
1822
1823 memset(&action, 0, sizeof(action));
1824 #endif
1825 huds_visible = debug_get_bool_option("GALLIUM_HUD_VISIBLE", TRUE);
1826 hud_scale = debug_get_num_option("GALLIUM_HUD_SCALE", 1);
1827
1828 if (!env || !*env)
1829 return NULL;
1830
1831 if (strcmp(env, "help") == 0) {
1832 print_help(screen);
1833 return NULL;
1834 }
1835
1836 hud = CALLOC_STRUCT(hud_context);
1837 if (!hud)
1838 return NULL;
1839
1840 /* font (the context is only used for the texture upload) */
1841 if (!util_font_create(cso_get_pipe_context(cso),
1842 UTIL_FONT_FIXED_8X13, &hud->font)) {
1843 FREE(hud);
1844 return NULL;
1845 }
1846
1847 hud->refcount = 1;
1848 hud->has_srgb = screen->is_format_supported(screen,
1849 PIPE_FORMAT_B8G8R8A8_SRGB,
1850 PIPE_TEXTURE_2D, 0, 0,
1851 PIPE_BIND_RENDER_TARGET) != 0;
1852
1853 /* blend state */
1854 hud->no_blend.rt[0].colormask = PIPE_MASK_RGBA;
1855
1856 hud->alpha_blend.rt[0].colormask = PIPE_MASK_RGBA;
1857 hud->alpha_blend.rt[0].blend_enable = 1;
1858 hud->alpha_blend.rt[0].rgb_func = PIPE_BLEND_ADD;
1859 hud->alpha_blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_SRC_ALPHA;
1860 hud->alpha_blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_INV_SRC_ALPHA;
1861 hud->alpha_blend.rt[0].alpha_func = PIPE_BLEND_ADD;
1862 hud->alpha_blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ZERO;
1863 hud->alpha_blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ONE;
1864
1865 /* rasterizer */
1866 hud->rasterizer.half_pixel_center = 1;
1867 hud->rasterizer.bottom_edge_rule = 1;
1868 hud->rasterizer.depth_clip_near = 1;
1869 hud->rasterizer.depth_clip_far = 1;
1870 hud->rasterizer.line_width = 1;
1871 hud->rasterizer.line_last_pixel = 1;
1872
1873 hud->rasterizer_aa_lines = hud->rasterizer;
1874 hud->rasterizer_aa_lines.line_smooth = 1;
1875
1876 /* vertex elements */
1877 hud->velems.count = 2;
1878 for (i = 0; i < 2; i++) {
1879 hud->velems.velems[i].src_offset = i * 2 * sizeof(float);
1880 hud->velems.velems[i].src_format = PIPE_FORMAT_R32G32_FLOAT;
1881 hud->velems.velems[i].vertex_buffer_index = 0;
1882 }
1883
1884 /* sampler state (for font drawing) */
1885 hud->font_sampler_state.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
1886 hud->font_sampler_state.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
1887 hud->font_sampler_state.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
1888 hud->font_sampler_state.normalized_coords = 0;
1889
1890 /* constants */
1891 hud->constbuf.buffer_size = sizeof(hud->constants);
1892 hud->constbuf.user_buffer = &hud->constants;
1893
1894 list_inithead(&hud->pane_list);
1895
1896 /* setup sig handler once for all hud contexts */
1897 #ifdef PIPE_OS_UNIX
1898 if (!sig_handled && signo != 0) {
1899 action.sa_sigaction = &signal_visible_handler;
1900 action.sa_flags = SA_SIGINFO;
1901
1902 if (signo >= NSIG)
1903 fprintf(stderr, "gallium_hud: invalid signal %u\n", signo);
1904 else if (sigaction(signo, &action, NULL) < 0)
1905 fprintf(stderr, "gallium_hud: unable to set handler for signal %u\n", signo);
1906 fflush(stderr);
1907
1908 sig_handled = TRUE;
1909 }
1910 #endif
1911
1912 if (record_ctx == 0)
1913 hud_set_record_context(hud, cso_get_pipe_context(cso));
1914 if (draw_ctx == 0)
1915 hud_set_draw_context(hud, cso);
1916
1917 hud_parse_env_var(hud, screen, env);
1918 return hud;
1919 }
1920
1921 /**
1922 * Destroy a HUD. If the HUD has several users, decrease the reference counter
1923 * and detach the context from the HUD.
1924 */
1925 void
1926 hud_destroy(struct hud_context *hud, struct cso_context *cso)
1927 {
1928 if (!cso || hud->record_pipe == cso_get_pipe_context(cso))
1929 hud_unset_record_context(hud);
1930
1931 if (!cso || hud->cso == cso)
1932 hud_unset_draw_context(hud);
1933
1934 if (p_atomic_dec_zero(&hud->refcount)) {
1935 pipe_resource_reference(&hud->font.texture, NULL);
1936 FREE(hud);
1937 }
1938 }
1939
1940 void
1941 hud_add_queue_for_monitoring(struct hud_context *hud,
1942 struct util_queue_monitoring *queue_info)
1943 {
1944 assert(!hud->monitored_queue);
1945 hud->monitored_queue = queue_info;
1946 }