Path: blob/21.2-virgl/src/gallium/auxiliary/hud/hud_context.c
4565 views
/**************************************************************************1*2* Copyright 2013 Marek Olšák <[email protected]>3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL THE AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/2627/* This head-up display module can draw transparent graphs on top of what28* the app is rendering, visualizing various data like framerate, cpu load,29* performance counters, etc. It can be hook up into any gallium frontend.30*31* The HUD is controlled with the GALLIUM_HUD environment variable.32* Set GALLIUM_HUD=help for more info.33*/3435#include <inttypes.h>36#include <signal.h>37#include <stdio.h>3839#include "hud/hud_context.h"40#include "hud/hud_private.h"4142#include "frontend/api.h"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"5556/* Control the visibility of all HUD contexts */57static boolean huds_visible = TRUE;58static int hud_scale = 1;596061#ifdef PIPE_OS_UNIX62static void63signal_visible_handler(int sig, siginfo_t *siginfo, void *context)64{65huds_visible = !huds_visible;66}67#endif6869static void70hud_draw_colored_prims(struct hud_context *hud, unsigned prim,71float *buffer, unsigned num_vertices,72float r, float g, float b, float a,73int xoffset, int yoffset, float yscale)74{75struct cso_context *cso = hud->cso;76struct pipe_context *pipe = hud->pipe;77struct pipe_vertex_buffer vbuffer = {0};7879hud->constants.color[0] = r;80hud->constants.color[1] = g;81hud->constants.color[2] = b;82hud->constants.color[3] = a;83hud->constants.translate[0] = (float) (xoffset * hud_scale);84hud->constants.translate[1] = (float) (yoffset * hud_scale);85hud->constants.scale[0] = hud_scale;86hud->constants.scale[1] = yscale * hud_scale;87pipe->set_constant_buffer(pipe, PIPE_SHADER_VERTEX, 0, false, &hud->constbuf);8889u_upload_data(hud->pipe->stream_uploader, 0,90num_vertices * 2 * sizeof(float), 16, buffer,91&vbuffer.buffer_offset, &vbuffer.buffer.resource);92u_upload_unmap(hud->pipe->stream_uploader);93vbuffer.stride = 2 * sizeof(float);9495cso_set_vertex_buffers(cso, 0, 1, &vbuffer);96pipe_resource_reference(&vbuffer.buffer.resource, NULL);97cso_set_fragment_shader_handle(hud->cso, hud->fs_color);98cso_draw_arrays(cso, prim, 0, num_vertices);99}100101static void102hud_draw_colored_quad(struct hud_context *hud, unsigned prim,103unsigned x1, unsigned y1, unsigned x2, unsigned y2,104float r, float g, float b, float a)105{106float buffer[] = {107(float) x1, (float) y1,108(float) x1, (float) y2,109(float) x2, (float) y2,110(float) x2, (float) y1,111};112113hud_draw_colored_prims(hud, prim, buffer, 4, r, g, b, a, 0, 0, 1);114}115116static void117hud_draw_background_quad(struct hud_context *hud,118unsigned x1, unsigned y1, unsigned x2, unsigned y2)119{120float *vertices = hud->bg.vertices + hud->bg.num_vertices*2;121unsigned num = 0;122123assert(hud->bg.num_vertices + 4 <= hud->bg.max_num_vertices);124125vertices[num++] = (float) x1;126vertices[num++] = (float) y1;127128vertices[num++] = (float) x1;129vertices[num++] = (float) y2;130131vertices[num++] = (float) x2;132vertices[num++] = (float) y2;133134vertices[num++] = (float) x2;135vertices[num++] = (float) y1;136137hud->bg.num_vertices += num/2;138}139140static void141hud_draw_string(struct hud_context *hud, unsigned x, unsigned y,142const char *str, ...)143{144char buf[256];145char *s = buf;146float *vertices = hud->text.vertices + hud->text.num_vertices*4;147unsigned num = 0;148149va_list ap;150va_start(ap, str);151vsnprintf(buf, sizeof(buf), str, ap);152va_end(ap);153154if (!*s)155return;156157hud_draw_background_quad(hud,158x, y,159x + strlen(buf)*hud->font.glyph_width,160y + hud->font.glyph_height);161162while (*s) {163unsigned x1 = x;164unsigned y1 = y;165unsigned x2 = x + hud->font.glyph_width;166unsigned y2 = y + hud->font.glyph_height;167unsigned tx1 = (*s % 16) * hud->font.glyph_width;168unsigned ty1 = (*s / 16) * hud->font.glyph_height;169unsigned tx2 = tx1 + hud->font.glyph_width;170unsigned ty2 = ty1 + hud->font.glyph_height;171172if (*s == ' ') {173x += hud->font.glyph_width;174s++;175continue;176}177178assert(hud->text.num_vertices + num/4 + 4 <= hud->text.max_num_vertices);179180vertices[num++] = (float) x1;181vertices[num++] = (float) y1;182vertices[num++] = (float) tx1;183vertices[num++] = (float) ty1;184185vertices[num++] = (float) x1;186vertices[num++] = (float) y2;187vertices[num++] = (float) tx1;188vertices[num++] = (float) ty2;189190vertices[num++] = (float) x2;191vertices[num++] = (float) y2;192vertices[num++] = (float) tx2;193vertices[num++] = (float) ty2;194195vertices[num++] = (float) x2;196vertices[num++] = (float) y1;197vertices[num++] = (float) tx2;198vertices[num++] = (float) ty1;199200x += hud->font.glyph_width;201s++;202}203204hud->text.num_vertices += num/4;205}206207static void208number_to_human_readable(double num, enum pipe_driver_query_type type,209char *out)210{211static const char *byte_units[] =212{" B", " KB", " MB", " GB", " TB", " PB", " EB"};213static const char *metric_units[] =214{"", " k", " M", " G", " T", " P", " E"};215static const char *time_units[] =216{" us", " ms", " s"}; /* based on microseconds */217static const char *hz_units[] =218{" Hz", " KHz", " MHz", " GHz"};219static const char *percent_units[] = {"%"};220static const char *dbm_units[] = {" (-dBm)"};221static const char *temperature_units[] = {" C"};222static const char *volt_units[] = {" mV", " V"};223static const char *amp_units[] = {" mA", " A"};224static const char *watt_units[] = {" mW", " W"};225static const char *float_units[] = {""};226227const char **units;228unsigned max_unit;229double divisor = (type == PIPE_DRIVER_QUERY_TYPE_BYTES) ? 1024 : 1000;230unsigned unit = 0;231double d = num;232233switch (type) {234case PIPE_DRIVER_QUERY_TYPE_MICROSECONDS:235max_unit = ARRAY_SIZE(time_units)-1;236units = time_units;237break;238case PIPE_DRIVER_QUERY_TYPE_VOLTS:239max_unit = ARRAY_SIZE(volt_units)-1;240units = volt_units;241break;242case PIPE_DRIVER_QUERY_TYPE_AMPS:243max_unit = ARRAY_SIZE(amp_units)-1;244units = amp_units;245break;246case PIPE_DRIVER_QUERY_TYPE_DBM:247max_unit = ARRAY_SIZE(dbm_units)-1;248units = dbm_units;249break;250case PIPE_DRIVER_QUERY_TYPE_TEMPERATURE:251max_unit = ARRAY_SIZE(temperature_units)-1;252units = temperature_units;253break;254case PIPE_DRIVER_QUERY_TYPE_FLOAT:255max_unit = ARRAY_SIZE(float_units)-1;256units = float_units;257break;258case PIPE_DRIVER_QUERY_TYPE_PERCENTAGE:259max_unit = ARRAY_SIZE(percent_units)-1;260units = percent_units;261break;262case PIPE_DRIVER_QUERY_TYPE_BYTES:263max_unit = ARRAY_SIZE(byte_units)-1;264units = byte_units;265break;266case PIPE_DRIVER_QUERY_TYPE_HZ:267max_unit = ARRAY_SIZE(hz_units)-1;268units = hz_units;269break;270case PIPE_DRIVER_QUERY_TYPE_WATTS:271max_unit = ARRAY_SIZE(watt_units)-1;272units = watt_units;273break;274default:275max_unit = ARRAY_SIZE(metric_units)-1;276units = metric_units;277}278279while (d > divisor && unit < max_unit) {280d /= divisor;281unit++;282}283284/* Round to 3 decimal places so as not to print trailing zeros. */285if (d*1000 != (int)(d*1000))286d = round(d * 1000) / 1000;287288/* Show at least 4 digits with at most 3 decimal places, but not zeros. */289if (d >= 1000 || d == (int)d)290sprintf(out, "%.0f%s", d, units[unit]);291else if (d >= 100 || d*10 == (int)(d*10))292sprintf(out, "%.1f%s", d, units[unit]);293else if (d >= 10 || d*100 == (int)(d*100))294sprintf(out, "%.2f%s", d, units[unit]);295else296sprintf(out, "%.3f%s", d, units[unit]);297}298299static void300hud_draw_graph_line_strip(struct hud_context *hud, const struct hud_graph *gr,301unsigned xoffset, unsigned yoffset, float yscale)302{303if (gr->num_vertices <= 1)304return;305306assert(gr->index <= gr->num_vertices);307308hud_draw_colored_prims(hud, PIPE_PRIM_LINE_STRIP,309gr->vertices, gr->index,310gr->color[0], gr->color[1], gr->color[2], 1,311xoffset + (gr->pane->max_num_vertices - gr->index - 1) * 2 - 1,312yoffset, yscale);313314if (gr->num_vertices <= gr->index)315return;316317hud_draw_colored_prims(hud, PIPE_PRIM_LINE_STRIP,318gr->vertices + gr->index*2,319gr->num_vertices - gr->index,320gr->color[0], gr->color[1], gr->color[2], 1,321xoffset - gr->index*2 - 1, yoffset, yscale);322}323324static void325hud_pane_accumulate_vertices(struct hud_context *hud,326const struct hud_pane *pane)327{328struct hud_graph *gr;329float *line_verts = hud->whitelines.vertices + hud->whitelines.num_vertices*2;330unsigned i, num = 0;331char str[32];332const unsigned last_line = pane->last_line;333334/* draw background */335hud_draw_background_quad(hud,336pane->x1, pane->y1,337pane->x2, pane->y2);338339/* draw numbers on the right-hand side */340for (i = 0; i <= last_line; i++) {341unsigned x = pane->x2 + 2;342unsigned y = pane->inner_y1 +343pane->inner_height * (last_line - i) / last_line -344hud->font.glyph_height / 2;345346number_to_human_readable(pane->max_value * i / last_line,347pane->type, str);348hud_draw_string(hud, x, y, "%s", str);349}350351/* draw info below the pane */352i = 0;353LIST_FOR_EACH_ENTRY(gr, &pane->graph_list, head) {354unsigned x = pane->x1 + 2;355unsigned y = pane->y2 + 2 + i*hud->font.glyph_height;356357number_to_human_readable(gr->current_value, pane->type, str);358hud_draw_string(hud, x, y, " %s: %s", gr->name, str);359i++;360}361362/* draw border */363assert(hud->whitelines.num_vertices + num/2 + 8 <= hud->whitelines.max_num_vertices);364line_verts[num++] = (float) pane->x1;365line_verts[num++] = (float) pane->y1;366line_verts[num++] = (float) pane->x2;367line_verts[num++] = (float) pane->y1;368369line_verts[num++] = (float) pane->x2;370line_verts[num++] = (float) pane->y1;371line_verts[num++] = (float) pane->x2;372line_verts[num++] = (float) pane->y2;373374line_verts[num++] = (float) pane->x1;375line_verts[num++] = (float) pane->y2;376line_verts[num++] = (float) pane->x2;377line_verts[num++] = (float) pane->y2;378379line_verts[num++] = (float) pane->x1;380line_verts[num++] = (float) pane->y1;381line_verts[num++] = (float) pane->x1;382line_verts[num++] = (float) pane->y2;383384/* draw horizontal lines inside the graph */385for (i = 0; i <= last_line; i++) {386float y = round((pane->max_value * i / (double)last_line) *387pane->yscale + pane->inner_y2);388389assert(hud->whitelines.num_vertices + num/2 + 2 <= hud->whitelines.max_num_vertices);390line_verts[num++] = pane->x1;391line_verts[num++] = y;392line_verts[num++] = pane->x2;393line_verts[num++] = y;394}395396hud->whitelines.num_vertices += num/2;397}398399static void400hud_pane_accumulate_vertices_simple(struct hud_context *hud,401const struct hud_pane *pane)402{403struct hud_graph *gr;404unsigned i;405char str[32];406407/* draw info below the pane */408i = 0;409LIST_FOR_EACH_ENTRY(gr, &pane->graph_list, head) {410unsigned x = pane->x1;411unsigned y = pane->y_simple + i*hud->font.glyph_height;412413number_to_human_readable(gr->current_value, pane->type, str);414hud_draw_string(hud, x, y, "%s: %s", gr->name, str);415i++;416}417}418419static void420hud_pane_draw_colored_objects(struct hud_context *hud,421const struct hud_pane *pane)422{423struct hud_graph *gr;424unsigned i;425426/* draw colored quads below the pane */427i = 0;428LIST_FOR_EACH_ENTRY(gr, &pane->graph_list, head) {429unsigned x = pane->x1 + 2;430unsigned y = pane->y2 + 2 + i*hud->font.glyph_height;431432hud_draw_colored_quad(hud, PIPE_PRIM_QUADS, x + 1, y + 1, x + 12, y + 13,433gr->color[0], gr->color[1], gr->color[2], 1);434i++;435}436437/* draw the line strips */438LIST_FOR_EACH_ENTRY(gr, &pane->graph_list, head) {439hud_draw_graph_line_strip(hud, gr, pane->inner_x1, pane->inner_y2, pane->yscale);440}441}442443static void444hud_prepare_vertices(struct hud_context *hud, struct vertex_queue *v,445unsigned num_vertices, unsigned stride)446{447v->num_vertices = 0;448v->max_num_vertices = num_vertices;449v->vbuf.stride = stride;450v->buffer_size = stride * num_vertices;451}452453/**454* Draw the HUD to the texture \p tex.455* The texture is usually the back buffer being displayed.456*/457static void458hud_draw_results(struct hud_context *hud, struct pipe_resource *tex)459{460struct cso_context *cso = hud->cso;461struct pipe_context *pipe = hud->pipe;462struct pipe_framebuffer_state fb;463struct pipe_surface surf_templ, *surf;464struct pipe_viewport_state viewport;465const struct pipe_sampler_state *sampler_states[] =466{ &hud->font_sampler_state };467struct hud_pane *pane;468469if (!huds_visible)470return;471472hud->fb_width = tex->width0;473hud->fb_height = tex->height0;474hud->constants.two_div_fb_width = 2.0f / hud->fb_width;475hud->constants.two_div_fb_height = 2.0f / hud->fb_height;476477cso_save_state(cso, (CSO_BIT_FRAMEBUFFER |478CSO_BIT_SAMPLE_MASK |479CSO_BIT_MIN_SAMPLES |480CSO_BIT_BLEND |481CSO_BIT_DEPTH_STENCIL_ALPHA |482CSO_BIT_FRAGMENT_SHADER |483CSO_BIT_FRAGMENT_SAMPLERS |484CSO_BIT_RASTERIZER |485CSO_BIT_VIEWPORT |486CSO_BIT_STREAM_OUTPUTS |487CSO_BIT_GEOMETRY_SHADER |488CSO_BIT_TESSCTRL_SHADER |489CSO_BIT_TESSEVAL_SHADER |490CSO_BIT_VERTEX_SHADER |491CSO_BIT_VERTEX_ELEMENTS |492CSO_BIT_PAUSE_QUERIES |493CSO_BIT_RENDER_CONDITION));494495/* set states */496memset(&surf_templ, 0, sizeof(surf_templ));497surf_templ.format = tex->format;498499/* Without this, AA lines look thinner if they are between 2 pixels500* because the alpha is 0.5 on both pixels. (it's ugly)501*502* sRGB makes the width of all AA lines look the same.503*/504if (hud->has_srgb) {505enum pipe_format srgb_format = util_format_srgb(tex->format);506507if (srgb_format != PIPE_FORMAT_NONE)508surf_templ.format = srgb_format;509}510surf = pipe->create_surface(pipe, tex, &surf_templ);511512memset(&fb, 0, sizeof(fb));513fb.nr_cbufs = 1;514fb.cbufs[0] = surf;515fb.zsbuf = NULL;516fb.width = hud->fb_width;517fb.height = hud->fb_height;518519viewport.scale[0] = 0.5f * hud->fb_width;520viewport.scale[1] = 0.5f * hud->fb_height;521viewport.scale[2] = 0.0f;522viewport.translate[0] = 0.5f * hud->fb_width;523viewport.translate[1] = 0.5f * hud->fb_height;524viewport.translate[2] = 0.0f;525viewport.swizzle_x = PIPE_VIEWPORT_SWIZZLE_POSITIVE_X;526viewport.swizzle_y = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Y;527viewport.swizzle_z = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Z;528viewport.swizzle_w = PIPE_VIEWPORT_SWIZZLE_POSITIVE_W;529530cso_set_framebuffer(cso, &fb);531cso_set_sample_mask(cso, ~0);532cso_set_min_samples(cso, 1);533cso_set_depth_stencil_alpha(cso, &hud->dsa);534cso_set_rasterizer(cso, &hud->rasterizer);535cso_set_viewport(cso, &viewport);536cso_set_stream_outputs(cso, 0, NULL, NULL);537cso_set_tessctrl_shader_handle(cso, NULL);538cso_set_tesseval_shader_handle(cso, NULL);539cso_set_geometry_shader_handle(cso, NULL);540cso_set_vertex_shader_handle(cso, hud->vs_color);541cso_set_vertex_elements(cso, &hud->velems);542cso_set_render_condition(cso, NULL, FALSE, 0);543pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 1, 0,544&hud->font_sampler_view);545cso_set_samplers(cso, PIPE_SHADER_FRAGMENT, 1, sampler_states);546pipe->set_constant_buffer(pipe, PIPE_SHADER_VERTEX, 0, false, &hud->constbuf);547548/* draw accumulated vertices for background quads */549cso_set_blend(cso, &hud->alpha_blend);550cso_set_fragment_shader_handle(hud->cso, hud->fs_color);551552if (hud->bg.num_vertices) {553hud->constants.color[0] = 0;554hud->constants.color[1] = 0;555hud->constants.color[2] = 0;556hud->constants.color[3] = 0.666f;557hud->constants.translate[0] = 0;558hud->constants.translate[1] = 0;559hud->constants.scale[0] = hud_scale;560hud->constants.scale[1] = hud_scale;561562pipe->set_constant_buffer(pipe, PIPE_SHADER_VERTEX, 0, false, &hud->constbuf);563564cso_set_vertex_buffers(cso, 0, 1, &hud->bg.vbuf);565cso_draw_arrays(cso, PIPE_PRIM_QUADS, 0, hud->bg.num_vertices);566}567pipe_resource_reference(&hud->bg.vbuf.buffer.resource, NULL);568569/* draw accumulated vertices for text */570if (hud->text.num_vertices) {571cso_set_vertex_shader_handle(cso, hud->vs_text);572cso_set_vertex_buffers(cso, 0, 1, &hud->text.vbuf);573cso_set_fragment_shader_handle(hud->cso, hud->fs_text);574cso_draw_arrays(cso, PIPE_PRIM_QUADS, 0, hud->text.num_vertices);575}576pipe_resource_reference(&hud->text.vbuf.buffer.resource, NULL);577578if (hud->simple)579goto done;580581/* draw accumulated vertices for white lines */582cso_set_blend(cso, &hud->no_blend);583584hud->constants.color[0] = 1;585hud->constants.color[1] = 1;586hud->constants.color[2] = 1;587hud->constants.color[3] = 1;588hud->constants.translate[0] = 0;589hud->constants.translate[1] = 0;590hud->constants.scale[0] = hud_scale;591hud->constants.scale[1] = hud_scale;592pipe->set_constant_buffer(pipe, PIPE_SHADER_VERTEX, 0, false, &hud->constbuf);593594if (hud->whitelines.num_vertices) {595cso_set_vertex_shader_handle(cso, hud->vs_color);596cso_set_vertex_buffers(cso, 0, 1, &hud->whitelines.vbuf);597cso_set_fragment_shader_handle(hud->cso, hud->fs_color);598cso_draw_arrays(cso, PIPE_PRIM_LINES, 0, hud->whitelines.num_vertices);599}600pipe_resource_reference(&hud->whitelines.vbuf.buffer.resource, NULL);601602/* draw the rest */603cso_set_blend(cso, &hud->alpha_blend);604cso_set_rasterizer(cso, &hud->rasterizer_aa_lines);605LIST_FOR_EACH_ENTRY(pane, &hud->pane_list, head) {606if (pane)607hud_pane_draw_colored_objects(hud, pane);608}609610done:611cso_restore_state(cso);612613/* Unbind resources that we have bound. */614pipe->set_constant_buffer(pipe, PIPE_SHADER_VERTEX, 0, false, NULL);615pipe->set_vertex_buffers(pipe, 0, 0, 1, false, NULL);616pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 0, 1, NULL);617618/* restore states not restored by cso */619if (hud->st) {620hud->st->invalidate_state(hud->st,621ST_INVALIDATE_FS_SAMPLER_VIEWS |622ST_INVALIDATE_VS_CONSTBUF0 |623ST_INVALIDATE_VERTEX_BUFFERS);624}625626pipe_surface_reference(&surf, NULL);627}628629static void630hud_start_queries(struct hud_context *hud, struct pipe_context *pipe)631{632struct hud_pane *pane;633struct hud_graph *gr;634635/* Start queries. */636hud_batch_query_begin(hud->batch_query, pipe);637638LIST_FOR_EACH_ENTRY(pane, &hud->pane_list, head) {639LIST_FOR_EACH_ENTRY(gr, &pane->graph_list, head) {640if (gr->begin_query)641gr->begin_query(gr, pipe);642}643}644}645646/* Stop queries, query results, and record vertices for charts. */647static void648hud_stop_queries(struct hud_context *hud, struct pipe_context *pipe)649{650struct hud_pane *pane;651struct hud_graph *gr, *next;652653/* prepare vertex buffers */654hud_prepare_vertices(hud, &hud->bg, 16 * 256, 2 * sizeof(float));655hud_prepare_vertices(hud, &hud->whitelines, 4 * 256, 2 * sizeof(float));656hud_prepare_vertices(hud, &hud->text, 16 * 1024, 4 * sizeof(float));657658/* Allocate everything once and divide the storage into 3 portions659* manually, because u_upload_alloc can unmap memory from previous calls.660*/661u_upload_alloc(pipe->stream_uploader, 0,662hud->bg.buffer_size +663hud->whitelines.buffer_size +664hud->text.buffer_size,66516, &hud->bg.vbuf.buffer_offset, &hud->bg.vbuf.buffer.resource,666(void**)&hud->bg.vertices);667if (!hud->bg.vertices)668return;669670pipe_resource_reference(&hud->whitelines.vbuf.buffer.resource, hud->bg.vbuf.buffer.resource);671pipe_resource_reference(&hud->text.vbuf.buffer.resource, hud->bg.vbuf.buffer.resource);672673hud->whitelines.vbuf.buffer_offset = hud->bg.vbuf.buffer_offset +674hud->bg.buffer_size;675hud->whitelines.vertices = hud->bg.vertices +676hud->bg.buffer_size / sizeof(float);677678hud->text.vbuf.buffer_offset = hud->whitelines.vbuf.buffer_offset +679hud->whitelines.buffer_size;680hud->text.vertices = hud->whitelines.vertices +681hud->whitelines.buffer_size / sizeof(float);682683/* prepare all graphs */684hud_batch_query_update(hud->batch_query, pipe);685686LIST_FOR_EACH_ENTRY(pane, &hud->pane_list, head) {687LIST_FOR_EACH_ENTRY(gr, &pane->graph_list, head) {688gr->query_new_value(gr, pipe);689}690691if (pane->sort_items) {692LIST_FOR_EACH_ENTRY_SAFE(gr, next, &pane->graph_list, head) {693/* ignore the last one */694if (&gr->head == pane->graph_list.prev)695continue;696697/* This is an incremental bubble sort, because we only do one pass698* per frame. It will eventually reach an equilibrium.699*/700if (gr->current_value <701LIST_ENTRY(struct hud_graph, next, head)->current_value) {702list_del(&gr->head);703list_add(&gr->head, &next->head);704}705}706}707708if (hud->simple)709hud_pane_accumulate_vertices_simple(hud, pane);710else711hud_pane_accumulate_vertices(hud, pane);712}713714/* unmap the uploader's vertex buffer before drawing */715u_upload_unmap(pipe->stream_uploader);716}717718/**719* Record queries and draw the HUD. The "cso" parameter acts as a filter.720* If "cso" is not the recording context, recording is skipped.721* If "cso" is not the drawing context, drawing is skipped.722* cso == NULL ignores the filter.723*/724void725hud_run(struct hud_context *hud, struct cso_context *cso,726struct pipe_resource *tex)727{728struct pipe_context *pipe = cso ? cso_get_pipe_context(cso) : NULL;729730/* If "cso" is the recording or drawing context or NULL, execute731* the operation. Otherwise, don't do anything.732*/733if (hud->record_pipe && (!pipe || pipe == hud->record_pipe))734hud_stop_queries(hud, hud->record_pipe);735736if (hud->cso && (!cso || cso == hud->cso))737hud_draw_results(hud, tex);738739if (hud->record_pipe && (!pipe || pipe == hud->record_pipe))740hud_start_queries(hud, hud->record_pipe);741}742743/**744* Record query results and assemble vertices if "pipe" is a recording but745* not drawing context.746*/747void748hud_record_only(struct hud_context *hud, struct pipe_context *pipe)749{750assert(pipe);751752/* If it's a drawing context, only hud_run() records query results. */753if (pipe == hud->pipe || pipe != hud->record_pipe)754return;755756hud_stop_queries(hud, hud->record_pipe);757hud_start_queries(hud, hud->record_pipe);758}759760static void761fixup_bytes(enum pipe_driver_query_type type, int position, uint64_t *exp10)762{763if (type == PIPE_DRIVER_QUERY_TYPE_BYTES && position % 3 == 0)764*exp10 = (*exp10 / 1000) * 1024;765}766767/**768* Set the maximum value for the Y axis of the graph.769* This scales the graph accordingly.770*/771void772hud_pane_set_max_value(struct hud_pane *pane, uint64_t value)773{774double leftmost_digit;775uint64_t exp10;776int i;777778/* The following code determines the max_value in the graph as well as779* how many describing lines are drawn. The max_value is rounded up,780* so that all drawn numbers are rounded for readability.781* We want to print multiples of a simple number instead of multiples of782* hard-to-read numbers like 1.753.783*/784785/* Find the left-most digit. Make sure exp10 * 10 and fixup_bytes doesn't786* overflow. (11 is safe) */787exp10 = 1;788for (i = 0; exp10 <= UINT64_MAX / 11 && exp10 * 9 < value; i++) {789exp10 *= 10;790fixup_bytes(pane->type, i + 1, &exp10);791}792793leftmost_digit = DIV_ROUND_UP(value, exp10);794795/* Round 9 to 10. */796if (leftmost_digit == 9) {797leftmost_digit = 1;798exp10 *= 10;799fixup_bytes(pane->type, i + 1, &exp10);800}801802switch ((unsigned)leftmost_digit) {803case 1:804pane->last_line = 5; /* lines in +1/5 increments */805break;806case 2:807pane->last_line = 8; /* lines in +1/4 increments. */808break;809case 3:810case 4:811pane->last_line = leftmost_digit * 2; /* lines in +1/2 increments */812break;813case 5:814case 6:815case 7:816case 8:817pane->last_line = leftmost_digit; /* lines in +1 increments */818break;819default:820assert(0);821}822823/* Truncate {3,4} to {2.5, 3.5} if possible. */824for (i = 3; i <= 4; i++) {825if (leftmost_digit == i && value <= (i - 0.5) * exp10) {826leftmost_digit = i - 0.5;827pane->last_line = leftmost_digit * 2; /* lines in +1/2 increments. */828}829}830831/* Truncate 2 to a multiple of 0.2 in (1, 1.6] if possible. */832if (leftmost_digit == 2) {833for (i = 1; i <= 3; i++) {834if (value <= (1 + i*0.2) * exp10) {835leftmost_digit = 1 + i*0.2;836pane->last_line = 5 + i; /* lines in +1/5 increments. */837break;838}839}840}841842pane->max_value = leftmost_digit * exp10;843pane->yscale = -(int)pane->inner_height / (float)pane->max_value;844}845846static void847hud_pane_update_dyn_ceiling(struct hud_graph *gr, struct hud_pane *pane)848{849unsigned i;850float tmp = 0.0f;851852if (pane->dyn_ceil_last_ran != gr->index) {853LIST_FOR_EACH_ENTRY(gr, &pane->graph_list, head) {854for (i = 0; i < gr->num_vertices; ++i) {855tmp = gr->vertices[i * 2 + 1] > tmp ?856gr->vertices[i * 2 + 1] : tmp;857}858}859860/* Avoid setting it lower than the initial starting height. */861tmp = tmp > pane->initial_max_value ? tmp : pane->initial_max_value;862hud_pane_set_max_value(pane, tmp);863}864865/*866* Mark this adjustment run so we could avoid repeating a full update867* again needlessly in case the pane has more than one graph.868*/869pane->dyn_ceil_last_ran = gr->index;870}871872static struct hud_pane *873hud_pane_create(struct hud_context *hud,874unsigned x1, unsigned y1, unsigned x2, unsigned y2,875unsigned y_simple,876unsigned period, uint64_t max_value, uint64_t ceiling,877boolean dyn_ceiling, boolean sort_items)878{879struct hud_pane *pane = CALLOC_STRUCT(hud_pane);880881if (!pane)882return NULL;883884pane->hud = hud;885pane->x1 = x1;886pane->y1 = y1;887pane->x2 = x2;888pane->y2 = y2;889pane->y_simple = y_simple;890pane->inner_x1 = x1 + 1;891pane->inner_x2 = x2 - 1;892pane->inner_y1 = y1 + 1;893pane->inner_y2 = y2 - 1;894pane->inner_width = pane->inner_x2 - pane->inner_x1;895pane->inner_height = pane->inner_y2 - pane->inner_y1;896pane->period = period;897pane->max_num_vertices = (x2 - x1 + 2) / 2;898pane->ceiling = ceiling;899pane->dyn_ceiling = dyn_ceiling;900pane->dyn_ceil_last_ran = 0;901pane->sort_items = sort_items;902pane->initial_max_value = max_value;903hud_pane_set_max_value(pane, max_value);904list_inithead(&pane->graph_list);905return pane;906}907908/* replace '-' with a space */909static void910strip_hyphens(char *s)911{912while (*s) {913if (*s == '-')914*s = ' ';915s++;916}917}918919/**920* Add a graph to an existing pane.921* One pane can contain multiple graphs over each other.922*/923void924hud_pane_add_graph(struct hud_pane *pane, struct hud_graph *gr)925{926static const float colors[][3] = {927{0, 1, 0},928{1, 0, 0},929{0, 1, 1},930{1, 0, 1},931{1, 1, 0},932{0.5, 1, 0.5},933{1, 0.5, 0.5},934{0.5, 1, 1},935{1, 0.5, 1},936{1, 1, 0.5},937{0, 0.5, 0},938{0.5, 0, 0},939{0, 0.5, 0.5},940{0.5, 0, 0.5},941{0.5, 0.5, 0},942};943unsigned color = pane->next_color % ARRAY_SIZE(colors);944945strip_hyphens(gr->name);946947gr->vertices = MALLOC(pane->max_num_vertices * sizeof(float) * 2);948gr->color[0] = colors[color][0];949gr->color[1] = colors[color][1];950gr->color[2] = colors[color][2];951gr->pane = pane;952list_addtail(&gr->head, &pane->graph_list);953pane->num_graphs++;954pane->next_color++;955}956957void958hud_graph_add_value(struct hud_graph *gr, double value)959{960gr->current_value = value;961value = value > gr->pane->ceiling ? gr->pane->ceiling : value;962963if (gr->fd) {964if (fabs(value - lround(value)) > FLT_EPSILON) {965fprintf(gr->fd, "%f\n", value);966}967else {968fprintf(gr->fd, "%" PRIu64 "\n", (uint64_t) lround(value));969}970}971972if (gr->index == gr->pane->max_num_vertices) {973gr->vertices[0] = 0;974gr->vertices[1] = gr->vertices[(gr->index-1)*2+1];975gr->index = 1;976}977gr->vertices[(gr->index)*2+0] = (float) (gr->index * 2);978gr->vertices[(gr->index)*2+1] = (float) value;979gr->index++;980981if (gr->num_vertices < gr->pane->max_num_vertices) {982gr->num_vertices++;983}984985if (gr->pane->dyn_ceiling == true) {986hud_pane_update_dyn_ceiling(gr, gr->pane);987}988if (value > gr->pane->max_value) {989hud_pane_set_max_value(gr->pane, value);990}991}992993static void994hud_graph_destroy(struct hud_graph *graph, struct pipe_context *pipe)995{996FREE(graph->vertices);997if (graph->free_query_data)998graph->free_query_data(graph->query_data, pipe);999if (graph->fd)1000fclose(graph->fd);1001FREE(graph);1002}10031004static void strcat_without_spaces(char *dst, const char *src)1005{1006dst += strlen(dst);1007while (*src) {1008if (*src == ' ')1009*dst++ = '_';1010else1011*dst++ = *src;1012src++;1013}1014*dst = 0;1015}101610171018#ifdef PIPE_OS_WINDOWS1019#define W_OK 01020static int1021access(const char *pathname, int mode)1022{1023/* no-op */1024return 0;1025}10261027#define PATH_SEP "\\"10281029#else10301031#define PATH_SEP "/"10321033#endif103410351036/**1037* If the GALLIUM_HUD_DUMP_DIR env var is set, we'll write the raw1038* HUD values to files at ${GALLIUM_HUD_DUMP_DIR}/<stat> where <stat>1039* is a HUD variable such as "fps", or "cpu"1040*/1041static void1042hud_graph_set_dump_file(struct hud_graph *gr)1043{1044const char *hud_dump_dir = getenv("GALLIUM_HUD_DUMP_DIR");10451046if (hud_dump_dir && access(hud_dump_dir, W_OK) == 0) {1047char *dump_file = malloc(strlen(hud_dump_dir) + sizeof(PATH_SEP)1048+ sizeof(gr->name));1049if (dump_file) {1050strcpy(dump_file, hud_dump_dir);1051strcat(dump_file, PATH_SEP);1052strcat_without_spaces(dump_file, gr->name);1053gr->fd = fopen(dump_file, "w+");1054if (gr->fd) {1055/* flush output after each line is written */1056setvbuf(gr->fd, NULL, _IOLBF, 0);1057}1058free(dump_file);1059}1060}1061}10621063/**1064* Read a string from the environment variable.1065* The separators "+", ",", ":", and ";" terminate the string.1066* Return the number of read characters.1067*/1068static int1069parse_string(const char *s, char *out)1070{1071int i;10721073for (i = 0; *s && *s != '+' && *s != ',' && *s != ':' && *s != ';' && *s != '=';1074s++, out++, i++)1075*out = *s;10761077*out = 0;10781079if (*s && !i) {1080fprintf(stderr, "gallium_hud: syntax error: unexpected '%c' (%i) while "1081"parsing a string\n", *s, *s);1082fflush(stderr);1083}10841085return i;1086}10871088static char *1089read_pane_settings(char *str, unsigned * const x, unsigned * const y,1090unsigned * const width, unsigned * const height,1091uint64_t * const ceiling, boolean * const dyn_ceiling,1092boolean *reset_colors, boolean *sort_items)1093{1094char *ret = str;1095unsigned tmp;10961097while (*str == '.') {1098++str;1099switch (*str) {1100case 'x':1101++str;1102*x = strtoul(str, &ret, 10);1103str = ret;1104break;11051106case 'y':1107++str;1108*y = strtoul(str, &ret, 10);1109str = ret;1110break;11111112case 'w':1113++str;1114tmp = strtoul(str, &ret, 10);1115*width = tmp > 80 ? tmp : 80; /* 80 is chosen arbitrarily */1116str = ret;1117break;11181119/*1120* Prevent setting height to less than 50. If the height is set to less,1121* the text of the Y axis labels on the graph will start overlapping.1122*/1123case 'h':1124++str;1125tmp = strtoul(str, &ret, 10);1126*height = tmp > 50 ? tmp : 50;1127str = ret;1128break;11291130case 'c':1131++str;1132tmp = strtoul(str, &ret, 10);1133*ceiling = tmp > 10 ? tmp : 10;1134str = ret;1135break;11361137case 'd':1138++str;1139ret = str;1140*dyn_ceiling = true;1141break;11421143case 'r':1144++str;1145ret = str;1146*reset_colors = true;1147break;11481149case 's':1150++str;1151ret = str;1152*sort_items = true;1153break;11541155default:1156fprintf(stderr, "gallium_hud: syntax error: unexpected '%c'\n", *str);1157fflush(stderr);1158}11591160}11611162return ret;1163}11641165static boolean1166has_occlusion_query(struct pipe_screen *screen)1167{1168return screen->get_param(screen, PIPE_CAP_OCCLUSION_QUERY) != 0;1169}11701171static boolean1172has_streamout(struct pipe_screen *screen)1173{1174return screen->get_param(screen, PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS) != 0;1175}11761177static boolean1178has_pipeline_stats_query(struct pipe_screen *screen)1179{1180return screen->get_param(screen, PIPE_CAP_QUERY_PIPELINE_STATISTICS) != 0;1181}11821183static void1184hud_parse_env_var(struct hud_context *hud, struct pipe_screen *screen,1185const char *env)1186{1187unsigned num, i;1188char name_a[256], s[256];1189char *name;1190struct hud_pane *pane = NULL;1191unsigned x = 10, y = 10, y_simple = 10;1192unsigned width = 251, height = 100;1193unsigned period = 500 * 1000; /* default period (1/2 second) */1194uint64_t ceiling = UINT64_MAX;1195unsigned column_width = 251;1196boolean dyn_ceiling = false;1197boolean reset_colors = false;1198boolean sort_items = false;1199const char *period_env;12001201if (strncmp(env, "simple,", 7) == 0) {1202hud->simple = true;1203env += 7;1204}12051206/*1207* The GALLIUM_HUD_PERIOD env var sets the graph update rate.1208* The env var is in seconds (a float).1209* Zero means update after every frame.1210*/1211period_env = getenv("GALLIUM_HUD_PERIOD");1212if (period_env) {1213float p = (float) atof(period_env);1214if (p >= 0.0f) {1215period = (unsigned) (p * 1000 * 1000);1216}1217}12181219while ((num = parse_string(env, name_a)) != 0) {1220bool added = true;12211222env += num;12231224/* check for explicit location, size and etc. settings */1225name = read_pane_settings(name_a, &x, &y, &width, &height, &ceiling,1226&dyn_ceiling, &reset_colors, &sort_items);12271228/*1229* Keep track of overall column width to avoid pane overlapping in case1230* later we create a new column while the bottom pane in the current1231* column is less wide than the rest of the panes in it.1232*/1233column_width = width > column_width ? width : column_width;12341235if (!pane) {1236pane = hud_pane_create(hud, x, y, x + width, y + height, y_simple,1237period, 10, ceiling, dyn_ceiling, sort_items);1238if (!pane)1239return;1240}12411242if (reset_colors) {1243pane->next_color = 0;1244reset_colors = false;1245}12461247/* Add a graph. */1248#if defined(HAVE_GALLIUM_EXTRA_HUD) || defined(HAVE_LIBSENSORS)1249char arg_name[64];1250#endif1251/* IF YOU CHANGE THIS, UPDATE print_help! */1252if (strcmp(name, "fps") == 0) {1253hud_fps_graph_install(pane);1254}1255else if (strcmp(name, "frametime") == 0) {1256hud_frametime_graph_install(pane);1257}1258else if (strcmp(name, "cpu") == 0) {1259hud_cpu_graph_install(pane, ALL_CPUS);1260}1261else if (sscanf(name, "cpu%u%s", &i, s) == 1) {1262hud_cpu_graph_install(pane, i);1263}1264else if (strcmp(name, "API-thread-busy") == 0) {1265hud_thread_busy_install(pane, name, false);1266}1267else if (strcmp(name, "API-thread-offloaded-slots") == 0) {1268hud_thread_counter_install(pane, name, HUD_COUNTER_OFFLOADED);1269}1270else if (strcmp(name, "API-thread-direct-slots") == 0) {1271hud_thread_counter_install(pane, name, HUD_COUNTER_DIRECT);1272}1273else if (strcmp(name, "API-thread-num-syncs") == 0) {1274hud_thread_counter_install(pane, name, HUD_COUNTER_SYNCS);1275}1276else if (strcmp(name, "main-thread-busy") == 0) {1277hud_thread_busy_install(pane, name, true);1278}1279#ifdef HAVE_GALLIUM_EXTRA_HUD1280else if (sscanf(name, "nic-rx-%s", arg_name) == 1) {1281hud_nic_graph_install(pane, arg_name, NIC_DIRECTION_RX);1282}1283else if (sscanf(name, "nic-tx-%s", arg_name) == 1) {1284hud_nic_graph_install(pane, arg_name, NIC_DIRECTION_TX);1285}1286else if (sscanf(name, "nic-rssi-%s", arg_name) == 1) {1287hud_nic_graph_install(pane, arg_name, NIC_RSSI_DBM);1288pane->type = PIPE_DRIVER_QUERY_TYPE_DBM;1289}1290else if (sscanf(name, "diskstat-rd-%s", arg_name) == 1) {1291hud_diskstat_graph_install(pane, arg_name, DISKSTAT_RD);1292pane->type = PIPE_DRIVER_QUERY_TYPE_BYTES;1293}1294else if (sscanf(name, "diskstat-wr-%s", arg_name) == 1) {1295hud_diskstat_graph_install(pane, arg_name, DISKSTAT_WR);1296pane->type = PIPE_DRIVER_QUERY_TYPE_BYTES;1297}1298else if (sscanf(name, "cpufreq-min-cpu%u", &i) == 1) {1299hud_cpufreq_graph_install(pane, i, CPUFREQ_MINIMUM);1300pane->type = PIPE_DRIVER_QUERY_TYPE_HZ;1301}1302else if (sscanf(name, "cpufreq-cur-cpu%u", &i) == 1) {1303hud_cpufreq_graph_install(pane, i, CPUFREQ_CURRENT);1304pane->type = PIPE_DRIVER_QUERY_TYPE_HZ;1305}1306else if (sscanf(name, "cpufreq-max-cpu%u", &i) == 1) {1307hud_cpufreq_graph_install(pane, i, CPUFREQ_MAXIMUM);1308pane->type = PIPE_DRIVER_QUERY_TYPE_HZ;1309}1310#endif1311#ifdef HAVE_LIBSENSORS1312else if (sscanf(name, "sensors_temp_cu-%s", arg_name) == 1) {1313hud_sensors_temp_graph_install(pane, arg_name,1314SENSORS_TEMP_CURRENT);1315pane->type = PIPE_DRIVER_QUERY_TYPE_TEMPERATURE;1316}1317else if (sscanf(name, "sensors_temp_cr-%s", arg_name) == 1) {1318hud_sensors_temp_graph_install(pane, arg_name,1319SENSORS_TEMP_CRITICAL);1320pane->type = PIPE_DRIVER_QUERY_TYPE_TEMPERATURE;1321}1322else if (sscanf(name, "sensors_volt_cu-%s", arg_name) == 1) {1323hud_sensors_temp_graph_install(pane, arg_name,1324SENSORS_VOLTAGE_CURRENT);1325pane->type = PIPE_DRIVER_QUERY_TYPE_VOLTS;1326}1327else if (sscanf(name, "sensors_curr_cu-%s", arg_name) == 1) {1328hud_sensors_temp_graph_install(pane, arg_name,1329SENSORS_CURRENT_CURRENT);1330pane->type = PIPE_DRIVER_QUERY_TYPE_AMPS;1331}1332else if (sscanf(name, "sensors_pow_cu-%s", arg_name) == 1) {1333hud_sensors_temp_graph_install(pane, arg_name,1334SENSORS_POWER_CURRENT);1335pane->type = PIPE_DRIVER_QUERY_TYPE_WATTS;1336}1337#endif1338else if (strcmp(name, "samples-passed") == 0 &&1339has_occlusion_query(screen)) {1340hud_pipe_query_install(&hud->batch_query, pane,1341"samples-passed",1342PIPE_QUERY_OCCLUSION_COUNTER, 0, 0,1343PIPE_DRIVER_QUERY_TYPE_UINT64,1344PIPE_DRIVER_QUERY_RESULT_TYPE_AVERAGE,13450);1346}1347else if (strcmp(name, "primitives-generated") == 0 &&1348has_streamout(screen)) {1349hud_pipe_query_install(&hud->batch_query, pane,1350"primitives-generated",1351PIPE_QUERY_PRIMITIVES_GENERATED, 0, 0,1352PIPE_DRIVER_QUERY_TYPE_UINT64,1353PIPE_DRIVER_QUERY_RESULT_TYPE_AVERAGE,13540);1355}1356else {1357boolean processed = FALSE;13581359/* pipeline statistics queries */1360if (has_pipeline_stats_query(screen)) {1361static const char *pipeline_statistics_names[] =1362{1363"ia-vertices",1364"ia-primitives",1365"vs-invocations",1366"gs-invocations",1367"gs-primitives",1368"clipper-invocations",1369"clipper-primitives-generated",1370"ps-invocations",1371"hs-invocations",1372"ds-invocations",1373"cs-invocations"1374};1375for (i = 0; i < ARRAY_SIZE(pipeline_statistics_names); ++i)1376if (strcmp(name, pipeline_statistics_names[i]) == 0)1377break;1378if (i < ARRAY_SIZE(pipeline_statistics_names)) {1379hud_pipe_query_install(&hud->batch_query, pane, name,1380PIPE_QUERY_PIPELINE_STATISTICS, i,13810, PIPE_DRIVER_QUERY_TYPE_UINT64,1382PIPE_DRIVER_QUERY_RESULT_TYPE_AVERAGE,13830);1384processed = TRUE;1385}1386}13871388/* driver queries */1389if (!processed) {1390if (!hud_driver_query_install(&hud->batch_query, pane,1391screen, name)) {1392fprintf(stderr, "gallium_hud: unknown driver query '%s'\n", name);1393fflush(stderr);1394added = false;1395}1396}1397}13981399if (*env == ':') {1400env++;14011402if (!pane) {1403fprintf(stderr, "gallium_hud: syntax error: unexpected ':', "1404"expected a name\n");1405fflush(stderr);1406break;1407}14081409num = parse_string(env, s);1410env += num;14111412if (num && sscanf(s, "%u", &i) == 1) {1413hud_pane_set_max_value(pane, i);1414pane->initial_max_value = i;1415}1416else {1417fprintf(stderr, "gallium_hud: syntax error: unexpected '%c' (%i) "1418"after ':'\n", *env, *env);1419fflush(stderr);1420}1421}14221423if (*env == '=') {1424env++;14251426if (!pane) {1427fprintf(stderr, "gallium_hud: syntax error: unexpected '=', "1428"expected a name\n");1429fflush(stderr);1430break;1431}14321433num = parse_string(env, s);1434env += num;14351436strip_hyphens(s);1437if (added && !list_is_empty(&pane->graph_list)) {1438struct hud_graph *graph;1439graph = LIST_ENTRY(struct hud_graph, pane->graph_list.prev, head);1440strncpy(graph->name, s, sizeof(graph->name)-1);1441graph->name[sizeof(graph->name)-1] = 0;1442}1443}14441445if (*env == 0)1446break;14471448/* parse a separator */1449switch (*env) {1450case '+':1451env++;1452break;14531454case ',':1455env++;1456if (!pane)1457break;14581459y += height + hud->font.glyph_height * (pane->num_graphs + 2);1460y_simple += hud->font.glyph_height * (pane->num_graphs + 1);1461height = 100;14621463if (pane && pane->num_graphs) {1464list_addtail(&pane->head, &hud->pane_list);1465pane = NULL;1466}1467break;14681469case ';':1470env++;1471y = 10;1472y_simple = 10;1473x += column_width + hud->font.glyph_width * 9;1474height = 100;14751476if (pane && pane->num_graphs) {1477list_addtail(&pane->head, &hud->pane_list);1478pane = NULL;1479}14801481/* Starting a new column; reset column width. */1482column_width = 251;1483break;14841485default:1486fprintf(stderr, "gallium_hud: syntax error: unexpected '%c'\n", *env);1487fflush(stderr);1488}14891490/* Reset to defaults for the next pane in case these were modified. */1491width = 251;1492ceiling = UINT64_MAX;1493dyn_ceiling = false;1494sort_items = false;14951496}14971498if (pane) {1499if (pane->num_graphs) {1500list_addtail(&pane->head, &hud->pane_list);1501}1502else {1503FREE(pane);1504}1505}15061507LIST_FOR_EACH_ENTRY(pane, &hud->pane_list, head) {1508struct hud_graph *gr;15091510LIST_FOR_EACH_ENTRY(gr, &pane->graph_list, head) {1511hud_graph_set_dump_file(gr);1512}1513}1514}15151516static void1517print_help(struct pipe_screen *screen)1518{1519int i, num_queries, num_cpus = hud_get_num_cpus();15201521puts("Syntax: GALLIUM_HUD=name1[+name2][...][:value1][,nameI...][;nameJ...]");1522puts("");1523puts(" Names are identifiers of data sources which will be drawn as graphs");1524puts(" in panes. Multiple graphs can be drawn in the same pane.");1525puts(" There can be multiple panes placed in rows and columns.");1526puts("");1527puts(" '+' separates names which will share a pane.");1528puts(" ':[value]' specifies the initial maximum value of the Y axis");1529puts(" for the given pane.");1530puts(" ',' creates a new pane below the last one.");1531puts(" ';' creates a new pane at the top of the next column.");1532puts(" '=' followed by a string, changes the name of the last data source");1533puts(" to that string");1534puts("");1535puts(" Example: GALLIUM_HUD=\"cpu,fps;primitives-generated\"");1536puts("");1537puts(" Additionally, by prepending '.[identifier][value]' modifiers to");1538puts(" a name, it is possible to explicitly set the location and size");1539puts(" of a pane, along with limiting overall maximum value of the");1540puts(" Y axis and activating dynamic readjustment of the Y axis.");1541puts(" Several modifiers may be applied to the same pane simultaneously.");1542puts("");1543puts(" 'x[value]' sets the location of the pane on the x axis relative");1544puts(" to the upper-left corner of the viewport, in pixels.");1545puts(" 'y[value]' sets the location of the pane on the y axis relative");1546puts(" to the upper-left corner of the viewport, in pixels.");1547puts(" 'w[value]' sets width of the graph pixels.");1548puts(" 'h[value]' sets height of the graph in pixels.");1549puts(" 'c[value]' sets the ceiling of the value of the Y axis.");1550puts(" If the graph needs to draw values higher than");1551puts(" the ceiling allows, the value is clamped.");1552puts(" 'd' activates dynamic Y axis readjustment to set the value of");1553puts(" the Y axis to match the highest value still visible in the graph.");1554puts(" 'r' resets the color counter (the next color will be green)");1555puts(" 's' sort items below graphs in descending order");1556puts("");1557puts(" If 'c' and 'd' modifiers are used simultaneously, both are in effect:");1558puts(" the Y axis does not go above the restriction imposed by 'c' while");1559puts(" still adjusting the value of the Y axis down when appropriate.");1560puts("");1561puts(" You can change behavior of the whole HUD by adding these options at");1562puts(" the beginning of the environment variable:");1563puts(" 'simple,' disables all the fancy stuff and only draws text.");1564puts("");1565puts(" Example: GALLIUM_HUD=\".w256.h64.x1600.y520.d.c1000fps+cpu,.datom-count\"");1566puts("");1567puts(" Available names:");1568puts(" fps");1569puts(" frametime");1570puts(" cpu");15711572for (i = 0; i < num_cpus; i++)1573printf(" cpu%i\n", i);15741575if (has_occlusion_query(screen))1576puts(" samples-passed");1577if (has_streamout(screen))1578puts(" primitives-generated");15791580if (has_pipeline_stats_query(screen)) {1581puts(" ia-vertices");1582puts(" ia-primitives");1583puts(" vs-invocations");1584puts(" gs-invocations");1585puts(" gs-primitives");1586puts(" clipper-invocations");1587puts(" clipper-primitives-generated");1588puts(" ps-invocations");1589puts(" hs-invocations");1590puts(" ds-invocations");1591puts(" cs-invocations");1592}15931594#ifdef HAVE_GALLIUM_EXTRA_HUD1595hud_get_num_disks(1);1596hud_get_num_nics(1);1597hud_get_num_cpufreq(1);1598#endif1599#ifdef HAVE_LIBSENSORS1600hud_get_num_sensors(1);1601#endif16021603if (screen->get_driver_query_info){1604boolean skipping = false;1605struct pipe_driver_query_info info;1606num_queries = screen->get_driver_query_info(screen, 0, NULL);16071608for (i = 0; i < num_queries; i++){1609screen->get_driver_query_info(screen, i, &info);1610if (info.flags & PIPE_DRIVER_QUERY_FLAG_DONT_LIST) {1611if (!skipping)1612puts(" ...");1613skipping = true;1614} else {1615printf(" %s\n", info.name);1616skipping = false;1617}1618}1619}16201621puts("");1622fflush(stdout);1623}16241625static void1626hud_unset_draw_context(struct hud_context *hud)1627{1628struct pipe_context *pipe = hud->pipe;16291630if (!pipe)1631return;16321633pipe_sampler_view_reference(&hud->font_sampler_view, NULL);16341635if (hud->fs_color) {1636pipe->delete_fs_state(pipe, hud->fs_color);1637hud->fs_color = NULL;1638}1639if (hud->fs_text) {1640pipe->delete_fs_state(pipe, hud->fs_text);1641hud->fs_text = NULL;1642}1643if (hud->vs_color) {1644pipe->delete_vs_state(pipe, hud->vs_color);1645hud->vs_color = NULL;1646}1647if (hud->vs_text) {1648pipe->delete_vs_state(pipe, hud->vs_text);1649hud->vs_text = NULL;1650}16511652hud->cso = NULL;1653hud->pipe = NULL;1654}16551656static bool1657hud_set_draw_context(struct hud_context *hud, struct cso_context *cso,1658struct st_context_iface *st)1659{1660struct pipe_context *pipe = cso_get_pipe_context(cso);16611662assert(!hud->pipe);1663hud->pipe = pipe;1664hud->cso = cso;1665hud->st = st;16661667struct pipe_sampler_view view_templ;1668u_sampler_view_default_template(1669&view_templ, hud->font.texture, hud->font.texture->format);1670hud->font_sampler_view = pipe->create_sampler_view(pipe, hud->font.texture,1671&view_templ);1672if (!hud->font_sampler_view)1673goto fail;16741675/* color fragment shader */1676hud->fs_color =1677util_make_fragment_passthrough_shader(pipe,1678TGSI_SEMANTIC_COLOR,1679TGSI_INTERPOLATE_CONSTANT,1680TRUE);16811682/* text fragment shader */1683{1684/* Read a texture and do .xxxx swizzling. */1685static const char *fragment_shader_text = {1686"FRAG\n"1687"DCL IN[0], GENERIC[0], LINEAR\n"1688"DCL SAMP[0]\n"1689"DCL SVIEW[0], RECT, FLOAT\n"1690"DCL OUT[0], COLOR[0]\n"1691"DCL TEMP[0]\n"16921693"TEX TEMP[0], IN[0], SAMP[0], RECT\n"1694"MOV OUT[0], TEMP[0].xxxx\n"1695"END\n"1696};16971698struct tgsi_token tokens[1000];1699struct pipe_shader_state state = {0};17001701if (!tgsi_text_translate(fragment_shader_text, tokens, ARRAY_SIZE(tokens))) {1702assert(0);1703goto fail;1704}1705pipe_shader_state_from_tgsi(&state, tokens);1706hud->fs_text = pipe->create_fs_state(pipe, &state);1707}17081709/* color vertex shader */1710{1711static const char *vertex_shader_text = {1712"VERT\n"1713"DCL IN[0..1]\n"1714"DCL OUT[0], POSITION\n"1715"DCL OUT[1], COLOR[0]\n" /* color */1716"DCL OUT[2], GENERIC[0]\n" /* texcoord */1717/* [0] = color,1718* [1] = (2/fb_width, 2/fb_height, xoffset, yoffset)1719* [2] = (xscale, yscale, 0, 0) */1720"DCL CONST[0][0..2]\n"1721"DCL TEMP[0]\n"1722"IMM[0] FLT32 { -1, 0, 0, 1 }\n"17231724/* v = in * (xscale, yscale) + (xoffset, yoffset) */1725"MAD TEMP[0].xy, IN[0], CONST[0][2].xyyy, CONST[0][1].zwww\n"1726/* pos = v * (2 / fb_width, 2 / fb_height) - (1, 1) */1727"MAD OUT[0].xy, TEMP[0], CONST[0][1].xyyy, IMM[0].xxxx\n"1728"MOV OUT[0].zw, IMM[0]\n"17291730"MOV OUT[1], CONST[0][0]\n"1731"MOV OUT[2], IN[1]\n"1732"END\n"1733};17341735struct tgsi_token tokens[1000];1736struct pipe_shader_state state = {0};1737if (!tgsi_text_translate(vertex_shader_text, tokens, ARRAY_SIZE(tokens))) {1738assert(0);1739goto fail;1740}1741pipe_shader_state_from_tgsi(&state, tokens);1742hud->vs_color = pipe->create_vs_state(pipe, &state);1743}17441745/* text vertex shader */1746{1747/* similar to the above, without the color component1748* to match the varyings in fs_text */1749static const char *vertex_shader_text = {1750"VERT\n"1751"DCL IN[0..1]\n"1752"DCL OUT[0], POSITION\n"1753"DCL OUT[1], GENERIC[0]\n" /* texcoord */1754/* [0] = color,1755* [1] = (2/fb_width, 2/fb_height, xoffset, yoffset)1756* [2] = (xscale, yscale, 0, 0) */1757"DCL CONST[0][0..2]\n"1758"DCL TEMP[0]\n"1759"IMM[0] FLT32 { -1, 0, 0, 1 }\n"17601761/* v = in * (xscale, yscale) + (xoffset, yoffset) */1762"MAD TEMP[0].xy, IN[0], CONST[0][2].xyyy, CONST[0][1].zwww\n"1763/* pos = v * (2 / fb_width, 2 / fb_height) - (1, 1) */1764"MAD OUT[0].xy, TEMP[0], CONST[0][1].xyyy, IMM[0].xxxx\n"1765"MOV OUT[0].zw, IMM[0]\n"17661767"MOV OUT[1], IN[1]\n"1768"END\n"1769};17701771struct tgsi_token tokens[1000];1772struct pipe_shader_state state = {0};1773if (!tgsi_text_translate(vertex_shader_text, tokens, ARRAY_SIZE(tokens))) {1774assert(0);1775goto fail;1776}1777pipe_shader_state_from_tgsi(&state, tokens);1778hud->vs_text = pipe->create_vs_state(pipe, &state);1779}17801781return true;17821783fail:1784hud_unset_draw_context(hud);1785fprintf(stderr, "hud: failed to set a draw context");1786return false;1787}17881789static void1790hud_unset_record_context(struct hud_context *hud)1791{1792struct pipe_context *pipe = hud->record_pipe;1793struct hud_pane *pane, *pane_tmp;1794struct hud_graph *graph, *graph_tmp;17951796if (!pipe)1797return;17981799LIST_FOR_EACH_ENTRY_SAFE(pane, pane_tmp, &hud->pane_list, head) {1800LIST_FOR_EACH_ENTRY_SAFE(graph, graph_tmp, &pane->graph_list, head) {1801list_del(&graph->head);1802hud_graph_destroy(graph, pipe);1803}1804list_del(&pane->head);1805FREE(pane);1806}18071808hud_batch_query_cleanup(&hud->batch_query, pipe);1809hud->record_pipe = NULL;1810}18111812static void1813hud_set_record_context(struct hud_context *hud, struct pipe_context *pipe)1814{1815hud->record_pipe = pipe;1816}18171818/**1819* Create the HUD.1820*1821* If "share" is non-NULL and GALLIUM_HUD_SHARE=x,y is set, increment the1822* reference counter of "share", set "cso" as the recording or drawing context1823* according to the environment variable, and return "share".1824* This allows sharing the HUD instance within a multi-context share group,1825* record queries in one context and draw them in another.1826*/1827struct hud_context *1828hud_create(struct cso_context *cso, struct st_context_iface *st,1829struct hud_context *share)1830{1831const char *share_env = debug_get_option("GALLIUM_HUD_SHARE", NULL);1832unsigned record_ctx = 0, draw_ctx = 0;18331834if (share_env && sscanf(share_env, "%u,%u", &record_ctx, &draw_ctx) != 2)1835share_env = NULL;18361837if (share && share_env) {1838/* All contexts in a share group share the HUD instance.1839* Only one context can record queries and only one context1840* can draw the HUD.1841*1842* GALLIUM_HUD_SHARE=x,y determines the context indices.1843*/1844int context_id = p_atomic_inc_return(&share->refcount) - 1;18451846if (context_id == record_ctx) {1847assert(!share->record_pipe);1848hud_set_record_context(share, cso_get_pipe_context(cso));1849}18501851if (context_id == draw_ctx) {1852assert(!share->pipe);1853hud_set_draw_context(share, cso, st);1854}18551856return share;1857}18581859struct pipe_screen *screen = cso_get_pipe_context(cso)->screen;1860struct hud_context *hud;1861unsigned i;1862const char *env = debug_get_option("GALLIUM_HUD", NULL);1863#ifdef PIPE_OS_UNIX1864unsigned signo = debug_get_num_option("GALLIUM_HUD_TOGGLE_SIGNAL", 0);1865static boolean sig_handled = FALSE;1866struct sigaction action;18671868memset(&action, 0, sizeof(action));1869#endif1870huds_visible = debug_get_bool_option("GALLIUM_HUD_VISIBLE", TRUE);1871hud_scale = debug_get_num_option("GALLIUM_HUD_SCALE", 1);18721873if (!env || !*env)1874return NULL;18751876if (strcmp(env, "help") == 0) {1877print_help(screen);1878return NULL;1879}18801881hud = CALLOC_STRUCT(hud_context);1882if (!hud)1883return NULL;18841885/* font (the context is only used for the texture upload) */1886if (!util_font_create(cso_get_pipe_context(cso),1887UTIL_FONT_FIXED_8X13, &hud->font)) {1888FREE(hud);1889return NULL;1890}18911892hud->refcount = 1;18931894static const enum pipe_format srgb_formats[] = {1895PIPE_FORMAT_B8G8R8A8_SRGB,1896PIPE_FORMAT_B8G8R8X8_SRGB1897};1898for (i = 0; i < ARRAY_SIZE(srgb_formats); i++) {1899if (!screen->is_format_supported(screen, srgb_formats[i],1900PIPE_TEXTURE_2D, 0, 0,1901PIPE_BIND_RENDER_TARGET))1902break;1903}19041905hud->has_srgb = (i == ARRAY_SIZE(srgb_formats));19061907/* blend state */1908hud->no_blend.rt[0].colormask = PIPE_MASK_RGBA;19091910hud->alpha_blend.rt[0].colormask = PIPE_MASK_RGBA;1911hud->alpha_blend.rt[0].blend_enable = 1;1912hud->alpha_blend.rt[0].rgb_func = PIPE_BLEND_ADD;1913hud->alpha_blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_SRC_ALPHA;1914hud->alpha_blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_INV_SRC_ALPHA;1915hud->alpha_blend.rt[0].alpha_func = PIPE_BLEND_ADD;1916hud->alpha_blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ZERO;1917hud->alpha_blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ONE;19181919/* rasterizer */1920hud->rasterizer.half_pixel_center = 1;1921hud->rasterizer.bottom_edge_rule = 1;1922hud->rasterizer.depth_clip_near = 1;1923hud->rasterizer.depth_clip_far = 1;1924hud->rasterizer.line_width = 1;1925hud->rasterizer.line_last_pixel = 1;19261927hud->rasterizer_aa_lines = hud->rasterizer;1928hud->rasterizer_aa_lines.line_smooth = 1;19291930/* vertex elements */1931hud->velems.count = 2;1932for (i = 0; i < 2; i++) {1933hud->velems.velems[i].src_offset = i * 2 * sizeof(float);1934hud->velems.velems[i].src_format = PIPE_FORMAT_R32G32_FLOAT;1935hud->velems.velems[i].vertex_buffer_index = 0;1936}19371938/* sampler state (for font drawing) */1939hud->font_sampler_state.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;1940hud->font_sampler_state.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;1941hud->font_sampler_state.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;1942hud->font_sampler_state.normalized_coords = 0;19431944/* constants */1945hud->constbuf.buffer_size = sizeof(hud->constants);1946hud->constbuf.user_buffer = &hud->constants;19471948list_inithead(&hud->pane_list);19491950/* setup sig handler once for all hud contexts */1951#ifdef PIPE_OS_UNIX1952if (!sig_handled && signo != 0) {1953action.sa_sigaction = &signal_visible_handler;1954action.sa_flags = SA_SIGINFO;19551956if (signo >= NSIG)1957fprintf(stderr, "gallium_hud: invalid signal %u\n", signo);1958else if (sigaction(signo, &action, NULL) < 0)1959fprintf(stderr, "gallium_hud: unable to set handler for signal %u\n", signo);1960fflush(stderr);19611962sig_handled = TRUE;1963}1964#endif19651966if (record_ctx == 0)1967hud_set_record_context(hud, cso_get_pipe_context(cso));1968if (draw_ctx == 0)1969hud_set_draw_context(hud, cso, st);19701971hud_parse_env_var(hud, screen, env);1972return hud;1973}19741975/**1976* Destroy a HUD. If the HUD has several users, decrease the reference counter1977* and detach the context from the HUD.1978*/1979void1980hud_destroy(struct hud_context *hud, struct cso_context *cso)1981{1982if (!cso || hud->record_pipe == cso_get_pipe_context(cso))1983hud_unset_record_context(hud);19841985if (!cso || hud->cso == cso)1986hud_unset_draw_context(hud);19871988if (p_atomic_dec_zero(&hud->refcount)) {1989pipe_resource_reference(&hud->font.texture, NULL);1990FREE(hud);1991}1992}19931994void1995hud_add_queue_for_monitoring(struct hud_context *hud,1996struct util_queue_monitoring *queue_info)1997{1998assert(!hud->monitored_queue);1999hud->monitored_queue = queue_info;2000}200120022003