summaryrefslogtreecommitdiff
path: root/gsk/gl/gskglpathcache.c
blob: 1dcb0c33d870c80100b7be2c923f01ee38f3c22f (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
#include "config.h"

#include "gskglpathcacheprivate.h"
#include "gskstrokeprivate.h"

#define MAX_UNUSED_FRAMES (16 * 5)

typedef struct
{
  GskPath *path;
  GskFillRule fill_rule;
  const GskStroke *stroke;
  guint hash;

  GskStroke stroke_copy;
  graphene_rect_t bounds;
  int texture_id;
  int unused_frames;
} CacheItem;

static guint
compute_hash (CacheItem *item)
{
  guint hash;

  hash = GPOINTER_TO_UINT (item->path);
  hash += item->fill_rule;
  if (item->stroke)
    hash += gsk_stroke_hash (item->stroke);

  return hash;
}

static guint
cache_key_hash (gconstpointer v)
{
  const CacheItem *item = v;

  return item->hash;
}

static gboolean
cache_key_equal (gconstpointer v1,
                 gconstpointer v2)
{
  const CacheItem *item1 = v1;
  const CacheItem *item2 = v2;

  return item1->path == item2->path &&
         item1->fill_rule == item2->fill_rule &&
         (item1->stroke == item2->stroke ||
          (item1->stroke && item2->stroke &&
           gsk_stroke_equal (item1->stroke, item2->stroke)));
}

void
gsk_gl_path_cache_init (GskGLPathCache *self)
{
  self->textures = g_hash_table_new_full (cache_key_hash,
                                          cache_key_equal,
                                          NULL,
                                          g_free);
}

void
gsk_gl_path_cache_free (GskGLPathCache *self,
                        GskGLDriver    *gl_driver)
{
  GHashTableIter iter;
  CacheItem *item;

  g_hash_table_iter_init (&iter, self->textures);
  while (g_hash_table_iter_next (&iter, NULL, (gpointer *)&item))
    {
      gsk_gl_driver_destroy_texture (gl_driver, item->texture_id);
    }

  g_hash_table_unref (self->textures);
  self->textures = NULL;
}

void
gsk_gl_path_cache_begin_frame (GskGLPathCache *self,
                               GskGLDriver    *gl_driver)
{
  GHashTableIter iter;
  CacheItem *item;

  g_hash_table_iter_init (&iter, self->textures);
  while (g_hash_table_iter_next (&iter, NULL, (gpointer *)&item))
    {
      if (item->unused_frames > MAX_UNUSED_FRAMES)
        {
          gsk_gl_driver_destroy_texture (gl_driver, item->texture_id);
          g_hash_table_iter_remove (&iter);
        }
    }
}

int
gsk_gl_path_cache_get_texture_id (GskGLPathCache  *self,
                                  GskPath         *path,
                                  GskFillRule      fill_rule,
                                  const GskStroke *stroke,
                                  graphene_rect_t *out_bounds)
{
  CacheItem key;
  CacheItem *item;

  key.path = path;
  key.fill_rule = fill_rule;
  key.stroke = stroke;
  key.hash = compute_hash (&key);

  item = g_hash_table_lookup (self->textures, &key);
  if (item == NULL)
    return 0;

  item->unused_frames = 0;

  g_assert (item->texture_id != 0);

  *out_bounds = item->bounds;

  return item->texture_id;
}

void
gsk_gl_path_cache_commit (GskGLPathCache        *self,
                          GskPath               *path,
                          GskFillRule            fill_rule,
                          const GskStroke       *stroke,
                          int                    texture_id,
                          const graphene_rect_t *bounds)
{
  CacheItem *item;

  g_assert (self != NULL);
  g_assert (texture_id > 0);

  item = g_new0 (CacheItem, 1);

  item->path = path;
  item->fill_rule = fill_rule;
  if (stroke)
    {
      gsk_stroke_init_copy (&item->stroke_copy, stroke);
      item->stroke = &item->stroke_copy;
    }
  item->hash = compute_hash (item);

  item->unused_frames = 0;
  item->texture_id = texture_id;
  item->bounds = *bounds;

  g_hash_table_add (self->textures, item);
}