File: gtkgraphicsoffload.c

package info (click to toggle)
gtk4 4.20.3%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 187,060 kB
  • sloc: ansic: 779,084; xml: 3,093; javascript: 3,054; python: 1,911; java: 752; sh: 682; makefile: 315; perl: 162; cpp: 21
file content (456 lines) | stat: -rw-r--r-- 12,823 bytes parent folder | download | duplicates (3)
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
/* GTK - The GIMP Toolkit
 *
 * Copyright (C) 2023  Red Hat, Inc.
 *
 * 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/>.
 *
 * Author:
 *      Matthias Clasen <mclasen@redhat.com>
 */

#include "config.h"

#include "gtkbinlayout.h"
#include "gtkgraphicsoffload.h"
#include "gtksnapshotprivate.h"
#include "gtkwidgetprivate.h"
#include "gtkprivate.h"
#include "gdk/gdksurfaceprivate.h"
#include "gdk/gdksubsurfaceprivate.h"
#include "gdk/gdkrgbaprivate.h"
#include "gtktypebuiltins.h"

/**
 * GtkGraphicsOffload:
 *
 * Bypasses gsk rendering by passing the content of its child directly to the compositor.
 *
 * Graphics offload is an optimization to reduce overhead and battery use that is
 * most useful for video content. It only works on some platforms and in certain
 * situations. GTK will automatically fall back to normal rendering if it doesn't.
 *
 * Graphics offload is most efficient if there are no controls drawn on top of the
 * video content.
 *
 * You should consider using graphics offload for your main widget if it shows
 * frequently changing content (such as a video, or a VM display) and you provide
 * the content in the form of dmabuf textures (see [class@Gdk.DmabufTextureBuilder]),
 * in particular if it may be fullscreen.
 *
 * Numerous factors can prohibit graphics offload:
 *
 * - Unsupported platforms. Currently, graphics offload only works on Linux with Wayland.
 *
 * - Clipping, such as rounded corners that cause the video content to not be rectangular
 *
 * - Unsupported dmabuf formats (see [method@Gdk.Display.get_dmabuf_formats])
 *
 * - Translucent video content (content with an alpha channel, even if it isn't used)
 *
 * - Transforms that are more complex than translations and scales
 *
 * - Filters such as opacity, grayscale or similar
 *
 * To investigate problems related graphics offload, GTK offers debug flags to print
 * out information about graphics offload and dmabuf use:
 *
 *     GDK_DEBUG=offload
 *     GDK_DEBUG=dmabuf
 *
 * The GTK inspector provides a visual debugging tool for graphics offload.
 *
 * Since: 4.14
 */

struct _GtkGraphicsOffload
{
  GtkWidget parent_instance;

  GtkWidget *child;

  GdkSubsurface *subsurface;

  GtkGraphicsOffloadEnabled enabled;
  gboolean black_background;
};

struct _GtkGraphicsOffloadClass
{
  GtkWidgetClass parent_class;
};

enum
{
  PROP_0,
  PROP_CHILD,
  PROP_ENABLED,
  PROP_BLACK_BACKGROUND,
  LAST_PROP,
};

static GParamSpec *properties[LAST_PROP] = { NULL, };

G_DEFINE_TYPE (GtkGraphicsOffload, gtk_graphics_offload, GTK_TYPE_WIDGET)

static void
gtk_graphics_offload_init (GtkGraphicsOffload *self)
{
  self->enabled = GTK_GRAPHICS_OFFLOAD_ENABLED;
}

static void
gtk_graphics_offload_dispose (GObject *object)
{
  GtkGraphicsOffload *self = GTK_GRAPHICS_OFFLOAD (object);

  g_clear_pointer (&self->child, gtk_widget_unparent);

  G_OBJECT_CLASS (gtk_graphics_offload_parent_class)->dispose (object);
}

