gskrenderer.c 25.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* GSK - The GTK Scene Kit
 *
 * Copyright 2016  Endless
 *
 * 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/>.
 */

/**
 * SECTION:GskRenderer
21 22
 * @Title: GskRenderer
 * @Short_description: Renders a scene
23
 *
24 25 26 27 28 29 30 31 32 33
 * #GskRenderer is a class that renders a scene graph defined via a
 * tree of #GskRenderNode instances.
 *
 * Typically you will use a #GskRenderer instance with a #GdkDrawingContext
 * associated to a #GdkWindow, and call gsk_renderer_render() with the
 * drawing context and the scene to be rendered.
 *
 * It is necessary to realize a #GskRenderer instance using gsk_renderer_realize()
 * before calling gsk_renderer_render(), in order to create the appropriate
 * windowing system resources needed to render the scene.
34 35 36 37 38 39
 */

#include "config.h"

#include "gskrendererprivate.h"

40
#include "gskcairorendererprivate.h"
41 42
#include "gskdebugprivate.h"
#include "gskglrendererprivate.h"
43
#include "gskprofilerprivate.h"
44 45 46 47 48 49 50 51 52 53 54 55 56 57
#include "gskrendernodeprivate.h"

#include "gskenumtypes.h"

#include <graphene-gobject.h>
#include <cairo-gobject.h>
#include <gdk/gdk.h>

#ifdef GDK_WINDOWING_X11
#include <gdk/x11/gdkx.h>
#endif
#ifdef GDK_WINDOWING_WAYLAND
#include <gdk/wayland/gdkwayland.h>
#endif
58 59 60
#ifdef GDK_WINDOWING_BROADWAY
#include "gskbroadwayrendererprivate.h"
#endif
Matthias Clasen's avatar
Matthias Clasen committed
61
#ifdef GDK_RENDERING_VULKAN
62 63
#include "gskvulkanrendererprivate.h"
#endif
64 65 66 67 68 69 70 71 72 73

typedef struct
{
  GObject parent_instance;

  graphene_rect_t viewport;

  GskScalingFilter min_filter;
  GskScalingFilter mag_filter;

74 75
  GdkWindow *window;
  GdkDrawingContext *drawing_context;
76
  GskRenderNode *root_node;
77
  GdkDisplay *display;
78

79 80
  GskProfiler *profiler;

81 82 83 84 85 86 87
  gboolean is_realized : 1;
} GskRendererPrivate;

G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GskRenderer, gsk_renderer, G_TYPE_OBJECT)

enum {
  PROP_VIEWPORT = 1,
88
  PROP_WINDOW,
89
  PROP_DISPLAY,
90
  PROP_DRAWING_CONTEXT,
91 92 93 94 95 96 97 98 99 100

  N_PROPS
};

static GParamSpec *gsk_renderer_properties[N_PROPS];

