summaryrefslogtreecommitdiff
path: root/gtk/gtkdebugupdates.c
blob: 81ef6ad0ccaf1828f48232025c0b206cd2c4d286 (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
/* GTK - The GIMP Toolkit
 * Copyright (C) 2016 Benjamin Otte <otte@gnome.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include "gtkdebugupdatesprivate.h"

#include "gtksnapshot.h"

/* duration before we start fading in us */
#define GDK_DRAW_REGION_MIN_DURATION 50 * 1000
/* duration when fade is finished in us */
#define GDK_DRAW_REGION_MAX_DURATION 200 * 1000

typedef struct {
  gint64 timestamp;
  cairo_region_t *region;
} GtkDebugUpdate;

static gboolean _gtk_debug_updates_enabled = FALSE;

static GQuark _gtk_debug_updates_quark = 0;

static void
gtk_debug_updates_init (void)
{
  static gboolean inited = FALSE;

  if (G_LIKELY (inited))
    return;

  _gtk_debug_updates_quark = g_quark_from_static_string ("-gtk-debug-updates");

  inited = TRUE;
}

gboolean
gtk_debug_updates_get_enabled (void)
{
  return _gtk_debug_updates_enabled;
}

void
gtk_debug_updates_set_enabled (gboolean enabled)
{
  if (enabled)
    gtk_debug_updates_init ();

  _gtk_debug_updates_enabled = enabled;
}

gboolean
gtk_debug_updates_get_enabled_for_display (GdkDisplay *display)
{
  if (gtk_debug_updates_get_enabled ())
    return TRUE;

  if (_gtk_debug_updates_quark == 0)
    return FALSE;

  return g_object_get_qdata (G_OBJECT (display), _gtk_debug_updates_quark) != NULL;
}

void
gtk_debug_updates_set_enabled_for_display (GdkDisplay *display,
                                           gboolean    enabled)
{
  if (enabled)
    {
      gtk_debug_updates_init ();

      g_object_set_qdata (G_OBJECT (display), _gtk_debug_updates_quark, GINT_TO_POINTER (TRUE));
    }
  else
    {
      if (_gtk_debug_updates_quark != 0)
        g_object_steal_qdata (G_OBJECT (display), _gtk_debug_updates_quark);
    }
}

static void
gtk_debug_update_free (gpointer data)
{
  GtkDebugUpdate *region = data;

  cairo_region_destroy (region->region);
  g_slice_free (GtkDebugUpdate, region);
}

static void
gtk_debug_update_free_queue (gpointer data)
{
  GQueue *queue = data;

  g_queue_free_full (queue, gtk_debug_update_free);
}

static void
gtk_debug_updates_print (GQueue               *queue,
                         const cairo_region_t *region,
                         const char           *format,
                         ...) G_GNUC_PRINTF(3,4);
static void
gtk_debug_updates_print (GQueue               *queue,
                         const cairo_region_t *region,
                         const char           *format,
                         ...)
{
#if 0
  GString *string = g_string_new (NULL);
  va_list args;

  g_string_append_printf (string, "%3u: ", g_queue_get_length (queue));

  if (region)
    {
      cairo_rectangle_int_t extents;

      cairo_region_get_extents (region, &extents);
      g_string_append_printf (string, "{%u,%u,%u,%u}(%u) ",
                              extents.x, extents.y,
                              extents.width, extents.height,
                              cairo_region_num_rectangles (region));
    }

  va_start (args, format);
  g_string_append_vprintf (string, format, args);
  va_end (args);

  g_print ("%s\n", string->str);

  g_string_free (string, TRUE);
#endif
}

static gboolean
gtk_window_manage_updates (GtkWidget     *widget,
                           GdkFrameClock *frame_clock,
                           gpointer       user_data)
{
  GQueue *updates;
  GtkDebugUpdate *draw;
  cairo_region_t *region;
  gint64 timestamp;
  GList *l;

  updates = g_object_get_qdata (G_OBJECT (widget), _gtk_debug_updates_quark);
  if (updates == NULL)
    return FALSE;
  timestamp = gdk_frame_clock_get_frame_time (frame_clock);

  gtk_debug_updates_print (updates, NULL, "Managing updates");
  /* First queue an update for all regions.
   * While doing so, set the correct timestamp on all untimed regions */
  region = cairo_region_create ();
  for (l = g_queue_peek_head_link (updates); l != NULL; l = l->next)
    {
      draw = l->data;

      if (draw->timestamp == 0)
        {
          draw->timestamp = timestamp;
          gtk_debug_updates_print (updates, draw->region, "Setting timestamp to %lli\n", (long long) timestamp);
        }

      cairo_region_union (region, draw->region);
    }
  gtk_debug_updates_print (updates, region, "Queued update");
  gdk_window_invalidate_region (gtk_widget_get_window (widget), region, TRUE);
  cairo_region_destroy (region);

  /* Then remove all outdated regions */
  do {
    draw = g_queue_peek_tail (updates);

    if (draw == NULL)
      {
        gtk_debug_updates_print (updates, NULL, "Empty, no more updates");
        g_object_set_qdata (G_OBJECT (widget), _gtk_debug_updates_quark, NULL);
        return FALSE;
      }

    if (draw->timestamp + GDK_DRAW_REGION_MAX_DURATION >= timestamp)
      break;

    gtk_debug_updates_print (updates, draw->region, "Popped region");
    draw = g_queue_pop_tail (updates);
    gtk_debug_update_free (draw);
  } while (TRUE);

  return TRUE;
}

void
gtk_debug_updates_add (GtkWidget              *widget,
                       const cairo_region_t   *region)
{
  GQueue *updates;
  GtkDebugUpdate *draw;

  if (!gtk_debug_updates_get_enabled_for_display (gtk_widget_get_display (widget)))
    return;

  updates = g_object_get_qdata (G_OBJECT (widget), _gtk_debug_updates_quark);
  if (updates == NULL)
    {
      updates = g_queue_new ();
      gtk_widget_add_tick_callback (widget,
                                    gtk_window_manage_updates,
                                    NULL,
                                    NULL);
      g_object_set_qdata_full (G_OBJECT (widget),
                               _gtk_debug_updates_quark,
                               updates,
                               gtk_debug_update_free_queue);
      gtk_debug_updates_print (updates, NULL, "Newly created");
    }
  else
    {
      GtkDebugUpdate *first = g_queue_peek_head (updates);

      if (first->timestamp == 0)
        {
          cairo_region_union (first->region, region);
          gtk_debug_updates_print (updates, first->region, "Added to existing region");
          return;
        }
    }

  draw = g_slice_new0 (GtkDebugUpdate);
  draw->region = cairo_region_copy (region);
  g_queue_push_head (updates, draw);
  gtk_debug_updates_print (updates, draw->region, "Added new region");
}

void
gtk_debug_updates_snapshot (GtkWidget   *widget,
                            GtkSnapshot *snapshot)
{
  GQueue *updates;
  GtkDebugUpdate *draw;
  GdkRectangle rect;
  gint64 timestamp;
  double progress;
  GList *l;
  guint i;

  if (!gtk_debug_updates_get_enabled_for_display (gtk_widget_get_display (widget)))
    return;

  updates = g_object_get_qdata (G_OBJECT (widget), _gtk_debug_updates_quark);
  if (updates == NULL)
    return;
  timestamp = gdk_frame_clock_get_frame_time (gtk_widget_get_frame_clock (widget));
  
  gtk_debug_updates_print (updates, NULL, "Painting at %lli", (long long) timestamp);

  for (l = g_queue_peek_head_link (updates); l != NULL; l = l->next)
    {
      draw = l->data;
      if (timestamp - draw->timestamp < GDK_DRAW_REGION_MIN_DURATION)
        progress = 0.0;
      else if (timestamp - draw->timestamp < GDK_DRAW_REGION_MAX_DURATION)
        progress = (double) (timestamp - draw->timestamp - GDK_DRAW_REGION_MIN_DURATION)
                   / (GDK_DRAW_REGION_MAX_DURATION - GDK_DRAW_REGION_MIN_DURATION);
      else
        continue;

      gtk_debug_updates_print (updates, draw->region, "Painting with progress %g", progress);
      for (i = 0; i < cairo_region_num_rectangles (draw->region); i++)
        {
          cairo_region_get_rectangle (draw->region, i, &rect);
          gtk_snapshot_append_color (snapshot,
                                     &(GdkRGBA) { 1, 0, 0, 0.4 * (1 - progress) },
                                     &GRAPHENE_RECT_INIT(rect.x, rect.y, rect.width, rect.height),
                                     "Debug Updates<%g>", progress);
        }
    }
}