mirror of
https://github.com/varun-r-mallya/sysprof.git
synced 2026-02-11 07:30:54 +00:00
libsysprof-gtk: use timeseries for rendering chart marks
This allows us to pre-calculate the values and re-use them, while also allowing for us to avoid inflating GObject's for marks unless it becomes necessary to access additional information.
This commit is contained in:
@ -26,8 +26,11 @@
|
||||
|
||||
struct _SysprofMarkChartRow
|
||||
{
|
||||
GtkWidget parent_instance;
|
||||
GtkWidget parent_instance;
|
||||
GCancellable *cancellable;
|
||||
SysprofTimeSeries *series;
|
||||
SysprofMarkChartItem *item;
|
||||
guint update_source;
|
||||
};
|
||||
|
||||
enum {
|
||||
@ -40,6 +43,25 @@ G_DEFINE_FINAL_TYPE (SysprofMarkChartRow, sysprof_mark_chart_row, GTK_TYPE_WIDGE
|
||||
|
||||
static GParamSpec *properties [N_PROPS];
|
||||
|
||||
static void
|
||||
cancel_and_clear (GCancellable **cancellable)
|
||||
{
|
||||
g_cancellable_cancel (*cancellable);
|
||||
g_clear_object (cancellable);
|
||||
}
|
||||
|
||||
static void
|
||||
sysprof_mark_chart_row_set_series (SysprofMarkChartRow *self,
|
||||
SysprofTimeSeries *series)
|
||||
{
|
||||
g_assert (SYSPROF_IS_MARK_CHART_ROW (self));
|
||||
|
||||
g_clear_pointer (&self->series, sysprof_time_series_unref);
|
||||
self->series = series ? sysprof_time_series_ref (series) : NULL;
|
||||
|
||||
gtk_widget_queue_draw (GTK_WIDGET (self));
|
||||
}
|
||||
|
||||
static void
|
||||
sysprof_mark_chart_row_snapshot (GtkWidget *widget,
|
||||
GtkSnapshot *snapshot)
|
||||
@ -48,37 +70,36 @@ sysprof_mark_chart_row_snapshot (GtkWidget *widget,
|
||||
static const GdkRGBA blue = {0,0,1,1};
|
||||
static const GdkRGBA red = {1,0,0,1};
|
||||
static const GdkRGBA white = {1,1,1,1};
|
||||
const SysprofTimeSeriesValue *values;
|
||||
GListModel *model;
|
||||
PangoLayout *layout;
|
||||
guint n_items;
|
||||
guint n_values;
|
||||
int width;
|
||||
int height;
|
||||
|
||||
g_assert (SYSPROF_IS_MARK_CHART_ROW (self));
|
||||
g_assert (GTK_IS_SNAPSHOT (snapshot));
|
||||
|
||||
if (self->item == NULL)
|
||||
if (self->series == NULL)
|
||||
return;
|
||||
|
||||
width = gtk_widget_get_width (widget);
|
||||
height = gtk_widget_get_height (widget);
|
||||
|
||||
model = sysprof_mark_chart_item_get_marks (self->item);
|
||||
n_items = g_list_model_get_n_items (model);
|
||||
model = sysprof_time_series_get_model (self->series);
|
||||
values = sysprof_time_series_get_values (self->series, &n_values);
|
||||
|
||||
layout = gtk_widget_create_pango_layout (widget, NULL);
|
||||
|
||||
for (guint i = 0; i < n_items; i++)
|
||||
for (guint i = 0; i < n_values; i++)
|
||||
{
|
||||
g_autoptr(SysprofDocumentMark) mark = g_list_model_get_item (model, i);
|
||||
double begin, end;
|
||||
const SysprofTimeSeriesValue *v = &values[i];
|
||||
|
||||
sysprof_document_mark_get_time_fraction (mark, &begin, &end);
|
||||
|
||||
if (begin == end)
|
||||
if (v->begin == v->end)
|
||||
{
|
||||
gtk_snapshot_save (snapshot);
|
||||
gtk_snapshot_translate (snapshot, &GRAPHENE_POINT_INIT (begin*width, height/2));
|
||||
gtk_snapshot_translate (snapshot,
|
||||
&GRAPHENE_POINT_INIT (v->begin * width, height / 2));
|
||||
gtk_snapshot_rotate (snapshot, 45.f);
|
||||
gtk_snapshot_append_color (snapshot,
|
||||
&red,
|
||||
@ -87,22 +108,34 @@ sysprof_mark_chart_row_snapshot (GtkWidget *widget,
|
||||
}
|
||||
else
|
||||
{
|
||||
const char *message = sysprof_document_mark_get_message (mark);
|
||||
|
||||
gtk_snapshot_append_color (snapshot,
|
||||
&blue,
|
||||
&GRAPHENE_RECT_INIT (begin*width, 0, end*width, height));
|
||||
&GRAPHENE_RECT_INIT (v->begin * width,
|
||||
0,
|
||||
v->end * width,
|
||||
height));
|
||||
|
||||
if (message)
|
||||
if (i + 1 < n_values && values[i+1].begin > v->end)
|
||||
{
|
||||
pango_layout_set_text (layout, message, -1);
|
||||
g_autoptr(SysprofDocumentMark) mark = g_list_model_get_item (model, v->index);
|
||||
const char *message = sysprof_document_mark_get_message (mark);
|
||||
|
||||
gtk_snapshot_save (snapshot);
|
||||
gtk_snapshot_push_clip (snapshot, &GRAPHENE_RECT_INIT (begin*width, 0, end*width, height));
|
||||
gtk_snapshot_translate (snapshot, &GRAPHENE_POINT_INIT (begin*width, 0));
|
||||
gtk_snapshot_append_layout (snapshot, layout, &white);
|
||||
gtk_snapshot_pop (snapshot);
|
||||
gtk_snapshot_restore (snapshot);
|
||||
if (message && message[0])
|
||||
{
|
||||
pango_layout_set_text (layout, message, -1);
|
||||
|
||||
gtk_snapshot_save (snapshot);
|
||||
gtk_snapshot_push_clip (snapshot,
|
||||
&GRAPHENE_RECT_INIT (v->begin * width,
|
||||
0,
|
||||
v->end * width,
|
||||
height));
|
||||
gtk_snapshot_translate (snapshot,
|
||||
&GRAPHENE_POINT_INIT (v->begin * width, 0));
|
||||
gtk_snapshot_append_layout (snapshot, layout, &white);
|
||||
gtk_snapshot_pop (snapshot);
|
||||
gtk_snapshot_restore (snapshot);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -110,11 +143,64 @@ sysprof_mark_chart_row_snapshot (GtkWidget *widget,
|
||||
g_object_unref (layout);
|
||||
}
|
||||
|
||||
static void
|
||||
sysprof_mark_chart_row_load_time_series_cb (GObject *object,
|
||||
GAsyncResult *result,
|
||||
gpointer user_data)
|
||||
{
|
||||
SysprofMarkChartItem *item = (SysprofMarkChartItem *)object;
|
||||
g_autoptr(SysprofMarkChartRow) self = user_data;
|
||||
g_autoptr(SysprofTimeSeries) series = NULL;
|
||||
|
||||
g_assert (SYSPROF_IS_MARK_CHART_ITEM (item));
|
||||
g_assert (G_IS_ASYNC_RESULT (result));
|
||||
g_assert (SYSPROF_IS_MARK_CHART_ROW (self));
|
||||
|
||||
series = sysprof_mark_chart_item_load_time_series_finish (item, result, NULL);
|
||||
|
||||
sysprof_mark_chart_row_set_series (self, series);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
sysprof_mark_chart_row_dispatch_update (gpointer user_data)
|
||||
{
|
||||
SysprofMarkChartRow *self = user_data;
|
||||
|
||||
g_assert (SYSPROF_IS_MARK_CHART_ROW (self));
|
||||
|
||||
g_clear_handle_id (&self->update_source, g_source_remove);
|
||||
cancel_and_clear (&self->cancellable);
|
||||
|
||||
if (self->item != NULL)
|
||||
{
|
||||
self->cancellable = g_cancellable_new ();
|
||||
sysprof_mark_chart_item_load_time_series (self->item,
|
||||
self->cancellable,
|
||||
sysprof_mark_chart_row_load_time_series_cb,
|
||||
g_object_ref (self));
|
||||
}
|
||||
|
||||
return G_SOURCE_REMOVE;
|
||||
}
|
||||
|
||||
static void
|
||||
sysprof_mark_chart_row_queue_update (SysprofMarkChartRow *self)
|
||||
{
|
||||
g_assert (SYSPROF_IS_MARK_CHART_ROW (self));
|
||||
|
||||
cancel_and_clear (&self->cancellable);
|
||||
g_clear_handle_id (&self->update_source, g_source_remove);
|
||||
self->update_source = g_idle_add (sysprof_mark_chart_row_dispatch_update, self);
|
||||
}
|
||||
|
||||
static void
|
||||
sysprof_mark_chart_row_item_changed_cb (SysprofMarkChartRow *self,
|
||||
SysprofMarkChartItem *item)
|
||||
{
|
||||
g_assert (SYSPROF_IS_MARK_CHART_ROW (self));
|
||||
g_assert (SYSPROF_IS_MARK_CHART_ITEM (item));
|
||||
|
||||
sysprof_mark_chart_row_queue_update (self);
|
||||
}
|
||||
|
||||
static void
|
||||
@ -122,6 +208,8 @@ sysprof_mark_chart_row_dispose (GObject *object)
|
||||
{
|
||||
SysprofMarkChartRow *self = (SysprofMarkChartRow *)object;
|
||||
|
||||
g_clear_handle_id (&self->update_source, g_source_remove);
|
||||
|
||||
sysprof_mark_chart_row_set_item (self, NULL);
|
||||
|
||||
G_OBJECT_CLASS (sysprof_mark_chart_row_parent_class)->dispose (object);
|
||||
@ -208,17 +296,20 @@ sysprof_mark_chart_row_set_item (SysprofMarkChartRow *self,
|
||||
if (self->item == item)
|
||||
return;
|
||||
|
||||
cancel_and_clear (&self->cancellable);
|
||||
g_clear_handle_id (&self->update_source, g_source_remove);
|
||||
|
||||
if (self->item)
|
||||
g_signal_handlers_disconnect_by_func (sysprof_mark_chart_item_get_marks (self->item),
|
||||
G_CALLBACK (sysprof_mark_chart_row_queue_update),
|
||||
g_signal_handlers_disconnect_by_func (self->item,
|
||||
G_CALLBACK (sysprof_mark_chart_row_item_changed_cb),
|
||||
self);
|
||||
|
||||
g_set_object (&self->item, item);
|
||||
|
||||
if (item)
|
||||
g_signal_connect_object (sysprof_mark_chart_item_get_marks (item),
|
||||
"items-changed",
|
||||
G_CALLBACK (sysprof_mark_chart_row_queue_update),
|
||||
g_signal_connect_object (self->item,
|
||||
"changed",
|
||||
G_CALLBACK (sysprof_mark_chart_row_item_changed_cb),
|
||||
self,
|
||||
G_CONNECT_SWAPPED);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user