gskvulkanrenderer.c 11.2 KB
Newer Older
1 2 3 4 5
#include "config.h"

#include "gskvulkanrendererprivate.h"

#include "gskdebugprivate.h"
6
#include "gskprivate.h"
7 8
#include "gskrendererprivate.h"
#include "gskrendernodeprivate.h"
9
#include "gskvulkanbufferprivate.h"
10
#include "gskvulkanimageprivate.h"
11
#include "gskvulkanpipelineprivate.h"
12
#include "gskvulkanrenderprivate.h"
13
#include "gskvulkanglyphcacheprivate.h"
14

Benjamin Otte's avatar
Benjamin Otte committed
15 16
#include "gdk/gdktextureprivate.h"

17 18
#include <graphene.h>

19 20 21
typedef struct _GskVulkanTextureData GskVulkanTextureData;

struct _GskVulkanTextureData {
Benjamin Otte's avatar
Benjamin Otte committed
22
  GdkTexture *texture;
23 24 25 26
  GskVulkanImage *image;
  GskVulkanRenderer *renderer;
};

27
#ifdef G_ENABLE_DEBUG
28 29
typedef struct {
  GQuark frames;
30
  GQuark render_passes;
31
  GQuark fallback_pixels;
32
  GQuark texture_pixels;
33 34
} ProfileCounters;

35 36 37 38 39 40 41 42 43 44 45
typedef struct {
  GQuark cpu_time;
  GQuark gpu_time;
} ProfileTimers;
#endif

struct _GskVulkanRenderer
{
  GskRenderer parent_instance;

  GdkVulkanContext *vulkan;
46 47

  guint n_targets;
48
  GskVulkanImage **targets;
49

50
  GskVulkanRender *render;
51

52 53
  GSList *textures;

54
  GskVulkanGlyphCache *glyph_cache;
55

56
#ifdef G_ENABLE_DEBUG
57
  ProfileCounters profile_counters;
58 59 60 61 62 63 64 65 66 67 68
  ProfileTimers profile_timers;
#endif
};

struct _GskVulkanRendererClass
{
  GskRendererClass parent_class;
};

G_DEFINE_TYPE (GskVulkanRenderer, gsk_vulkan_renderer, GSK_TYPE_RENDERER)

69 70 71 72 73 74 75
static void
gsk_vulkan_renderer_free_targets (GskVulkanRenderer *self)
{
  guint i;

  for (i = 0; i < self->n_targets; i++)
    {
76
      g_object_unref (self->targets[i]);
77 78 79 80 81 82 83 84 85 86
    }

  g_clear_pointer (&self->targets, g_free);
  self->n_targets = 0;
}

static void
gsk_vulkan_renderer_update_images_cb (GdkVulkanContext  *context,
                                      GskVulkanRenderer *self)
{
87
  GdkSurface *window;
88 89
  gint scale_factor;
  gsize width, height;
90 91 92 93 94
  guint i;

  gsk_vulkan_renderer_free_targets (self);

  self->n_targets = gdk_vulkan_context_get_n_images (context);
95
  self->targets = g_new (GskVulkanImage *, self->n_targets);
96

97
  window = gsk_renderer_get_surface (GSK_RENDERER (self));
98 99 100
  scale_factor = gdk_surface_get_scale_factor (window);
  width = gdk_surface_get_width (window) * scale_factor;
  height = gdk_surface_get_height (window) * scale_factor;
101

102 103
  for (i = 0; i < self->n_targets; i++)
    {
104 105 106 107
      self->targets[i] = gsk_vulkan_image_new_for_swapchain (self->vulkan,
                                                             gdk_vulkan_context_get_image (context, i),
                                                             gdk_vulkan_context_get_image_format (self->vulkan),
                                                             width, height);
108 109 110
    }
}

111 112
static gboolean
gsk_vulkan_renderer_realize (GskRenderer  *renderer,
113
                             GdkSurface    *window,
114
                             GError      **error)
115 116 117
{
  GskVulkanRenderer *self = GSK_VULKAN_RENDERER (renderer);

118
  self->vulkan = gdk_surface_create_vulkan_context (window, error);
119 120 121
  if (self->vulkan == NULL)
    return FALSE;

122 123 124 125 126 127
  g_signal_connect (self->vulkan,
                    "images-updated",
                    G_CALLBACK (gsk_vulkan_renderer_update_images_cb),
                    self);
  gsk_vulkan_renderer_update_images_cb (self->vulkan, self);

128
  self->render = gsk_vulkan_render_new (renderer, self->vulkan);
129

130
  self->glyph_cache = gsk_vulkan_glyph_cache_new (renderer, self->vulkan);
131

132 133 134 135 136 137 138
  return TRUE;
}