#define GSK_RENDERER_WARN_NOT_IMPLEMENTED_METHOD(obj,method) \
  g_critical ("Renderer of type '%s' does not implement GskRenderer::" # method, G_OBJECT_TYPE_NAME (obj))

static gboolean
101 102 103
gsk_renderer_real_realize (GskRenderer  *self,
                           GdkWindow    *window,
                           GError      **error)
104 105 106 107 108 109 110 111 112 113 114
{
  GSK_RENDERER_WARN_NOT_IMPLEMENTED_METHOD (self, realize);
  return FALSE;
}

static void
gsk_renderer_real_unrealize (GskRenderer *self)
{
  GSK_RENDERER_WARN_NOT_IMPLEMENTED_METHOD (self, unrealize);
}

Benjamin Otte's avatar
Benjamin Otte committed
115
static GdkTexture *
116 117 118 119 120 121 122 123
gsk_renderer_real_render_texture (GskRenderer           *self,
                                  GskRenderNode         *root,
                                  const graphene_rect_t *viewport)
{
  GSK_RENDERER_WARN_NOT_IMPLEMENTED_METHOD (self, render_texture);
  return NULL;
}

124 125 126 127 128 129 130
static GdkDrawingContext *
gsk_renderer_real_begin_draw_frame (GskRenderer          *self,
                                    const cairo_region_t *region)
{
  GskRendererPrivate *priv = gsk_renderer_get_instance_private (self);

  return gdk_window_begin_draw_frame (priv->window,
131
                                      NULL,
132 133 134 135 136 137 138 139 140 141 142 143 144
                                      region);
}

static void
gsk_renderer_real_end_draw_frame (GskRenderer       *self,
                                  GdkDrawingContext *context)
{
  GskRendererPrivate *priv = gsk_renderer_get_instance_private (self);

  gdk_window_end_draw_frame (priv->window,
                             context);
}

145
static void
146
gsk_renderer_real_render (GskRenderer *self,
147
                          GskRenderNode *root)
148 149 150 151
{
  GSK_RENDERER_WARN_NOT_IMPLEMENTED_METHOD (self, render);
}

152 153 154 155 156 157 158
static cairo_surface_t *
gsk_renderer_real_create_cairo_surface (GskRenderer    *self,
                                        cairo_format_t  format,
                                        int             width,
                                        int             height)
{
  GskRendererPrivate *priv = gsk_renderer_get_instance_private (self);
159
  int scale_factor = priv->window ? gdk_window_get_scale_factor (priv->window) : 1;
160 161 162 163 164 165 166 167 168 169
  int real_width = width * scale_factor;
  int real_height = height * scale_factor;

  cairo_surface_t *res = cairo_image_surface_create (format, real_width, real_height);

  cairo_surface_set_device_scale (res, scale_factor, scale_factor);

  return res;
}

170 171 172 173 174 175
static void
gsk_renderer_dispose (GObject *gobject)
{
  GskRenderer *self = GSK_RENDERER (gobject);
  GskRendererPrivate *priv = gsk_renderer_get_instance_private (self);

176 177 178
  /* We can't just unrealize here because superclasses have already run dispose.
   * So we insist that unrealize must be called before unreffing. */
  g_assert (!priv->is_realized);
179

180
  g_clear_object (&priv->profiler);
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
  g_clear_object (&priv->display);

  G_OBJECT_CLASS (gsk_renderer_parent_class)->dispose (gobject);
}

static void
gsk_renderer_set_property (GObject      *gobject,
                           guint         prop_id,
                           const GValue *value,
                           GParamSpec   *pspec)
{
  GskRenderer *self = GSK_RENDERER (gobject);
  GskRendererPrivate *priv = gsk_renderer_get_instance_private (self);

  switch (prop_id)
    {
    case PROP_VIEWPORT:
      gsk_renderer_set_viewport (self, g_value_get_boxed (value));
      break;

    case PROP_DISPLAY:
202
      /* Construct-only */
203 204
      priv->display = g_value_dup_object (value);
      break;
205 206 207 208

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
      break;
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
    }
}

static void
gsk_renderer_get_property (GObject    *gobject,
                           guint       prop_id,
                           GValue     *value,
                           GParamSpec *pspec)
{
  GskRenderer *self = GSK_RENDERER (gobject);
  GskRendererPrivate *priv = gsk_renderer_get_instance_private (self);

  switch (prop_id)
    {
    case PROP_VIEWPORT:
      g_value_set_boxed (value, &priv->viewport);
      break;

    case PROP_WINDOW:
      g_value_set_object (value, priv->window);
      break;

231 232 233 234 235 236
    case PROP_DRAWING_CONTEXT:
      g_value_set_object (value, priv->drawing_context);
      break;

    case PROP_DISPLAY:
      g_value_set_object (value, priv->display);
237
      break;
238 239 240 241

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
      break;
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
    }
}

static void
gsk_renderer_constructed (GObject *gobject)
{
  GskRenderer *self = GSK_RENDERER (gobject);
  GskRendererPrivate *priv = gsk_renderer_get_instance_private (self);

  if (priv->display == NULL)
    {
      GdkDisplayManager *manager = gdk_display_manager_get ();

      priv->display = gdk_display_manager_get_default_display (manager);
      g_assert (priv->display != NULL);
    }

  G_OBJECT_CLASS (gsk_renderer_parent_class)->constructed (gobject);
}

static void
gsk_renderer_class_init (GskRendererClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  klass->realize = gsk_renderer_real_realize;
  klass->unrealize = gsk_renderer_real_unrealize;
269 270
  klass->begin_draw_frame = gsk_renderer_real_begin_draw_frame;
  klass->end_draw_frame = gsk_renderer_real_end_draw_frame;
271
  klass->render = gsk_renderer_real_render;
272
  klass->render_texture = gsk_renderer_real_render_texture;
273
  klass->create_cairo_surface = gsk_renderer_real_create_cairo_surface;
274 275 276 277 278 279 280 281 282 283 284

  gobject_class->constructed = gsk_renderer_constructed;
  gobject_class->set_property = gsk_renderer_set_property;
  gobject_class->get_property = gsk_renderer_get_property;
  gobject_class->dispose = gsk_renderer_dispose;

  /**
   * GskRenderer:viewport:
   *
   * The visible area used by the #GskRenderer to render its contents.
   *
285
   * Since: 3.90
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
   */
  gsk_renderer_properties[PROP_VIEWPORT] =
    g_param_spec_boxed ("viewport",
			"Viewport",
			"The visible area used by the renderer",
			GRAPHENE_TYPE_RECT,
			G_PARAM_READWRITE |
			G_PARAM_STATIC_STRINGS |
			G_PARAM_EXPLICIT_NOTIFY);

  /**
   * GskRenderer:display:
   *
   * The #GdkDisplay used by the #GskRenderer.
   *
301
   * Since: 3.90
302 303 304 305 306 307 308 309 310 311 312 313 314
   */
  gsk_renderer_properties[PROP_DISPLAY] =
    g_param_spec_object ("display",
			 "Display",
			 "The GdkDisplay object used by the renderer",
			 GDK_TYPE_DISPLAY,
			 G_PARAM_READWRITE |
			 G_PARAM_CONSTRUCT_ONLY |
			 G_PARAM_STATIC_STRINGS);

  gsk_renderer_properties[PROP_WINDOW] =
    g_param_spec_object ("window",
                         "Window",
315
                         "The window associated to the renderer",
316
                         GDK_TYPE_WINDOW,
317
                         G_PARAM_READABLE |
318 319 320 321 322 323 324
                         G_PARAM_STATIC_STRINGS);

  /**
   * GskRenderer:drawing-context:
   *
   * The drawing context used when rendering.
   *
325
   * Since: 3.90
326 327 328 329 330 331 332 333
   */
  gsk_renderer_properties[PROP_DRAWING_CONTEXT] =
    g_param_spec_object ("drawing-context",
                         "Drawing Context",
                         "The drawing context used by the renderer",
                         GDK_TYPE_DRAWING_CONTEXT,
                         G_PARAM_READABLE |
                         G_PARAM_STATIC_STRINGS);
334 335 336 337 338 339 340 341 342

  g_object_class_install_properties (gobject_class, N_PROPS, gsk_renderer_properties);
}

static void
gsk_renderer_init (GskRenderer *self)
{
  GskRendererPrivate *priv = gsk_renderer_get_instance_private (self);

343
  priv->profiler = gsk_profiler_new ();
344 345 346 347 348 349 350 351 352 353
}

/**
 * gsk_renderer_set_viewport:
 * @renderer: a #GskRenderer
 * @viewport: (nullable): the viewport rectangle used by the @renderer
 *
 * Sets the visible rectangle to be used as the viewport for
 * the rendering.
 *
354
 * Since: 3.90
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
 */
void
gsk_renderer_set_viewport (GskRenderer           *renderer,
                           const graphene_rect_t *viewport)
{
  GskRendererPrivate *priv = gsk_renderer_get_instance_private (renderer);

  g_return_if_fail (GSK_IS_RENDERER (renderer));

  if (viewport == NULL)
    {
      graphene_rect_init (&priv->viewport, 0.f, 0.f, 0.f, 0.f);
      g_object_notify_by_pspec (G_OBJECT (renderer), gsk_renderer_properties[PROP_VIEWPORT]);
      return;
    }

  if (graphene_rect_equal (viewport, &priv->viewport))
    return;

  graphene_rect_init_from_rect (&priv->viewport, viewport);

  g_object_notify_by_pspec (G_OBJECT (renderer), gsk_renderer_properties[PROP_VIEWPORT]);
}

/**
 * gsk_renderer_get_viewport:
 * @renderer: a #GskRenderer
 * @viewport: (out caller-allocates): return location for the viewport rectangle
 *
 * Retrieves the viewport of the #GskRenderer.
 *
386
 * Since: 3.90
387 388 389 390 391 392 393 394 395 396 397 398 399 400
 */
void
gsk_renderer_get_viewport (GskRenderer     *renderer,
                           graphene_rect_t *viewport)
{
  GskRendererPrivate *priv = gsk_renderer_get_instance_private (renderer);

  g_return_if_fail (GSK_IS_RENDERER (renderer));
  g_return_if_fail (viewport != NULL);

  graphene_rect_init_from_rect (viewport, &priv->viewport);
}

/**
401
 * gsk_renderer_get_window:
402 403
 * @renderer: a #GskRenderer
 *
404 405
 * Retrieves the #GdkWindow set using gsk_renderer_realize(). If the renderer
 * has not been realized yet, %NULL will be returned.
406
 *
407
 * Returns: (transfer none) (nullable): a #GdkWindow
408
 *
409
 * Since: 3.90
410
 */
411 412
GdkWindow *
gsk_renderer_get_window (GskRenderer *renderer)
413 414 415 416 417
{
  GskRendererPrivate *priv = gsk_renderer_get_instance_private (renderer);

  g_return_val_if_fail (GSK_IS_RENDERER (renderer), NULL);

418
  return priv->window;
419 420
}

421
/*< private >
422 423 424
 * gsk_renderer_get_root_node:
 * @renderer: a #GskRenderer
 *
425
 * Retrieves the #GskRenderNode used by @renderer.
426 427 428 429 430 431 432 433 434 435 436 437 438 439
 *
 * Returns: (transfer none) (nullable): a #GskRenderNode
 */
GskRenderNode *
gsk_renderer_get_root_node (GskRenderer *renderer)
{
  GskRendererPrivate *priv = gsk_renderer_get_instance_private (renderer);

  g_return_val_if_fail (GSK_IS_RENDERER (renderer), NULL);

  return priv->root_node;
}

/*< private >
440
 * gsk_renderer_get_drawing_context:
441 442
 * @renderer: a #GskRenderer
 *
443
 * Retrieves the #GdkDrawingContext used by @renderer.
444
 *
445
 * Returns: (transfer none) (nullable): a #GdkDrawingContext
446
 */
447 448
GdkDrawingContext *
gsk_renderer_get_drawing_context (GskRenderer *renderer)
449 450 451
{
  GskRendererPrivate *priv = gsk_renderer_get_instance_private (renderer);

452
  g_return_val_if_fail (GSK_IS_RENDERER (renderer), NULL);
453

454
  return priv->drawing_context;
455 456 457
}

/**
458
 * gsk_renderer_get_display:
459 460
 * @renderer: a #GskRenderer
 *
461
 * Retrieves the #GdkDisplay used when creating the #GskRenderer.
462
 *
463
 * Returns: (transfer none): a #GdkDisplay
464
 *
465
 * Since: 3.90
466
 */
467 468
GdkDisplay *
gsk_renderer_get_display (GskRenderer *renderer)
469 470 471
{
  GskRendererPrivate *priv = gsk_renderer_get_instance_private (renderer);

472
  g_return_val_if_fail (GSK_IS_RENDERER (renderer), NULL);
473

474
  return priv->display;
475 476
}

477 478
/*< private >
 * gsk_renderer_is_realized:
479 480
 * @renderer: a #GskRenderer
 *
481
 * Checks whether the @renderer is realized or not.
482
 *
483
 * Returns: %TRUE if the #GskRenderer was realized, and %FALSE otherwise
484
 *
485
 * Since: 3.90
486
 */
487 488
gboolean
gsk_renderer_is_realized (GskRenderer *renderer)
489 490 491
{
  GskRendererPrivate *priv = gsk_renderer_get_instance_private (renderer);

492
  g_return_val_if_fail (GSK_IS_RENDERER (renderer), FALSE);
493

494
  return priv->is_realized;
495 496 497 498 499
}

/**
 * gsk_renderer_realize:
 * @renderer: a #GskRenderer
500
 * @window: the #GdkWindow renderer will be used on
501
 * @error: return location for an error
502 503 504 505
 *
 * Creates the resources needed by the @renderer to render the scene
 * graph.
 *
506
 * Since: 3.90
507 508
 */
gboolean
509 510 511
gsk_renderer_realize (GskRenderer  *renderer,
                      GdkWindow    *window,
                      GError      **error)
512 513 514 515
{
  GskRendererPrivate *priv = gsk_renderer_get_instance_private (renderer);

  g_return_val_if_fail (GSK_IS_RENDERER (renderer), FALSE);
516 517
  g_return_val_if_fail (!gsk_renderer_is_realized (renderer), FALSE);
  g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
518
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
519

520
  priv->window = g_object_ref (window);
521

522
  if (!GSK_RENDERER_GET_CLASS (renderer)->realize (renderer, window, error))
523 524 525 526
    {
      g_clear_object (&priv->window);
      return FALSE;
    }
527

528 529
  priv->is_realized = TRUE;
  return TRUE;
530 531 532 533 534 535 536 537
}

/**
 * gsk_renderer_unrealize:
 * @renderer: a #GskRenderer
 *
 * Releases all the resources created by gsk_renderer_realize().
 *
538
 * Since: 3.90
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
 */
void
gsk_renderer_unrealize (GskRenderer *renderer)
{
  GskRendererPrivate *priv = gsk_renderer_get_instance_private (renderer);

  g_return_if_fail (GSK_IS_RENDERER (renderer));

  if (!priv->is_realized)
    return;

  GSK_RENDERER_GET_CLASS (renderer)->unrealize (renderer);

  priv->is_realized = FALSE;
}

555 556 557 558 559 560 561
/**
 * gsk_renderer_render_texture:
 * @renderer: a realized #GdkRenderer
 * @root: a #GskRenderNode
 * @viewport: (allow-none): the section to draw or %NULL to use @root's bounds
 *
 * Renders the scene graph, described by a tree of #GskRenderNode instances,
Benjamin Otte's avatar
Benjamin Otte committed
562
 * to a #GdkTexture.
563 564 565 566 567 568 569
 *
 * The @renderer will acquire a reference on the #GskRenderNode tree while
 * the rendering is in progress, and will make the tree immutable.
 *
 * If you want to apply any transformations to @root, you should put it into a 
 * transform node and pass that node instead.
 *
Benjamin Otte's avatar
Benjamin Otte committed
570
 * Returns: (transfer full): a #GdkTexture with the rendered contents of @root.
571 572 573
 *
 * Since: 3.90
 */
Benjamin Otte's avatar
Benjamin Otte committed
574
GdkTexture *
575 576 577 578 579 580
gsk_renderer_render_texture (GskRenderer           *renderer,
                             GskRenderNode         *root,
                             const graphene_rect_t *viewport)
{
  GskRendererPrivate *priv = gsk_renderer_get_instance_private (renderer);
  graphene_rect_t real_viewport;
Benjamin Otte's avatar
Benjamin Otte committed
581
  GdkTexture *texture;
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619

  g_return_val_if_fail (GSK_IS_RENDERER (renderer), NULL);
  g_return_val_if_fail (priv->is_realized, NULL);
  g_return_val_if_fail (GSK_IS_RENDER_NODE (root), NULL);
  g_return_val_if_fail (priv->root_node == NULL, NULL);

  priv->root_node = gsk_render_node_ref (root);

  if (viewport == NULL)
    {
      gsk_render_node_get_bounds (root, &real_viewport);
      viewport = &real_viewport;
    }

  texture = GSK_RENDERER_GET_CLASS (renderer)->render_texture (renderer, root, viewport);

#ifdef G_ENABLE_DEBUG
  if (GSK_DEBUG_CHECK (RENDERER))
    {
      GString *buf = g_string_new ("*** Texture stats ***\n\n");

      gsk_profiler_append_counters (priv->profiler, buf);
      g_string_append_c (buf, '\n');

      gsk_profiler_append_timers (priv->profiler, buf);
      g_string_append_c (buf, '\n');

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

      g_string_free (buf, TRUE);
    }
#endif

  g_clear_pointer (&priv->root_node, gsk_render_node_unref);

  return texture;
}

620 621
/**
 * gsk_renderer_render:
622 623
 * @renderer: a #GskRenderer
 * @root: a #GskRenderNode
624
 * @context: The drawing context created via gsk_renderer_begin_draw_frame()
625 626 627
 *
 * Renders the scene graph, described by a tree of #GskRenderNode instances,
 * using the given #GdkDrawingContext.
628
 *
629
 * The @renderer will acquire a reference on the #GskRenderNode tree while
630
 * the rendering is in progress.
631
 *
632
 * Since: 3.90
633 634
 */
void
635 636 637
gsk_renderer_render (GskRenderer       *renderer,
                     GskRenderNode     *root,
                     GdkDrawingContext *context)
638 639 640 641 642
{
  GskRendererPrivate *priv = gsk_renderer_get_instance_private (renderer);

  g_return_if_fail (GSK_IS_RENDERER (renderer));
  g_return_if_fail (priv->is_realized);
643
  g_return_if_fail (GSK_IS_RENDER_NODE (root));
644
  g_return_if_fail (priv->root_node == NULL);
645
  g_return_if_fail (GDK_IS_DRAWING_CONTEXT (context));
646
  g_return_if_fail (context == priv->drawing_context);
647

648
  priv->root_node = gsk_render_node_ref (root);
649

650
  GSK_RENDERER_GET_CLASS (renderer)->render (renderer, root);
651

652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
#ifdef G_ENABLE_DEBUG
  if (GSK_DEBUG_CHECK (RENDERER))
    {
      GString *buf = g_string_new ("*** Frame stats ***\n\n");

      gsk_profiler_append_counters (priv->profiler, buf);
      g_string_append_c (buf, '\n');

      gsk_profiler_append_timers (priv->profiler, buf);
      g_string_append_c (buf, '\n');

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

      g_string_free (buf, TRUE);
    }
#endif

669
  g_clear_pointer (&priv->root_node, gsk_render_node_unref);
670 671
}

672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
/*< private >
 * gsk_renderer_get_profiler:
 * @renderer: a #GskRenderer
 *
 * Retrieves a pointer to the GskProfiler instance of the renderer.
 *
 * Returns: (transfer none): the profiler
 */
GskProfiler *
gsk_renderer_get_profiler (GskRenderer *renderer)
{
  GskRendererPrivate *priv = gsk_renderer_get_instance_private (renderer);

  g_return_val_if_fail (GSK_IS_RENDERER (renderer), NULL);

  return priv->profiler;
}

690 691 692 693 694 695 696 697 698 699 700 701
static GType
get_renderer_for_name (const char *renderer_name)
{
  if (renderer_name == NULL)
    return G_TYPE_INVALID;
  else if (g_ascii_strcasecmp (renderer_name, "cairo") == 0)
    return GSK_TYPE_CAIRO_RENDERER;
  else if (g_ascii_strcasecmp (renderer_name, "opengl") == 0
           || g_ascii_strcasecmp (renderer_name, "gl") == 0)
    return GSK_TYPE_GL_RENDERER;
#ifdef GDK_RENDERING_VULKAN
  else if (g_ascii_strcasecmp (renderer_name, "vulkan") == 0)
702
    return GSK_TYPE_VULKAN_RENDERER;
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
#endif
  else if (g_ascii_strcasecmp (renderer_name, "help") == 0)
    {
      g_print ("Supported arguments for GSK_RENDERER environment variable:\n");
      g_print ("   cairo - Use the Cairo fallback renderer\n");
      g_print ("  opengl - Use the default OpenGL renderer\n");
#ifdef GDK_RENDERING_VULKAN
      g_print ("  vulkan - Use the Vulkan renderer\n");
#endif
      g_print ("    help - Print this help\n\n");
      g_print ("Other arguments will cause a warning and be ignored.\n");
    }
  else
    {
      g_warning ("Unrecognized renderer \"%s\". Try GSK_RENDERER=help", renderer_name);
    }

  return G_TYPE_INVALID;
}

static GType
get_renderer_for_display (GdkWindow *window)
{
  GdkDisplay *display = gdk_window_get_display (window);
  const char *renderer_name;

729
  renderer_name = g_object_get_data (G_OBJECT (display), "gsk-renderer");
730 731 732
  return get_renderer_for_name (renderer_name);
}

733 734
static GType
get_renderer_for_env_var (GdkWindow *window)
735
{
736
  static GType env_var_type = G_TYPE_NONE;
737

738
  if (env_var_type == G_TYPE_NONE)
739
    {
740
      const char *renderer_name = g_getenv ("GSK_RENDERER");
741
      env_var_type = get_renderer_for_name (renderer_name);
742 743
    }

744
  return env_var_type;
745 746 747 748 749
}

static GType
get_renderer_for_backend (GdkWindow *window)
{
750
#ifdef GDK_WINDOWING_X11
751 752
  if (GDK_IS_X11_WINDOW (window))
    return GSK_TYPE_GL_RENDERER; 
753 754
#endif
#ifdef GDK_WINDOWING_WAYLAND
755 756
  if (GDK_IS_WAYLAND_WINDOW (window))
    return GSK_TYPE_GL_RENDERER;
757
#endif
758 759 760 761
#ifdef GDK_WINDOWING_BROADWAY
  if (GDK_IS_BROADWAY_WINDOW (window))
    return GSK_TYPE_BROADWAY_RENDERER;
#endif
762

763 764 765 766 767 768 769 770 771 772
  return G_TYPE_INVALID;
}

static GType
get_renderer_fallback (GdkWindow *window)
{
  return GSK_TYPE_CAIRO_RENDERER;
}

static struct {
773
  gboolean verbose;
774 775
  GType (* get_renderer) (GdkWindow *window);
} renderer_possibilities[] = {
776
  { TRUE,  get_renderer_for_display },
777 778 779
  { TRUE,  get_renderer_for_env_var },
  { FALSE, get_renderer_for_backend },
  { FALSE, get_renderer_fallback },
780
};
781

782 783
/**
 * gsk_renderer_new_for_window:
784
 * @window: a #GdkWindow
785 786 787 788 789 790 791 792 793 794 795 796 797 798
 *
 * Creates an appropriate #GskRenderer instance for the given @window.
 *
 * The renderer will be realized when it is returned.
 *
 * Returns: (transfer full) (nullable): a #GskRenderer
 *
 * Since: 3.90
 */
GskRenderer *
gsk_renderer_new_for_window (GdkWindow *window)
{
  GType renderer_type;
  GskRenderer *renderer;
799
  GError *error = NULL;
800
  gboolean verbose = FALSE;
801 802 803 804 805 806 807 808 809 810
  guint i;

  g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);

  for (i = 0; i < G_N_ELEMENTS (renderer_possibilities); i++)
    {
      renderer_type = renderer_possibilities[i].get_renderer (window);
      if (renderer_type == G_TYPE_INVALID)
        continue;

811 812 813 814
      /* If a renderer is selected that's marked as verbose, start printing
       * information to stdout.
       */
      verbose |= renderer_possibilities[i].verbose;
815 816 817 818
      renderer = g_object_new (renderer_type,
                               "display", gdk_window_get_display (window),
                               NULL);

819
      if (gsk_renderer_realize (renderer, window, &error))
820
        {
821 822 823 824 825 826
          if (verbose || GSK_DEBUG_CHECK (RENDERER))
            {
              g_print ("Using renderer of type '%s' for display '%s'\n",
                       G_OBJECT_TYPE_NAME (renderer),
                       G_OBJECT_TYPE_NAME (window));
            }
827 828 829
          return renderer;
        }

830 831 832 833 834 835 836
      if (verbose || GSK_DEBUG_CHECK (RENDERER))
        {
          g_print ("Failed to realize renderer of type '%s' for window '%s': %s\n",
                   G_OBJECT_TYPE_NAME (renderer),
                   G_OBJECT_TYPE_NAME (window),
                   error->message);
        }
837
      g_object_unref (renderer);
838
      g_clear_error (&error);
839
    }
840

841 842
  g_assert_not_reached ();
  return NULL;
843
}
844

