summaryrefslogtreecommitdiff
path: root/perf/gtkwidgetprofiler.c
blob: 0a42f1427a70f6d2fca4a2196bb82d03a79a3ddc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#include "config.h"
#include <string.h>
#include "gtkwidgetprofiler.h"
#include "typebuiltins.h"

typedef enum {
  STATE_NOT_CREATED,
  STATE_INSTRUMENTED_NOT_MAPPED,
  STATE_INSTRUMENTED_MAPPED
} State;

struct _GtkWidgetProfilerPrivate {
  State state;

  GtkWidget *profiled_widget;
  GtkWidget *toplevel;

  int n_iterations;

  GTimer *timer;

  gulong toplevel_draw_id;
  gulong toplevel_property_notify_event_id;

  GdkAtom profiler_atom;

  guint profiling : 1;
};

G_DEFINE_TYPE (GtkWidgetProfiler, gtk_widget_profiler, G_TYPE_OBJECT);

static void gtk_widget_profiler_finalize (GObject *object);

enum {
  CREATE_WIDGET,
  REPORT,
  LAST_SIGNAL
};

static guint signals[LAST_SIGNAL];

static void
gtk_widget_profiler_class_init (GtkWidgetProfilerClass *class)
{
  GObjectClass *object_class;

  object_class = (GObjectClass *) class;

  signals[CREATE_WIDGET] =
    g_signal_new ("create-widget",
		  G_OBJECT_CLASS_TYPE (object_class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkWidgetProfilerClass, create_widget),
		  NULL, NULL,
		  NULL,
		  G_TYPE_OBJECT, 0);

  signals[REPORT] =
    g_signal_new ("report",
		  G_OBJECT_CLASS_TYPE (object_class),
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (GtkWidgetProfilerClass, report),
		  NULL, NULL,
		  NULL,
		  G_TYPE_NONE, 3,
		  GTK_TYPE_WIDGET_PROFILER_REPORT,
		  G_TYPE_OBJECT,
		  G_TYPE_DOUBLE);

  object_class->finalize = gtk_widget_profiler_finalize;
}

static void
gtk_widget_profiler_init (GtkWidgetProfiler *profiler)
{
  GtkWidgetProfilerPrivate *priv;

  priv = g_new0 (GtkWidgetProfilerPrivate, 1);
  profiler->priv = priv;

  priv->state = STATE_NOT_CREATED;
  priv->n_iterations = 1;

  priv->timer = g_timer_new ();

  priv->profiler_atom = gdk_atom_intern ("GtkWidgetProfiler", FALSE);
}

static void
reset_state (GtkWidgetProfiler *profiler)
{
  GtkWidgetProfilerPrivate *priv;

  priv = profiler->priv;

  if (priv->toplevel)
    {
      g_signal_handler_disconnect (priv->toplevel, priv->toplevel_draw_id);
      priv->toplevel_draw_id = 0;

      g_signal_handler_disconnect (priv->toplevel, priv->toplevel_property_notify_event_id);
      priv->toplevel_property_notify_event_id = 0;

      gtk_widget_destroy (priv->toplevel);
      priv->toplevel = NULL;
      priv->profiled_widget = NULL;
    }

  priv->state = STATE_NOT_CREATED;
}

static void
gtk_widget_profiler_finalize (GObject *object)
{
  GtkWidgetProfiler *profiler;
  GtkWidgetProfilerPrivate *priv;

  profiler = GTK_WIDGET_PROFILER (object);
  priv = profiler->priv;

  reset_state (profiler);
  g_timer_destroy (priv->timer);

  g_free (priv);

  G_OBJECT_CLASS (gtk_widget_profiler_parent_class)->finalize (object);
}

GtkWidgetProfiler *
gtk_widget_profiler_new (void)
{
  return g_object_new (GTK_TYPE_WIDGET_PROFILER, NULL);
}

void
gtk_widget_profiler_set_num_iterations (GtkWidgetProfiler *profiler,
					gint               n_iterations)
{
  GtkWidgetProfilerPrivate *priv;

  g_return_if_fail (GTK_IS_WIDGET_PROFILER (profiler));
  g_return_if_fail (n_iterations > 0);

  priv = profiler->priv;
  priv->n_iterations = n_iterations;
}

static void
report (GtkWidgetProfiler      *profiler,
	GtkWidgetProfilerReport report,
	gdouble                 elapsed)
{
  GtkWidgetProfilerPrivate *priv;

  priv = profiler->priv;

  g_signal_emit (profiler, signals[REPORT], 0, report, priv->profiled_widget, elapsed);
}

