Merge remote-tracking branch 'mesa-public/master' into vulkan
[mesa.git] / src / gallium / auxiliary / hud / hud_driver_query.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 file contains code for reading values from pipe queries
29 * for displaying on the HUD. To prevent stalls when reading queries, we
30 * keep a list of busy queries in a ring. We read only those queries which
31 * are idle.
32 */
33
34 #include "hud/hud_private.h"
35 #include "pipe/p_screen.h"
36 #include "os/os_time.h"
37 #include "util/u_memory.h"
38 #include <stdio.h>
39
40 #define NUM_QUERIES 8
41
42 struct query_info {
43 struct pipe_context *pipe;
44 unsigned query_type;
45 unsigned result_index; /* unit depends on query_type */
46 enum pipe_driver_query_result_type result_type;
47
48 /* Ring of queries. If a query is busy, we use another slot. */
49 struct pipe_query *query[NUM_QUERIES];
50 unsigned head, tail;
51 unsigned num_queries;
52
53 uint64_t last_time;
54 uint64_t results_cumulative;
55 unsigned num_results;
56 };
57
58 static void
59 query_new_value(struct hud_graph *gr)
60 {
61 struct query_info *info = gr->query_data;
62 struct pipe_context *pipe = info->pipe;
63 uint64_t now = os_time_get();
64
65 if (info->last_time) {
66 if (info->query[info->head])
67 pipe->end_query(pipe, info->query[info->head]);
68
69 /* read query results */
70 while (1) {
71 struct pipe_query *query = info->query[info->tail];
72 union pipe_query_result result;
73 uint64_t *res64 = (uint64_t *)&result;
74
75 if (query && pipe->get_query_result(pipe, query, FALSE, &result)) {
76 info->results_cumulative += res64[info->result_index];
77 info->num_results++;
78
79 if (info->tail == info->head)
80 break;
81
82 info->tail = (info->tail+1) % NUM_QUERIES;
83 }
84 else {
85 /* the oldest query is busy */
86 if ((info->head+1) % NUM_QUERIES == info->tail) {
87 /* all queries are busy, throw away the last query and create
88 * a new one */
89 fprintf(stderr,
90 "gallium_hud: all queries are busy after %i frames, "
91 "can't add another query\n",
92 NUM_QUERIES);
93 if (info->query[info->head])
94 pipe->destroy_query(pipe, info->query[info->head]);
95 info->query[info->head] =
96 pipe->create_query(pipe, info->query_type, 0);
97 }
98 else {
99 /* the last query is busy, we need to add a new one we can use
100 * for this frame */
101 info->head = (info->head+1) % NUM_QUERIES;
102 if (!info->query[info->head]) {
103 info->query[info->head] =
104 pipe->create_query(pipe, info->query_type, 0);
105 }
106 }
107 break;
108 }
109 }
110
111 if (info->num_results && info->last_time + gr->pane->period <= now) {
112 uint64_t value;
113
114 switch (info->result_type) {
115 default:
116 case PIPE_DRIVER_QUERY_RESULT_TYPE_AVERAGE:
117 value = info->results_cumulative / info->num_results;
118 break;
119 case PIPE_DRIVER_QUERY_RESULT_TYPE_CUMULATIVE:
120 value = info->results_cumulative;
121 break;
122 }
123
124 hud_graph_add_value(gr, value);
125
126 info->last_time = now;
127 info->results_cumulative = 0;
128 info->num_results = 0;
129 }
130 }
131 else {
132 /* initialize */
133 info->last_time = now;
134 info->query[info->head] = pipe->create_query(pipe, info->query_type, 0);
135 }
136
137 if (info->query[info->head])
138 pipe->begin_query(pipe, info->query[info->head]);
139 }
140
141 static void
142 free_query_info(void *ptr)
143 {
144 struct query_info *info = ptr;
145
146 if (info->last_time) {
147 struct pipe_context *pipe = info->pipe;
148 int i;
149
150 pipe->end_query(pipe, info->query[info->head]);
151
152 for (i = 0; i < Elements(info->query); i++) {
153 if (info->query[i]) {
154 pipe->destroy_query(pipe, info->query[i]);
155 }
156 }
157 }
158 FREE(info);
159 }
160
161 void
162 hud_pipe_query_install(struct hud_pane *pane, struct pipe_context *pipe,
163 const char *name, unsigned query_type,
164 unsigned result_index,
165 uint64_t max_value, enum pipe_driver_query_type type,
166 enum pipe_driver_query_result_type result_type)
167 {
168 struct hud_graph *gr;
169 struct query_info *info;
170
171 gr = CALLOC_STRUCT(hud_graph);
172 if (!gr)
173 return;
174
175 strncpy(gr->name, name, sizeof(gr->name));
176 gr->name[sizeof(gr->name) - 1] = '\0';
177 gr->query_data = CALLOC_STRUCT(query_info);
178 if (!gr->query_data) {
179 FREE(gr);
180 return;
181 }
182
183 gr->query_new_value = query_new_value;
184 gr->free_query_data = free_query_info;
185
186 info = gr->query_data;
187 info->pipe = pipe;
188 info->query_type = query_type;
189 info->result_index = result_index;
190 info->result_type = result_type;
191
192 hud_pane_add_graph(pane, gr);
193 if (pane->max_value < max_value)
194 hud_pane_set_max_value(pane, max_value);
195 pane->type = type;
196 }
197
198 boolean
199 hud_driver_query_install(struct hud_pane *pane, struct pipe_context *pipe,
200 const char *name)
201 {
202 struct pipe_screen *screen = pipe->screen;
203 struct pipe_driver_query_info query;
204 unsigned num_queries, i;
205 boolean found = FALSE;
206
207 if (!screen->get_driver_query_info)
208 return FALSE;
209
210 num_queries = screen->get_driver_query_info(screen, 0, NULL);
211
212 for (i = 0; i < num_queries; i++) {
213 if (screen->get_driver_query_info(screen, i, &query) &&
214 strcmp(query.name, name) == 0) {
215 found = TRUE;
216 break;
217 }
218 }
219
220 if (!found)
221 return FALSE;
222
223 hud_pipe_query_install(pane, pipe, query.name, query.query_type, 0,
224 query.max_value.u64, query.type, query.result_type);
225
226 return TRUE;
227 }