static void
gtk_graphics_offload_set_property (GObject      *object,
                                   guint         property_id,
                                   const GValue *value,
                                   GParamSpec   *pspec)
{
  GtkGraphicsOffload *self = GTK_GRAPHICS_OFFLOAD (object);

  switch (property_id)
    {
    case PROP_CHILD:
      gtk_graphics_offload_set_child (self, g_value_get_object (value));
      break;

    case PROP_ENABLED:
      gtk_graphics_offload_set_enabled (self, g_value_get_enum (value));
      break;

    case PROP_BLACK_BACKGROUND:
      gtk_graphics_offload_set_black_background (self, g_value_get_boolean (value));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
}

static void
gtk_graphics_offload_get_property (GObject    *object,
                                   guint       property_id,
                                   GValue     *value,
                                   GParamSpec *pspec)
{
  GtkGraphicsOffload *self = GTK_GRAPHICS_OFFLOAD (object);

  switch (property_id)
    {
    case PROP_CHILD:
      g_value_set_object (value, gtk_graphics_offload_get_child (self));
      break;

    case PROP_ENABLED:
      g_value_set_enum (value, gtk_graphics_offload_get_enabled (self));
      break;

    case PROP_BLACK_BACKGROUND:
      g_value_set_boolean (value, gtk_graphics_offload_get_black_background (self));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
}

static void
sync_subsurface (GtkGraphicsOffload *self)
{
  GtkWidget *widget = GTK_WIDGET (self);

  if (gtk_widget_get_realized (widget) && self->enabled != GTK_GRAPHICS_OFFLOAD_DISABLED)
    {
      if (!self->subsurface)
        self->subsurface = gdk_surface_create_subsurface (gtk_widget_get_surface (widget));
    }
  else
    {
      g_clear_object (&self->subsurface);
    }
}

static void
gtk_graphics_offload_realize (GtkWidget *widget)
{
  GtkGraphicsOffload *self = GTK_GRAPHICS_OFFLOAD (widget);

  GTK_WIDGET_CLASS (gtk_graphics_offload_parent_class)->realize (widget);

  sync_subsurface (self);
}

static void
gtk_graphics_offload_unrealize (GtkWidget *widget)
{
  GtkGraphicsOffload *self = GTK_GRAPHICS_OFFLOAD (widget);

  GTK_WIDGET_CLASS (gtk_graphics_offload_parent_class)->unrealize (widget);

  sync_subsurface (self);
}

static void
gtk_graphics_offload_snapshot (GtkWidget   *widget,
                               GtkSnapshot *snapshot)
{
  GtkGraphicsOffload *self = GTK_GRAPHICS_OFFLOAD (widget);

  if (self->subsurface)
    gtk_snapshot_push_subsurface (snapshot, self->subsurface);

  if (self->black_background)
    gtk_snapshot_append_color (snapshot,
                               &GDK_RGBA_BLACK,
                               &GRAPHENE_RECT_INIT (0, 0,
                                                    gtk_widget_get_width (widget),
                                                    gtk_widget_get_height (widget)));

  if (self->child)
    gtk_widget_snapshot_child (widget, self->child, snapshot);

  if (self->subsurface)
    gtk_snapshot_pop (snapshot);
}

static void
gtk_graphics_offload_class_init (GtkGraphicsOffloadClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);

  object_class->dispose = gtk_graphics_offload_dispose;
  object_class->set_property = gtk_graphics_offload_set_property;
  object_class->get_property = gtk_graphics_offload_get_property;

  widget_class->realize = gtk_graphics_offload_realize;
  widget_class->unrealize = gtk_graphics_offload_unrealize;
  widget_class->snapshot = gtk_graphics_offload_snapshot;

  /**
   * GtkGraphicsOffload:child:
   *
   * The child widget.
   *
   * Since: 4.14
   */
  properties[PROP_CHILD] = g_param_spec_object ("child", NULL, NULL,
                                                GTK_TYPE_WIDGET,
                                                GTK_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);

  /**
   * GtkGraphicsOffload:enabled:
   *
   * Whether graphics offload is enabled.
   *
   * Since: 4.14
   */
  properties[PROP_ENABLED] = g_param_spec_enum ("enabled", NULL, NULL,
                                                GTK_TYPE_GRAPHICS_OFFLOAD_ENABLED,
                                                GTK_GRAPHICS_OFFLOAD_ENABLED,
                                                GTK_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);

  /**
   * GtkGraphicsOffload:black-background:
   *
   * Whether to draw a black background.
   *
   * Since: 4.16
   */
  properties[PROP_BLACK_BACKGROUND] = g_param_spec_boolean ("black-background", NULL, NULL,
                                                            FALSE,
                                                            GTK_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);

  g_object_class_install_properties (object_class, LAST_PROP, properties);

  gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BIN_LAYOUT);
  gtk_widget_class_set_css_name (widget_class, "graphicsoffload");
}

/**
 * gtk_graphics_offload_new:
 * @child: (nullable): the child widget
 *
 * Creates a new GtkGraphicsOffload widget.
 *
 * Returns: the new widget
 *
 * Since: 4.14
 */
