+static void
+hud_start_queries(struct hud_context *hud, struct pipe_context *pipe)
+{
+ struct hud_pane *pane;
+ struct hud_graph *gr;
+
+ /* Start queries. */
+ hud_batch_query_begin(hud->batch_query, pipe);
+
+ LIST_FOR_EACH_ENTRY(pane, &hud->pane_list, head) {
+ LIST_FOR_EACH_ENTRY(gr, &pane->graph_list, head) {
+ if (gr->begin_query)
+ gr->begin_query(gr, pipe);
+ }
+ }
+}
+
+/* Stop queries, query results, and record vertices for charts. */
+static void
+hud_stop_queries(struct hud_context *hud, struct pipe_context *pipe)
+{
+ struct hud_pane *pane;
+ struct hud_graph *gr, *next;
+
+ /* prepare vertex buffers */
+ hud_prepare_vertices(hud, &hud->bg, 16 * 256, 2 * sizeof(float));
+ hud_prepare_vertices(hud, &hud->whitelines, 4 * 256, 2 * sizeof(float));
+ hud_prepare_vertices(hud, &hud->text, 16 * 1024, 4 * sizeof(float));
+ hud_prepare_vertices(hud, &hud->color_prims, 32 * 1024, 2 * sizeof(float));
+
+ /* Allocate everything once and divide the storage into 3 portions
+ * manually, because u_upload_alloc can unmap memory from previous calls.
+ */
+ u_upload_alloc(pipe->stream_uploader, 0,
+ hud->bg.buffer_size +
+ hud->whitelines.buffer_size +
+ hud->text.buffer_size +
+ hud->color_prims.buffer_size,
+ 16, &hud->bg.vbuf.buffer_offset, &hud->bg.vbuf.buffer.resource,
+ (void**)&hud->bg.vertices);
+ if (!hud->bg.vertices)
+ return;
+
+ pipe_resource_reference(&hud->whitelines.vbuf.buffer.resource, hud->bg.vbuf.buffer.resource);
+ pipe_resource_reference(&hud->text.vbuf.buffer.resource, hud->bg.vbuf.buffer.resource);
+ pipe_resource_reference(&hud->color_prims.vbuf.buffer.resource, hud->bg.vbuf.buffer.resource);
+
+ hud->whitelines.vbuf.buffer_offset = hud->bg.vbuf.buffer_offset +
+ hud->bg.buffer_size;
+ hud->whitelines.vertices = hud->bg.vertices +
+ hud->bg.buffer_size / sizeof(float);
+
+ hud->text.vbuf.buffer_offset = hud->whitelines.vbuf.buffer_offset +
+ hud->whitelines.buffer_size;
+ hud->text.vertices = hud->whitelines.vertices +
+ hud->whitelines.buffer_size / sizeof(float);
+
+ hud->color_prims.vbuf.buffer_offset = hud->text.vbuf.buffer_offset +
+ hud->text.buffer_size;
+ hud->color_prims.vertices = hud->text.vertices +
+ hud->text.buffer_size / sizeof(float);
+
+ /* prepare all graphs */
+ hud_batch_query_update(hud->batch_query, pipe);
+
+ LIST_FOR_EACH_ENTRY(pane, &hud->pane_list, head) {
+ LIST_FOR_EACH_ENTRY(gr, &pane->graph_list, head) {
+ gr->query_new_value(gr, pipe);
+ }
+
+ if (pane->sort_items) {
+ LIST_FOR_EACH_ENTRY_SAFE(gr, next, &pane->graph_list, head) {
+ /* ignore the last one */
+ if (&gr->head == pane->graph_list.prev)
+ continue;
+
+ /* This is an incremental bubble sort, because we only do one pass
+ * per frame. It will eventually reach an equilibrium.
+ */
+ if (gr->current_value <
+ LIST_ENTRY(struct hud_graph, next, head)->current_value) {
+ list_del(&gr->head);
+ list_add(&gr->head, &next->head);
+ }
+ }
+ }
+
+ if (hud->simple)
+ hud_pane_accumulate_vertices_simple(hud, pane);
+ else
+ hud_pane_accumulate_vertices(hud, pane);
+ }
+
+ /* unmap the uploader's vertex buffer before drawing */
+ u_upload_unmap(pipe->stream_uploader);
+}
+
+/**
+ * Record queries and draw the HUD. The "cso" parameter acts as a filter.
+ * If "cso" is not the recording context, recording is skipped.
+ * If "cso" is not the drawing context, drawing is skipped.
+ * cso == NULL ignores the filter.
+ */
+void
+hud_run(struct hud_context *hud, struct cso_context *cso,
+ struct pipe_resource *tex)
+{
+ struct pipe_context *pipe = cso ? cso_get_pipe_context(cso) : NULL;
+
+ /* If "cso" is the recording or drawing context or NULL, execute
+ * the operation. Otherwise, don't do anything.
+ */
+ if (hud->record_pipe && (!pipe || pipe == hud->record_pipe))
+ hud_stop_queries(hud, hud->record_pipe);
+
+ if (hud->cso && (!cso || cso == hud->cso))
+ hud_draw_results(hud, tex);
+
+ if (hud->record_pipe && (!pipe || pipe == hud->record_pipe))
+ hud_start_queries(hud, hud->record_pipe);
+}
+
+/**
+ * Record query results and assemble vertices if "pipe" is a recording but
+ * not drawing context.
+ */
+void
+hud_record_only(struct hud_context *hud, struct pipe_context *pipe)
+{
+ assert(pipe);
+
+ /* If it's a drawing context, only hud_run() records query results. */
+ if (pipe == hud->pipe || pipe != hud->record_pipe)
+ return;
+
+ hud_stop_queries(hud, hud->record_pipe);
+ hud_start_queries(hud, hud->record_pipe);
+}
+