static GtkWidget *
create_widget_via_emission (GtkWidgetProfiler *profiler)
{
  GtkWidget *widget;

  widget = NULL;
  g_signal_emit (profiler, signals[CREATE_WIDGET], 0, &widget);
  if (!widget)
    g_error ("The profiler emitted the \"create-widget\" signal but the signal handler returned no widget!");

  if (gtk_widget_get_visible (widget) || gtk_widget_get_mapped (widget))
    g_error ("The handler for \"create-widget\" must return an unmapped and unshown widget");

  return widget;
}

static gboolean
toplevel_property_notify_event_cb (GtkWidget *widget, GdkEventProperty *event, gpointer data)
{
  GtkWidgetProfiler *profiler;
  GtkWidgetProfilerPrivate *priv;
  gdouble elapsed;

  profiler = GTK_WIDGET_PROFILER (data);
  priv = profiler->priv;

  if (event->atom != priv->profiler_atom)
    return FALSE;

  /* Finish timing map/expose */

  elapsed = g_timer_elapsed (priv->timer, NULL);
  report (profiler, GTK_WIDGET_PROFILER_REPORT_EXPOSE, elapsed);

  gtk_main_quit (); /* This will get us back to the end of profile_map_expose() */
  return TRUE;
}

static gboolean
toplevel_idle_after_draw_cb (gpointer data)
{
  GtkWidgetProfiler *profiler;
  GtkWidgetProfilerPrivate *priv;

  profiler = GTK_WIDGET_PROFILER (data);
  priv = profiler->priv;

  gdk_property_change (gtk_widget_get_window (priv->toplevel),
		       priv->profiler_atom,
		       gdk_atom_intern ("STRING", FALSE),
		       8,
		       GDK_PROP_MODE_REPLACE,
		       (guchar *) "hello",
		       strlen ("hello"));

  return FALSE;
}

static gboolean
toplevel_draw_cb (GtkWidget *widget, cairo_t *cr, gpointer data)
{
  GtkWidgetProfiler *profiler;

  profiler = GTK_WIDGET_PROFILER (data);

  gdk_threads_add_idle_full (G_PRIORITY_HIGH, toplevel_idle_after_draw_cb, profiler, NULL);
  return FALSE;
}

static void
instrument_toplevel (GtkWidgetProfiler *profiler,
		     GtkWidget         *toplevel)
{
  GtkWidgetProfilerPrivate *priv;

  priv = profiler->priv;

  priv->toplevel_draw_id = g_signal_connect (toplevel, "draw",
					     G_CALLBACK (toplevel_draw_cb), profiler);

  gtk_widget_add_events (toplevel, GDK_PROPERTY_CHANGE_MASK);
  priv->toplevel_property_notify_event_id = g_signal_connect (toplevel, "property-notify-event",
							      G_CALLBACK (toplevel_property_notify_event_cb), profiler);
}

static GtkWidget *
ensure_and_get_toplevel (GtkWidget *widget)
{
	GtkWidget *toplevel;
	GtkWidget *window;

	toplevel = gtk_widget_get_toplevel (widget);
	if (gtk_widget_is_toplevel (toplevel))
		return toplevel;

	g_assert (toplevel == widget); /* we don't want extraneous ancestors */

	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_container_add (GTK_CONTAINER (window), widget);

	return window;
}

static GtkWidget *
get_instrumented_toplevel (GtkWidgetProfiler *profiler,
			   GtkWidget         *widget)
{
  GtkWidget *window;

  window = ensure_and_get_toplevel (widget);
  instrument_toplevel (profiler, window);

  return window;
}

static void
map_widget (GtkWidgetProfiler *profiler)
{
  GtkWidgetProfilerPrivate *priv;

  priv = profiler->priv;
  g_assert (priv->state == STATE_INSTRUMENTED_NOT_MAPPED);

  /* Time map.
   *
   * FIXME: we are really timing a show_all(); we don't really wait for all the "map_event" signals
   * to happen.  Should we rename GTK_WIDGET_PROFILER_REPORT_MAP report to something else?
   */

  gtk_widget_show_all (priv->toplevel);
  priv->state = STATE_INSTRUMENTED_MAPPED;
}

static void
profile_map_expose (GtkWidgetProfiler *profiler)
{
  GtkWidgetProfilerPrivate *priv;
  gdouble elapsed;

  priv = profiler->priv;

  g_assert (priv->state == STATE_INSTRUMENTED_NOT_MAPPED);

  g_timer_reset (priv->timer);
  map_widget (profiler);
  elapsed = g_timer_elapsed (priv->timer, NULL);

  report (profiler, GTK_WIDGET_PROFILER_REPORT_MAP, elapsed);

  /* Time expose; this gets recorded in toplevel_property_notify_event_cb() */

  g_timer_reset (priv->timer);
  gtk_main ();
}