GtkWidget *
gtk_graphics_offload_new (GtkWidget *child)
{
  return g_object_new (GTK_TYPE_GRAPHICS_OFFLOAD,
                       "child", child,
                       NULL);
}

/**
 * gtk_graphics_offload_set_child:
 * @self: a `GtkGraphicsOffload`
 * @child: (nullable): the child widget
 *
 * Sets the child of @self.
 *
 * Since: 4.14
 */
void
gtk_graphics_offload_set_child (GtkGraphicsOffload *self,
                                GtkWidget           *child)
{
  g_return_if_fail (GTK_IS_GRAPHICS_OFFLOAD (self));
  g_return_if_fail (child == NULL || self->child == child || (GTK_IS_WIDGET (child) &&gtk_widget_get_parent (child) == NULL));

  if (self->child == child)
    return;

  g_clear_pointer (&self->child, gtk_widget_unparent);

  if (child)
    {
      self->child = child;
      gtk_widget_set_parent (child, GTK_WIDGET (self));
    }

  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_CHILD]);
}

/**
 * gtk_graphics_offload_get_child:
 * @self: a `GtkGraphicsOffload`
 *
 * Gets the child of @self.
 *
 * Returns: (nullable) (transfer none): the child widget
 *
 * Since: 4.14
 */
GtkWidget *
gtk_graphics_offload_get_child (GtkGraphicsOffload *self)
{
  g_return_val_if_fail (GTK_IS_GRAPHICS_OFFLOAD (self), NULL);

  return self->child;
}

/**
 * gtk_graphics_offload_set_enabled:
 * @self: a `GtkGraphicsOffload`
 * @enabled: whether to enable offload
 *
 * Sets whether this GtkGraphicsOffload widget will attempt
 * to offload the content of its child widget.
 *
 * Since: 4.14
 */
void
gtk_graphics_offload_set_enabled (GtkGraphicsOffload        *self,
                                  GtkGraphicsOffloadEnabled  enabled)
{
  g_return_if_fail (GTK_IS_GRAPHICS_OFFLOAD (self));

  if (self->enabled == enabled)
    return;

  self->enabled = enabled;

  sync_subsurface (self);
  gtk_widget_queue_draw (GTK_WIDGET (self));

  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_ENABLED]);
}

/**
 * gtk_graphics_offload_get_enabled:
 * @self: a `GtkGraphicsOffload`
 *
 * Returns whether offload is enabled for @self.
 *
 * Returns: whether offload is enabled
 *
 * Since: 4.14
 */
GtkGraphicsOffloadEnabled
gtk_graphics_offload_get_enabled (GtkGraphicsOffload *self)
{
  g_return_val_if_fail (GTK_IS_GRAPHICS_OFFLOAD (self), TRUE);

  return self->enabled;
}

/**
 * gtk_graphics_offload_set_black_background:
 * @self: a `GtkGraphicsOffload`
 * @value: whether to draw a black background behind the content
 *
 * Sets whether this GtkGraphicsOffload widget will draw a black
 * background.
 *
 * A main use case for this is **_letterboxing_** where black bars are
 * visible next to the content if the aspect ratio of the content does
 * not match the dimensions of the monitor.
 *
 * Using this property for letterboxing instead of CSS allows compositors
 * to show content with maximum efficiency, using direct scanout to avoid
 * extra copies in the compositor.
 *
 * On Wayland, this is implemented using the
 * [single-pixel buffer](https://wayland.app/protocols/single-pixel-buffer-v1)
 * protocol.
 *
 * Since: 4.16
 */
void
gtk_graphics_offload_set_black_background (GtkGraphicsOffload *self,
                                           gboolean            value)
{
  g_return_if_fail (GTK_IS_GRAPHICS_OFFLOAD (self));

  if (self->black_background == value)
    return;

  self->black_background = value;

  gtk_widget_queue_draw (GTK_WIDGET (self));

  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_BLACK_BACKGROUND]);
}

/**
 * gtk_graphics_offload_get_black_background:
 * @self: a `GtkGraphicsOffload`
 *
 * Returns whether the widget draws a black background.
 *
 * See [method@Gtk.GraphicsOffload.set_black_background].
 *
 * Returns: `TRUE` if black background is drawn
 *
 * Since: 4.16
 */
gboolean
gtk_graphics_offload_get_black_background (GtkGraphicsOffload *self)
{
  g_return_val_if_fail (GTK_IS_GRAPHICS_OFFLOAD (self), FALSE);

  return self->black_background;
}