Path: blob/21.2-virgl/src/gallium/auxiliary/hud/hud_fps.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 file contains code for calculating framerate for displaying on the HUD.28*/2930#include "hud/hud_private.h"31#include "util/os_time.h"32#include "util/u_memory.h"3334struct fps_info {35boolean frametime;36int frames;37uint64_t last_time;38};3940static void41query_fps(struct hud_graph *gr, struct pipe_context *pipe)42{43struct fps_info *info = gr->query_data;44uint64_t now = os_time_get();4546info->frames++;4748if (info->last_time) {49if (info->frametime) {50double frametime = ((double)now - (double)info->last_time) / 1000.0;51hud_graph_add_value(gr, frametime);52info->last_time = now;53}54else if (info->last_time + gr->pane->period <= now) {55double fps = ((uint64_t)info->frames) * 1000000 /56(double)(now - info->last_time);57info->frames = 0;58info->last_time = now;5960hud_graph_add_value(gr, fps);61}62}63else {64info->last_time = now;65}66}6768static void69free_query_data(void *p, struct pipe_context *pipe)70{71FREE(p);72}7374void75hud_fps_graph_install(struct hud_pane *pane)76{77struct hud_graph *gr = CALLOC_STRUCT(hud_graph);7879if (!gr)80return;8182strcpy(gr->name, "fps");83gr->query_data = CALLOC_STRUCT(fps_info);84if (!gr->query_data) {85FREE(gr);86return;87}88struct fps_info *info = gr->query_data;89info->frametime = false;9091gr->query_new_value = query_fps;9293/* Don't use free() as our callback as that messes up Gallium's94* memory debugger. Use simple free_query_data() wrapper.95*/96gr->free_query_data = free_query_data;9798hud_pane_add_graph(pane, gr);99}100101void102hud_frametime_graph_install(struct hud_pane *pane)103{104struct hud_graph *gr = CALLOC_STRUCT(hud_graph);105106if (!gr)107return;108109strcpy(gr->name, "frametime (ms)");110gr->query_data = CALLOC_STRUCT(fps_info);111if (!gr->query_data) {112FREE(gr);113return;114}115struct fps_info *info = gr->query_data;116info->frametime = true;117118gr->query_new_value = query_fps;119120gr->free_query_data = free_query_data;121122hud_pane_add_graph(pane, gr);123}124125126