static void
profile_destroy (GtkWidgetProfiler *profiler)
{
  GtkWidgetProfilerPrivate *priv;
  gdouble elapsed;

  priv = profiler->priv;

  g_assert (priv->state != STATE_NOT_CREATED);

  g_timer_reset (priv->timer);
  reset_state (profiler);
  elapsed = g_timer_elapsed (priv->timer, NULL);

  report (profiler, GTK_WIDGET_PROFILER_REPORT_DESTROY, elapsed);
}

static void
create_widget (GtkWidgetProfiler *profiler)
{
  GtkWidgetProfilerPrivate *priv;

  priv = profiler->priv;

  g_assert (priv->state == STATE_NOT_CREATED);

  priv->profiled_widget = create_widget_via_emission (profiler);
  priv->toplevel = get_instrumented_toplevel (profiler, priv->profiled_widget);

  priv->state = STATE_INSTRUMENTED_NOT_MAPPED;
}

/* The "boot time" of a widget is the time needed to
 *
 *   1. Create the widget
 *   2. Map it
 *   3. Expose it
 *   4. Destroy it.
 *
 * This runs a lot of interesting code:  instantiation, size requisition and
 * allocation, realization, mapping, exposing, destruction.
 */
static void
profile_boot (GtkWidgetProfiler *profiler)
{
  GtkWidgetProfilerPrivate *priv;
  gdouble elapsed;

  priv = profiler->priv;

  g_assert (priv->state == STATE_NOT_CREATED);

  /* Time creation */

  g_timer_reset (priv->timer);
  create_widget (profiler);
  elapsed = g_timer_elapsed (priv->timer, NULL);

  report (profiler, GTK_WIDGET_PROFILER_REPORT_CREATE, elapsed);

  /* Start timing map/expose */

  profile_map_expose (profiler);

  /* Profile destruction */

  profile_destroy (profiler);
}

/* To measure expose time, we trigger a full expose on the toplevel window.  We
 * do the same as xrefresh(1), i.e. we map and unmap a window to make the other
 * one expose.
 */
static void
profile_expose (GtkWidgetProfiler *profiler)
{
  GtkWidgetProfilerPrivate *priv;
  GtkAllocation allocation;
  GdkWindow *window;
  GdkWindowAttr attr;
  int attr_mask;

  priv = profiler->priv;

  g_assert (priv->state == STATE_INSTRUMENTED_MAPPED);

  /* Time creation */

  gtk_widget_get_allocation (priv->toplevel, &allocation);

  attr.x = 0;
  attr.y = 0;
  attr.width = allocation.width;
  attr.height = allocation.width;
  attr.wclass = GDK_INPUT_OUTPUT;
  attr.window_type = GDK_WINDOW_TEMP;

  attr_mask = GDK_WA_X | GDK_WA_Y;

  window = gdk_window_new (gdk_screen_get_root_window (gtk_widget_get_screen (priv->toplevel)),
                           &attr, attr_mask);

  gdk_window_show (window);
  gdk_window_hide (window);
  gdk_window_destroy (window);

  /* Time expose; this gets recorded in toplevel_property_notify_event_cb() */

  g_timer_reset (priv->timer);
  gtk_main ();
}

void
gtk_widget_profiler_profile_boot (GtkWidgetProfiler *profiler)
{
  GtkWidgetProfilerPrivate *priv;
  int i, n;

  g_return_if_fail (GTK_IS_WIDGET_PROFILER (profiler));

  priv = profiler->priv;
  g_return_if_fail (!priv->profiling);

  reset_state (profiler);
  priv->profiling = TRUE;

  n = priv->n_iterations;
  for (i = 0; i < n; i++)
    profile_boot (profiler);

  priv->profiling = FALSE;
}

void
gtk_widget_profiler_profile_expose (GtkWidgetProfiler *profiler)
{
  GtkWidgetProfilerPrivate *priv;
  int i, n;

  g_return_if_fail (GTK_IS_WIDGET_PROFILER (profiler));

  priv = profiler->priv;
  g_return_if_fail (!priv->profiling);

  reset_state (profiler);
  priv->profiling = TRUE;

  create_widget (profiler);
  map_widget (profiler);

  n = priv->n_iterations;
  for (i = 0; i < n; i++)
    profile_expose (profiler);

  priv->profiling = FALSE;

  reset_state (profiler);
}