static void
gsk_vulkan_renderer_unrealize (GskRenderer *renderer)
{
  GskVulkanRenderer *self = GSK_VULKAN_RENDERER (renderer);
139 140
  GSList *l;

141
  g_clear_object (&self->glyph_cache);
142

143 144 145 146 147
  for (l = self->textures; l; l = l->next)
    {
      GskVulkanTextureData *data = l->data;

      data->renderer = NULL;
Benjamin Otte's avatar
Benjamin Otte committed
148
      gdk_texture_clear_render_data (data->texture);
149 150
    }
  g_clear_pointer (&self->textures, (GDestroyNotify) g_slist_free);
151

152
  g_clear_pointer (&self->render, gsk_vulkan_render_free);
153

154 155 156 157
  gsk_vulkan_renderer_free_targets (self);
  g_signal_handlers_disconnect_by_func(self->vulkan,
                                       gsk_vulkan_renderer_update_images_cb,
                                       self);
158

159 160 161
  g_clear_object (&self->vulkan);
}

Benjamin Otte's avatar
Benjamin Otte committed
162
static GdkTexture *
163 164 165 166 167 168 169
gsk_vulkan_renderer_render_texture (GskRenderer           *renderer,
                                    GskRenderNode         *root,
                                    const graphene_rect_t *viewport)
{
  GskVulkanRenderer *self = GSK_VULKAN_RENDERER (renderer);
  GskVulkanRender *render;
  GskVulkanImage *image;
Benjamin Otte's avatar
Benjamin Otte committed
170
  GdkTexture *texture;
171 172 173 174 175 176 177
#ifdef G_ENABLE_DEBUG
  GskProfiler *profiler;
  gint64 cpu_time;
#endif

#ifdef G_ENABLE_DEBUG
  profiler = gsk_renderer_get_profiler (renderer);
178
  gsk_profiler_counter_set (profiler, self->profile_counters.fallback_pixels, 0);
179
  gsk_profiler_counter_set (profiler, self->profile_counters.texture_pixels, 0);
180
  gsk_profiler_counter_set (profiler, self->profile_counters.render_passes, 0);
181 182 183 184 185 186 187 188 189
  gsk_profiler_timer_begin (profiler, self->profile_timers.cpu_time);
#endif

  render = gsk_vulkan_render_new (renderer, self->vulkan);

  image = gsk_vulkan_image_new_for_framebuffer (self->vulkan,
                                                ceil (viewport->size.width),
                                                ceil (viewport->size.height));

190
  gsk_vulkan_render_reset (render, image, viewport, NULL);
191 192 193 194 195

  gsk_vulkan_render_add_node (render, root);

  gsk_vulkan_render_upload (render);

196
  gsk_vulkan_render_draw (render);
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212

  texture = gsk_vulkan_render_download_target (render);

  g_object_unref (image);
  gsk_vulkan_render_free (render);

#ifdef G_ENABLE_DEBUG
  cpu_time = gsk_profiler_timer_end (profiler, self->profile_timers.cpu_time);
  gsk_profiler_timer_set (profiler, self->profile_timers.cpu_time, cpu_time);

  gsk_profiler_push_samples (profiler);
#endif

  return texture;
}

213
static void
214 215 216
gsk_vulkan_renderer_render (GskRenderer          *renderer,
                            GskRenderNode        *root,
                            const cairo_region_t *region)
217 218
{
  GskVulkanRenderer *self = GSK_VULKAN_RENDERER (renderer);
219
  GskVulkanRender *render;
220 221
  GdkDrawingContext *context;
  GdkSurface *surface;
222
  const cairo_region_t *clip;
223 224 225 226 227
#ifdef G_ENABLE_DEBUG
  GskProfiler *profiler;
  gint64 cpu_time;
#endif

228 229
  surface = gsk_renderer_get_surface (renderer);

230 231
#ifdef G_ENABLE_DEBUG
  profiler = gsk_renderer_get_profiler (renderer);
232
  gsk_profiler_counter_set (profiler, self->profile_counters.fallback_pixels, 0);
233
  gsk_profiler_counter_set (profiler, self->profile_counters.texture_pixels, 0);
234
  gsk_profiler_counter_set (profiler, self->profile_counters.render_passes, 0);
235 236 237
  gsk_profiler_timer_begin (profiler, self->profile_timers.cpu_time);
#endif

238 239 240
  context = gdk_surface_begin_draw_frame (surface,
                                          GDK_DRAW_CONTEXT (self->vulkan),
                                          region);
241
  render = self->render;
242

243
  clip = gdk_draw_context_get_frame_region (GDK_DRAW_CONTEXT (self->vulkan));
244
  gsk_vulkan_render_reset (render, self->targets[gdk_vulkan_context_get_draw_index (self->vulkan)], NULL, clip);
245

246
  gsk_vulkan_render_add_node (render, root);
247

248
  gsk_vulkan_render_upload (render);
249

250
  gsk_vulkan_render_draw (render);
251

252
#ifdef G_ENABLE_DEBUG
253 254
  gsk_profiler_counter_inc (profiler, self->profile_counters.frames);

255 256 257 258 259
  cpu_time = gsk_profiler_timer_end (profiler, self->profile_timers.cpu_time);
  gsk_profiler_timer_set (profiler, self->profile_timers.cpu_time, cpu_time);

  gsk_profiler_push_samples (profiler);
#endif
260

261
  gdk_surface_end_draw_frame (surface, context);
262 263
}