845 846 847 848 849 850 851 852 853 854 855 856
cairo_surface_t *
gsk_renderer_create_cairo_surface (GskRenderer    *renderer,
                                   cairo_format_t  format,
                                   int             width,
                                   int             height)
{
  g_return_val_if_fail (GSK_IS_RENDERER (renderer), NULL);
  g_return_val_if_fail (width > 0 && height > 0, NULL);

  return GSK_RENDERER_GET_CLASS (renderer)->create_cairo_surface (renderer, format, width, height);
}

857 858 859 860 861 862 863 864 865 866 867 868 869
/**
 * gsk_renderer_begin_draw_frame:
 * @renderer: a #GskRenderer
 * @region: the #cairo_region_t that you wish to draw
 *
 * Indicates that you are beginning the process of redrawing @region using
 * @renderer, and provides you with a #GdkDrawingContext to use for this.
 *
 * Returns: (transfer none): a #GdkDrawingContext context that should be used to
 * draw the contents of the @renderer. This context is owned by GDK.
 *
 * Since: 3.90
 */
870 871 872 873 874 875 876 877 878 879
GdkDrawingContext *
gsk_renderer_begin_draw_frame (GskRenderer          *renderer,
                               const cairo_region_t *region)
{
  GskRendererPrivate *priv = gsk_renderer_get_instance_private (renderer);

  g_return_val_if_fail (GSK_IS_RENDERER (renderer), NULL);
  g_return_val_if_fail (region != NULL, NULL);
  g_return_val_if_fail (priv->drawing_context == NULL, NULL);

880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
  if (GSK_RENDER_MODE_CHECK (FULL_REDRAW))
    {
      cairo_region_t *full_window;
      
      full_window = cairo_region_create_rectangle (&(GdkRectangle) {
                                                       0, 0,
                                                       gdk_window_get_width (priv->window),
                                                       gdk_window_get_height (priv->window)
                                                   });
      
      priv->drawing_context = GSK_RENDERER_GET_CLASS (renderer)->begin_draw_frame (renderer, full_window);

      cairo_region_destroy (full_window);
    }
  else
    {
      priv->drawing_context = GSK_RENDERER_GET_CLASS (renderer)->begin_draw_frame (renderer, region);
    }
898 899 900 901

  return priv->drawing_context;
}

902 903 904 905 906 907 908 909 910 911 912 913 914
/**
 * gsk_renderer_end_draw_frame:
 * @renderer: a #GskRenderer
 * @context: the drawing context returned by the matching call to
 *     gsk_renderer_begin_draw_frame()
 *
 * Release the drawning context returned by gsk_renderer_begin_draw_frame().
 *
 * Calls to gsk_renderer_begin_draw_frame() and gsk_renderer_end_draw_frame()
 * must be paired.
 *
 * Since: 3.90
 */
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
void
gsk_renderer_end_draw_frame (GskRenderer       *renderer,
                             GdkDrawingContext *context)
{
  GskRendererPrivate *priv = gsk_renderer_get_instance_private (renderer);

  g_return_if_fail (GSK_IS_RENDERER (renderer));
  g_return_if_fail (GDK_IS_DRAWING_CONTEXT (context));
  g_return_if_fail (priv->drawing_context == context);

  priv->drawing_context = NULL;

  GSK_RENDERER_GET_CLASS (renderer)->end_draw_frame (renderer, context);
}