From 73e2aec6261394a163ced547f6165af33f819f83 Mon Sep 17 00:00:00 2001 From: Christian Hergert Date: Fri, 23 Jun 2023 10:19:10 -0700 Subject: [PATCH] libsysprof-gtk: add chart layer for timespan drawing This will allow us to drop the custom mark chart stuff and do this all more generically using timeseries and charts. Long term, we'll probably want to buy in on the expression stuff to get labels/etc as well as building the series from another data source. --- src/libsysprof-gtk/meson.build | 2 + src/libsysprof-gtk/sysprof-gtk.h | 1 + src/libsysprof-gtk/sysprof-time-span-layer.c | 316 +++++++++++++++++++ src/libsysprof-gtk/sysprof-time-span-layer.h | 54 ++++ src/libsysprof-gtk/tests/test-charts.c | 33 ++ 5 files changed, 406 insertions(+) create mode 100644 src/libsysprof-gtk/sysprof-time-span-layer.c create mode 100644 src/libsysprof-gtk/sysprof-time-span-layer.h diff --git a/src/libsysprof-gtk/meson.build b/src/libsysprof-gtk/meson.build index dc9cd218..71849b47 100644 --- a/src/libsysprof-gtk/meson.build +++ b/src/libsysprof-gtk/meson.build @@ -8,6 +8,7 @@ libsysprof_gtk_public_sources = [ 'sysprof-mark-table.c', 'sysprof-session.c', 'sysprof-split-layer.c', + 'sysprof-time-span-layer.c', 'sysprof-weighted-callgraph-view.c', ] @@ -23,6 +24,7 @@ libsysprof_gtk_public_headers = [ 'sysprof-mark-table.h', 'sysprof-session.h', 'sysprof-split-layer.h', + 'sysprof-time-span-layer.h', 'sysprof-weighted-callgraph-view.h', ] diff --git a/src/libsysprof-gtk/sysprof-gtk.h b/src/libsysprof-gtk/sysprof-gtk.h index aadea18b..c87c8207 100644 --- a/src/libsysprof-gtk/sysprof-gtk.h +++ b/src/libsysprof-gtk/sysprof-gtk.h @@ -32,6 +32,7 @@ G_BEGIN_DECLS # include "sysprof-mark-table.h" # include "sysprof-session.h" # include "sysprof-split-layer.h" +# include "sysprof-time-span-layer.h" # include "sysprof-weighted-callgraph-view.h" #undef SYSPROF_GTK_INSIDE diff --git a/src/libsysprof-gtk/sysprof-time-span-layer.c b/src/libsysprof-gtk/sysprof-time-span-layer.c new file mode 100644 index 00000000..ef85c8e9 --- /dev/null +++ b/src/libsysprof-gtk/sysprof-time-span-layer.c @@ -0,0 +1,316 @@ +/* + * sysprof-time-span-layer.c + * + * Copyright 2023 Christian Hergert + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include "config.h" + +#include "sysprof-time-span-layer.h" + +struct _SysprofTimeSpanLayer +{ + SysprofChartLayer parent_instance; + + SysprofTimeSeries *series; + + GdkRGBA color; + GdkRGBA event_color; +}; + +G_DEFINE_FINAL_TYPE (SysprofTimeSpanLayer, sysprof_time_span_layer, SYSPROF_TYPE_CHART_LAYER) + +enum { + PROP_0, + PROP_COLOR, + PROP_EVENT_COLOR, + PROP_SERIES, + N_PROPS +}; + +static GParamSpec *properties [N_PROPS]; + +SysprofChartLayer * +sysprof_time_span_layer_new (void) +{ + return g_object_new (SYSPROF_TYPE_TIME_SPAN_LAYER, NULL); +} + +static void +sysprof_time_span_layer_snapshot (GtkWidget *widget, + GtkSnapshot *snapshot) +{ + SysprofTimeSpanLayer *self = (SysprofTimeSpanLayer *)widget; + const SysprofTimeSeriesValue *values; + graphene_rect_t box_rect; + float last_end_x = 0; + guint n_values; + int width; + int height; + + g_assert (SYSPROF_IS_TIME_SPAN_LAYER (self)); + g_assert (GTK_IS_SNAPSHOT (snapshot)); + + width = gtk_widget_get_width (widget); + height = gtk_widget_get_height (widget); + + if (width == 0 || height == 0 || self->series == NULL || self->color.alpha == 0) + return; + + if (!(values = sysprof_time_series_get_values (self->series, &n_values))) + return; + + /* First pass, draw our rectangles for duration which we + * always want in the background compared to "points" which + * are marks w/o a duration. + */ + for (guint i = 0; i < n_values; i++) + { + const SysprofTimeSeriesValue *v = &values[i]; + + if (v->begin != v->end) + { + graphene_rect_t rect; + float end_x; + + rect = GRAPHENE_RECT_INIT (floorf (v->begin * width), + 0, + ceilf ((v->end - v->begin) * width), + height); + + /* Ignore empty sized draws */ + if (rect.size.width == 0) + continue; + + /* Cull draw unless it will extend past last rect */ + end_x = rect.origin.x + rect.size.width; + if (end_x <= last_end_x) + continue; + else + last_end_x = end_x; + + gtk_snapshot_append_color (snapshot, &self->color, &rect); + } + } + + box_rect = GRAPHENE_RECT_INIT (-ceil (height / 4.), + -ceil (height / 4.), + ceil (height/2.), + ceil (height/2.)); + + for (guint i = 0; i < n_values; i++) + { + const SysprofTimeSeriesValue *v = &values[i]; + + if (v->begin == v->end) + { + gtk_snapshot_save (snapshot); + gtk_snapshot_translate (snapshot, + &GRAPHENE_POINT_INIT (v->begin * width, height / 2)); + gtk_snapshot_rotate (snapshot, 45.f); + gtk_snapshot_append_color (snapshot, &self->event_color, &box_rect); + gtk_snapshot_restore (snapshot); + } + } +} + +static void +sysprof_time_span_layer_finalize (GObject *object) +{ + SysprofTimeSpanLayer *self = (SysprofTimeSpanLayer *)object; + + g_clear_pointer (&self->series, sysprof_time_series_unref); + + G_OBJECT_CLASS (sysprof_time_span_layer_parent_class)->finalize (object); +} + +static void +sysprof_time_span_layer_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + SysprofTimeSpanLayer *self = SYSPROF_TIME_SPAN_LAYER (object); + + switch (prop_id) + { + case PROP_COLOR: + g_value_set_boxed (value, sysprof_time_span_layer_get_color (self)); + break; + + case PROP_EVENT_COLOR: + g_value_set_boxed (value, sysprof_time_span_layer_get_event_color (self)); + break; + + case PROP_SERIES: + g_value_set_boxed (value, sysprof_time_span_layer_get_series (self)); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + +static void +sysprof_time_span_layer_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + SysprofTimeSpanLayer *self = SYSPROF_TIME_SPAN_LAYER (object); + + switch (prop_id) + { + case PROP_COLOR: + sysprof_time_span_layer_set_color (self, g_value_get_boxed (value)); + break; + + case PROP_EVENT_COLOR: + sysprof_time_span_layer_set_event_color (self, g_value_get_boxed (value)); + break; + + case PROP_SERIES: + sysprof_time_span_layer_set_series (self, g_value_get_boxed (value)); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + +static void +sysprof_time_span_layer_class_init (SysprofTimeSpanLayerClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); + + object_class->finalize = sysprof_time_span_layer_finalize; + object_class->get_property = sysprof_time_span_layer_get_property; + object_class->set_property = sysprof_time_span_layer_set_property; + + widget_class->snapshot = sysprof_time_span_layer_snapshot; + + properties[PROP_COLOR] = + g_param_spec_boxed ("color", NULL, NULL, + GDK_TYPE_RGBA, + (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS)); + + properties[PROP_EVENT_COLOR] = + g_param_spec_boxed ("event-color", NULL, NULL, + GDK_TYPE_RGBA, + (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS)); + + properties[PROP_SERIES] = + g_param_spec_boxed ("series", NULL, NULL, + SYSPROF_TYPE_TIME_SERIES, + (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS)); + + g_object_class_install_properties (object_class, N_PROPS, properties); +} + +static void +sysprof_time_span_layer_init (SysprofTimeSpanLayer *self) +{ + self->color.alpha = 1; + self->event_color.alpha = 1; +} + +const GdkRGBA * +sysprof_time_span_layer_get_color (SysprofTimeSpanLayer *self) +{ + g_return_val_if_fail (SYSPROF_IS_TIME_SPAN_LAYER (self), NULL); + + return &self->color; +} + +void +sysprof_time_span_layer_set_color (SysprofTimeSpanLayer *self, + const GdkRGBA *color) +{ + static const GdkRGBA black = {0,0,0,1}; + + g_return_if_fail (SYSPROF_IS_TIME_SPAN_LAYER (self)); + + if (color == NULL) + color = &black; + + if (!gdk_rgba_equal (&self->color, color)) + { + self->color = *color; + g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_COLOR]); + gtk_widget_queue_draw (GTK_WIDGET (self)); + } +} + +const GdkRGBA * +sysprof_time_span_layer_get_event_color (SysprofTimeSpanLayer *self) +{ + g_return_val_if_fail (SYSPROF_IS_TIME_SPAN_LAYER (self), NULL); + + return &self->event_color; +} + +void +sysprof_time_span_layer_set_event_color (SysprofTimeSpanLayer *self, + const GdkRGBA *event_color) +{ + static const GdkRGBA black = {0,0,0,1}; + + g_return_if_fail (SYSPROF_IS_TIME_SPAN_LAYER (self)); + + if (event_color == NULL) + event_color = &black; + + if (!gdk_rgba_equal (&self->event_color, event_color)) + { + self->event_color = *event_color; + g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_EVENT_COLOR]); + gtk_widget_queue_draw (GTK_WIDGET (self)); + } +} + +/** + * sysprof_time_span_layer_get_series: + * @self: a #SysprofTimeSpanLayer + * + * Returns: (transfer none) (nullable): a #SysprofTimeSeries or %NULL + */ +SysprofTimeSeries * +sysprof_time_span_layer_get_series (SysprofTimeSpanLayer *self) +{ + g_return_val_if_fail (SYSPROF_IS_TIME_SPAN_LAYER (self), NULL); + + return self->series; +} + +void +sysprof_time_span_layer_set_series (SysprofTimeSpanLayer *self, + SysprofTimeSeries *series) +{ + g_return_if_fail (SYSPROF_IS_TIME_SPAN_LAYER (self)); + + if (series == self->series) + return; + + g_clear_pointer (&self->series, sysprof_time_series_unref); + self->series = series ? sysprof_time_series_ref (series) : NULL; + + g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SERIES]); + + gtk_widget_queue_draw (GTK_WIDGET (self)); +} diff --git a/src/libsysprof-gtk/sysprof-time-span-layer.h b/src/libsysprof-gtk/sysprof-time-span-layer.h new file mode 100644 index 00000000..eee4e294 --- /dev/null +++ b/src/libsysprof-gtk/sysprof-time-span-layer.h @@ -0,0 +1,54 @@ +/* + * sysprof-time-span-layer.h + * + * Copyright 2023 Christian Hergert + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#pragma once + +#include + +#include "sysprof-chart-layer.h" + +G_BEGIN_DECLS + +#define SYSPROF_TYPE_TIME_SPAN_LAYER (sysprof_time_span_layer_get_type()) + +SYSPROF_AVAILABLE_IN_ALL +G_DECLARE_FINAL_TYPE (SysprofTimeSpanLayer, sysprof_time_span_layer, SYSPROF, TIME_SPAN_LAYER, SysprofChartLayer) + +SYSPROF_AVAILABLE_IN_ALL +SysprofChartLayer *sysprof_time_span_layer_new (void); +SYSPROF_AVAILABLE_IN_ALL +const GdkRGBA *sysprof_time_span_layer_get_color (SysprofTimeSpanLayer *self); +SYSPROF_AVAILABLE_IN_ALL +void sysprof_time_span_layer_set_color (SysprofTimeSpanLayer *self, + const GdkRGBA *color); +SYSPROF_AVAILABLE_IN_ALL +const GdkRGBA *sysprof_time_span_layer_get_event_color (SysprofTimeSpanLayer *self); +SYSPROF_AVAILABLE_IN_ALL +void sysprof_time_span_layer_set_event_color (SysprofTimeSpanLayer *self, + const GdkRGBA *event_color); +SYSPROF_AVAILABLE_IN_ALL +SysprofTimeSeries *sysprof_time_span_layer_get_series (SysprofTimeSpanLayer *self); +SYSPROF_AVAILABLE_IN_ALL +void sysprof_time_span_layer_set_series (SysprofTimeSpanLayer *self, + SysprofTimeSeries *series); + +G_END_DECLS + diff --git a/src/libsysprof-gtk/tests/test-charts.c b/src/libsysprof-gtk/tests/test-charts.c index d62d2844..594e242d 100644 --- a/src/libsysprof-gtk/tests/test-charts.c +++ b/src/libsysprof-gtk/tests/test-charts.c @@ -93,7 +93,9 @@ main (int argc, g_autoptr(SysprofSession) session = NULL; g_autoptr(SysprofXYSeries) samples_series = NULL; g_autoptr(SysprofXYSeries) num_series = NULL; + g_autoptr(SysprofTimeSeries) marks_series = NULL; g_autoptr(GListModel) samples = NULL; + g_autoptr(GListModel) marks = NULL; g_autoptr(GError) error = NULL; const SysprofTimeSpan *time_span; GtkWidget *chart; @@ -101,7 +103,10 @@ main (int argc, GtkWidget *split; GtkWindow *window; GtkWidget *box; + GdkRGBA blue = {0,0,1,1}; + GdkRGBA red = {1,0,0,1}; guint n_samples; + guint n_marks; sysprof_clock_init (); @@ -134,6 +139,7 @@ main (int argc, session = sysprof_session_new (document); time_span = sysprof_document_get_time_span (document); + marks = sysprof_document_list_marks (document); /* Generate an XY Series using the stacktraces depth for Y */ samples = sysprof_document_list_samples (document); @@ -155,6 +161,20 @@ main (int argc, g_print ("series built\n"); + marks_series = sysprof_time_series_new (marks, *sysprof_document_get_time_span (document)); + n_marks = g_list_model_get_n_items (marks); + for (guint i = 0; i < n_marks; i++) + { + g_autoptr(SysprofDocumentMark) mark = g_list_model_get_item (marks, i); + gint64 time = sysprof_document_frame_get_time (SYSPROF_DOCUMENT_FRAME (mark)); + gint64 duration = sysprof_document_mark_get_duration (mark); + + sysprof_time_series_add (marks_series, + (SysprofTimeSpan) {time, time+duration}, + i); + } + sysprof_time_series_sort (marks_series); + window = g_object_new (GTK_TYPE_WINDOW, "default-width", 800, "default-height", 600, @@ -220,6 +240,19 @@ main (int argc, SYSPROF_CHART_LAYER (layer)); gtk_box_append (GTK_BOX (box), chart); + chart = g_object_new (SYSPROF_TYPE_CHART, + "session", session, + "height-request", 24, + NULL); + layer = g_object_new (SYSPROF_TYPE_TIME_SPAN_LAYER, + "color", &blue, + "event-color", &red, + "series", marks_series, + NULL), + sysprof_chart_add_layer (SYSPROF_CHART (chart), + SYSPROF_CHART_LAYER (layer)); + gtk_box_append (GTK_BOX (box), chart); + gtk_window_present (window); g_main_loop_run (main_loop);