From d227b9ce38440fa22e85afa1e4f4422e9f66343c Mon Sep 17 00:00:00 2001 From: Emmanuele Bassi Date: Wed, 3 Aug 2016 16:51:21 +0100 Subject: [PATCH] gsk: Add GskProfiler to GskRenderer The profiler instance is per-renderer, and is accesible to implementations. --- gsk/gskglrenderer.c | 58 ++++++++++++++++++++++++++++++++++++++-- gsk/gskrenderer.c | 44 ++++++++++++++++++++++++++++++ gsk/gskrendererprivate.h | 3 +++ 3 files changed, 103 insertions(+), 2 deletions(-) diff --git a/gsk/gskglrenderer.c b/gsk/gskglrenderer.c index 22e1558729..e770a7c7f8 100644 --- a/gsk/gskglrenderer.c +++ b/gsk/gskglrenderer.c @@ -6,6 +6,7 @@ #include "gskenums.h" #include "gskgldriverprivate.h" #include "gskglprofilerprivate.h" +#include "gskprofilerprivate.h" #include "gskrendererprivate.h" #include "gskrendernodeprivate.h" #include "gskrendernodeiter.h" @@ -75,6 +76,18 @@ enum { N_ATTRIBUTES }; +#ifdef G_ENABLE_DEBUG +typedef struct { + GQuark frames; + GQuark draw_calls; +} ProfileCounters; + +typedef struct { + GQuark cpu_time; + GQuark gpu_time; +} ProfileTimers; +#endif + struct _GskGLRenderer { GskRenderer parent_instance; @@ -102,6 +115,11 @@ struct _GskGLRenderer GArray *render_items; +#ifdef G_ENABLE_DEBUG + ProfileCounters profile_counters; + ProfileTimers profile_timers; +#endif + gboolean has_buffers : 1; }; @@ -419,6 +437,11 @@ render_item (GskGLRenderer *self, glDrawArrays (GL_TRIANGLES, 0, N_VERTICES); +#ifdef G_ENABLE_DEBUG + gsk_profiler_counter_inc (gsk_renderer_get_profiler (GSK_RENDERER (self)), + self->profile_counters.draw_calls); +#endif + /* Render all children items, so we can take the result * render target texture during the compositing */ @@ -797,12 +820,17 @@ gsk_gl_renderer_render (GskRenderer *renderer, graphene_matrix_t modelview, projection; graphene_rect_t viewport; guint i; - guint64 gpu_time; int scale_factor; +#ifdef G_ENABLE_DEBUG + GskProfiler *profiler; + gint64 gpu_time, cpu_time; +#endif if (self->gl_context == NULL) return; + profiler = gsk_renderer_get_profiler (renderer); + gdk_gl_context_make_current (self->gl_context); gsk_renderer_get_viewport (renderer, &viewport); @@ -823,7 +851,11 @@ gsk_gl_renderer_render (GskRenderer *renderer, goto out; gsk_gl_driver_begin_frame (self->gl_driver); + +#ifdef G_ENABLE_DEBUG gsk_gl_profiler_begin_gpu_region (self->gl_profiler); + gsk_profiler_timer_begin (profiler, self->profile_timers.cpu_time); +#endif /* Ensure that the viewport is up to date */ if (gsk_gl_driver_bind_render_target (self->gl_driver, self->texture_id)) @@ -848,8 +880,18 @@ gsk_gl_renderer_render (GskRenderer *renderer, /* Draw the output of the GL rendering to the window */ gsk_gl_driver_end_frame (self->gl_driver); + +#ifdef G_ENABLE_DEBUG + gsk_profiler_counter_inc (profiler, self->profile_counters.frames); + + cpu_time = gsk_profiler_timer_end (profiler, self->profile_timers.cpu_time); + gsk_profiler_timer_set (profiler, self->profile_timers.cpu_time, cpu_time); + gpu_time = gsk_gl_profiler_end_gpu_region (self->gl_profiler); - GSK_NOTE (OPENGL, g_print ("GPU time: %g usec\n", (double) gpu_time / 1000.0)); + gsk_profiler_timer_set (profiler, self->profile_timers.gpu_time, gpu_time); + + gsk_profiler_push_samples (profiler); +#endif out: /* XXX: Add GdkDrawingContext API */ @@ -886,4 +928,16 @@ gsk_gl_renderer_init (GskGLRenderer *self) gsk_ensure_resources (); graphene_matrix_init_identity (&self->mvp); + +#ifdef G_ENABLE_DEBUG + { + GskProfiler *profiler = gsk_renderer_get_profiler (GSK_RENDERER (self)); + + self->profile_counters.frames = gsk_profiler_add_counter (profiler, "frames", "Frames", FALSE); + self->profile_counters.draw_calls = gsk_profiler_add_counter (profiler, "draws", "glDrawArrays", TRUE); + + self->profile_timers.cpu_time = gsk_profiler_add_timer (profiler, "cpu-time", "CPU time", FALSE, TRUE); + self->profile_timers.gpu_time = gsk_profiler_add_timer (profiler, "gpu-time", "GPU time", FALSE, TRUE); + } +#endif } diff --git a/gsk/gskrenderer.c b/gsk/gskrenderer.c index a1b9e3f217..0d38614645 100644 --- a/gsk/gskrenderer.c +++ b/gsk/gskrenderer.c @@ -39,6 +39,7 @@ #include "gskdebugprivate.h" #include "gskglrendererprivate.h" +#include "gskprofilerprivate.h" #include "gskrendernodeprivate.h" #include "gskenumtypes.h" @@ -70,6 +71,8 @@ typedef struct GskRenderNode *root_node; GdkDisplay *display; + GskProfiler *profiler; + int scale_factor; gboolean is_realized : 1; @@ -489,6 +492,8 @@ gsk_renderer_init (GskRenderer *self) graphene_matrix_init_identity (&priv->modelview); graphene_matrix_init_identity (&priv->projection); + priv->profiler = gsk_profiler_new (); + priv->auto_clear = TRUE; priv->scale_factor = 1; @@ -927,8 +932,29 @@ gsk_renderer_render (GskRenderer *renderer, priv->root_node = gsk_render_node_ref (root); gsk_render_node_make_immutable (priv->root_node); +#ifdef G_ENABLE_DEBUG + gsk_profiler_reset (priv->profiler); +#endif + GSK_RENDERER_GET_CLASS (renderer)->render (renderer, root, context); +#ifdef G_ENABLE_DEBUG + if (GSK_DEBUG_CHECK (RENDERER)) + { + GString *buf = g_string_new ("*** Frame stats ***\n\n"); + + gsk_profiler_append_counters (priv->profiler, buf); + g_string_append_c (buf, '\n'); + + gsk_profiler_append_timers (priv->profiler, buf); + g_string_append_c (buf, '\n'); + + g_print ("%s\n***\n\n", buf->str); + + g_string_free (buf, TRUE); + } +#endif + g_clear_object (&priv->drawing_context); g_clear_pointer (&priv->root_node, gsk_render_node_unref); } @@ -1053,6 +1079,24 @@ gsk_renderer_create_render_node (GskRenderer *renderer) return gsk_render_node_new (renderer); } +/*< private > + * gsk_renderer_get_profiler: + * @renderer: a #GskRenderer + * + * Retrieves a pointer to the GskProfiler instance of the renderer. + * + * Returns: (transfer none): the profiler + */ +GskProfiler * +gsk_renderer_get_profiler (GskRenderer *renderer) +{ + GskRendererPrivate *priv = gsk_renderer_get_instance_private (renderer); + + g_return_val_if_fail (GSK_IS_RENDERER (renderer), NULL); + + return priv->profiler; +} + /** * gsk_renderer_get_for_display: * @display: a #GdkDisplay diff --git a/gsk/gskrendererprivate.h b/gsk/gskrendererprivate.h index 81037193d4..d560d0183d 100644 --- a/gsk/gskrendererprivate.h +++ b/gsk/gskrendererprivate.h @@ -20,6 +20,7 @@ #define __GSK_RENDERER_PRIVATE_H__ #include "gskrenderer.h" +#include "gskprofilerprivate.h" G_BEGIN_DECLS @@ -49,6 +50,8 @@ gboolean gsk_renderer_is_realized (GskRenderer *renderer); GskRenderNode * gsk_renderer_get_root_node (GskRenderer *renderer); GdkDrawingContext * gsk_renderer_get_drawing_context (GskRenderer *renderer); +GskProfiler * gsk_renderer_get_profiler (GskRenderer *renderer); + G_END_DECLS #endif /* __GSK_RENDERER_PRIVATE_H__ */