264 265 266 267 268 269 270 271
static void
gsk_vulkan_renderer_class_init (GskVulkanRendererClass *klass)
{
  GskRendererClass *renderer_class = GSK_RENDERER_CLASS (klass);

  renderer_class->realize = gsk_vulkan_renderer_realize;
  renderer_class->unrealize = gsk_vulkan_renderer_unrealize;
  renderer_class->render = gsk_vulkan_renderer_render;
272
  renderer_class->render_texture = gsk_vulkan_renderer_render_texture;
273 274 275 276 277 278 279
}

static void
gsk_vulkan_renderer_init (GskVulkanRenderer *self)
{
#ifdef G_ENABLE_DEBUG
  GskProfiler *profiler = gsk_renderer_get_profiler (GSK_RENDERER (self));
280 281 282
#endif

  gsk_ensure_resources ();
283

284
#ifdef G_ENABLE_DEBUG
285
  self->profile_counters.frames = gsk_profiler_add_counter (profiler, "frames", "Frames", FALSE);
286
  self->profile_counters.render_passes = gsk_profiler_add_counter (profiler, "render-passes", "Render passes", FALSE);
287
  self->profile_counters.fallback_pixels = gsk_profiler_add_counter (profiler, "fallback-pixels", "Fallback pixels", TRUE);
288
  self->profile_counters.texture_pixels = gsk_profiler_add_counter (profiler, "texture-pixels", "Texture pixels", TRUE);
289

290
  self->profile_timers.cpu_time = gsk_profiler_add_timer (profiler, "cpu-time", "CPU time", FALSE, TRUE);
291
  if (GSK_RENDERER_DEBUG_CHECK (GSK_RENDERER (self), SYNC))
292
    self->profile_timers.gpu_time = gsk_profiler_add_timer (profiler, "gpu-time", "GPU time", FALSE, TRUE);
293 294
#endif
}
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309

static void
gsk_vulkan_renderer_clear_texture (gpointer p)
{
  GskVulkanTextureData *data = p;

  if (data->renderer != NULL)
    data->renderer->textures = g_slist_remove (data->renderer->textures, data);

  g_object_unref (data->image);

  g_slice_free (GskVulkanTextureData, data);
}

GskVulkanImage *
310
gsk_vulkan_renderer_ref_texture_image (GskVulkanRenderer *self,
Benjamin Otte's avatar
Benjamin Otte committed
311
                                       GdkTexture        *texture,
312
                                       GskVulkanUploader *uploader)
313 314 315 316 317
{
  GskVulkanTextureData *data;
  cairo_surface_t *surface;
  GskVulkanImage *image;

Benjamin Otte's avatar
Benjamin Otte committed
318
  data = gdk_texture_get_render_data (texture, self);
319 320 321
  if (data)
    return g_object_ref (data->image);

Benjamin Otte's avatar
Benjamin Otte committed
322
  surface = gdk_texture_download_surface (texture);
323
  image = gsk_vulkan_image_new_from_data (uploader,
324 325 326 327 328 329 330 331 332 333 334
                                          cairo_image_surface_get_data (surface),
                                          cairo_image_surface_get_width (surface),
                                          cairo_image_surface_get_height (surface),
                                          cairo_image_surface_get_stride (surface));
  cairo_surface_destroy (surface);

  data = g_slice_new0 (GskVulkanTextureData);
  data->image = image;
  data->texture = texture;
  data->renderer = self;

Benjamin Otte's avatar
Benjamin Otte committed
335
  if (gdk_texture_set_render_data (texture, self, data, gsk_vulkan_renderer_clear_texture))
336 337 338 339 340 341 342 343 344 345 346
    {
      g_object_ref (data->image);
      self->textures = g_slist_prepend (self->textures, data);
    }
  else
    {
      g_slice_free (GskVulkanTextureData, data);
    }

  return image;
}
347

348 349 350
guint
gsk_vulkan_renderer_cache_glyph (GskVulkanRenderer *self,
                                 PangoFont         *font,
351 352
                                 PangoGlyph         glyph,
                                 float              scale)
353
{
354
  return gsk_vulkan_glyph_cache_lookup (self->glyph_cache, TRUE, font, glyph, scale)->texture_index;
355
}
356

357 358 359 360
GskVulkanImage *
gsk_vulkan_renderer_ref_glyph_image (GskVulkanRenderer  *self,
                                     GskVulkanUploader  *uploader,
                                     guint               index)
361
{
362
  return g_object_ref (gsk_vulkan_glyph_cache_get_glyph_image (self->glyph_cache, uploader, index));
363
}
364

365 366 367
GskVulkanCachedGlyph *
gsk_vulkan_renderer_get_cached_glyph (GskVulkanRenderer *self,
                                      PangoFont         *font,
368 369
                                      PangoGlyph         glyph,
                                      float              scale)
370
{
371
  return gsk_vulkan_glyph_cache_lookup (self->glyph_cache, FALSE, font, glyph, scale);
372
}