summaryrefslogtreecommitdiff
path: root/gsk/gl/gskgltextureatlas.c
blob: fb27bbb7edfb9cebca0b312bf221339d16e3e4c4 (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
295

#include "config.h"
#include "gskgltextureatlasprivate.h"
#include "gskdebugprivate.h"
#include "gdkglcontextprivate.h"
#include <epoxy/gl.h>

#define ATLAS_SIZE (512)
#define MAX_OLD_RATIO 0.5

static void
free_atlas (gpointer v)
{
  GskGLTextureAtlas *atlas = v;

  gsk_gl_texture_atlas_free (atlas);

  g_free (atlas);
}

GskGLTextureAtlases *
gsk_gl_texture_atlases_new (void)
{
  GskGLTextureAtlases *atlases;

  atlases = g_new (GskGLTextureAtlases, 1);
  atlases->atlases = g_ptr_array_new_with_free_func (free_atlas);

  atlases->ref_count = 1;

  return atlases;
}

GskGLTextureAtlases *
gsk_gl_texture_atlases_ref (GskGLTextureAtlases *atlases)
{
  atlases->ref_count++;

  return atlases;
}

void
gsk_gl_texture_atlases_unref (GskGLTextureAtlases *atlases)
{
  g_assert (atlases->ref_count > 0);

  if (atlases->ref_count == 1)
    {
      g_ptr_array_unref (atlases->atlases);
      g_free (atlases);
      return;
    }

  atlases->ref_count--;
}

#if 0
static void
write_atlas_to_png (GskGLTextureAtlas *atlas,
                    const char        *filename)
{
  int stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32, atlas->width);
  guchar *data = g_malloc (atlas->height * stride);
  cairo_surface_t *s;

  glBindTexture (GL_TEXTURE_2D, atlas->texture_id);
  glGetTexImage (GL_TEXTURE_2D, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, data);
  s = cairo_image_surface_create_for_data (data, CAIRO_FORMAT_ARGB32, atlas->width, atlas->height, stride);
  cairo_surface_write_to_png (s, filename);

  cairo_surface_destroy (s);
  g_free (data);
}
#endif

void
gsk_gl_texture_atlases_begin_frame (GskGLTextureAtlases *atlases)
{
  int i;

  for (i = atlases->atlases->len - 1; i >= 0; i--)
    {
      GskGLTextureAtlas *atlas = g_ptr_array_index (atlases->atlases, i);

      if (gsk_gl_texture_atlas_get_unused_ratio (atlas) > MAX_OLD_RATIO)
        {
          GSK_NOTE(GLYPH_CACHE,
                   g_message ("Dropping atlas %d (%g.2%% old)", i,
                              gsk_gl_texture_atlas_get_unused_ratio (atlas)));

          if (atlas->texture_id != 0)
            {
              glDeleteTextures (1, &atlas->texture_id);
              atlas->texture_id = 0;
            }

          g_ptr_array_remove_index (atlases->atlases, i);
       }
    }

#if 0
  {
    static guint timestamp;

    timestamp++;
    if (timestamp % 10 == 0)
      for (i = 0; i < atlases->atlases->len; i++)
        {
          GskGLTextureAtlas *atlas = g_ptr_array_index (atlases->atlases, i);

          if (atlas->texture_id)
            {
              char *filename;

              filename = g_strdup_printf ("textureatlas%d-%u.png", i, timestamp);
              write_atlas_to_png (atlas, filename);
              g_free (filename);
            }
         }
   }
#endif
}

gboolean
gsk_gl_texture_atlases_pack (GskGLTextureAtlases *atlases,
                             int                  width,
                             int                  height,
                             GskGLTextureAtlas  **atlas_out,
                             int                 *out_x,
                             int                 *out_y)
{
  GskGLTextureAtlas *atlas;
  int x, y;
  int i;

  g_assert (width  < ATLAS_SIZE);
  g_assert (height < ATLAS_SIZE);

  for (i = 0; i < atlases->atlases->len; i++)
    {
      atlas = g_ptr_array_index (atlases->atlases, i);

      if (gsk_gl_texture_atlas_pack (atlas, width, height, &x, &y))
        break;

      atlas = NULL;
    }

  if (atlas == NULL)
    {
      /* No atlas has enough space, so create a new one... */
      atlas = g_malloc (sizeof (GskGLTextureAtlas));
      gsk_gl_texture_atlas_init (atlas, ATLAS_SIZE, ATLAS_SIZE);
      gsk_gl_texture_atlas_realize (atlas);
      g_ptr_array_add (atlases->atlases, atlas);

      /* Pack it onto that one, which surely has enough space... */
      gsk_gl_texture_atlas_pack (atlas, width, height, &x, &y);
    }

  *atlas_out = atlas;
  *out_x = x;
  *out_y = y;

  return TRUE;
}

void
gsk_gl_texture_atlas_init (GskGLTextureAtlas *self,
                           int                width,
                           int                height)
{
  memset (self, 0, sizeof (*self));

  self->texture_id = 0;
  self->width = width;
  self->height = height;

  /* TODO: We might want to change the strategy about the amount of
   *       nodes here? stb_rect_pack.h says with is optimal. */
  self->nodes = g_malloc0 (sizeof (struct stbrp_node) * width);
  stbrp_init_target (&self->context,
                     width, height,
                     self->nodes,
                     width);

  gsk_gl_texture_atlas_realize (self);
}

void
gsk_gl_texture_atlas_free (GskGLTextureAtlas *self)
{
  if (self->texture_id != 0)
    {
      glDeleteTextures (1, &self->texture_id);
      self->texture_id = 0;
    }

  g_clear_pointer (&self->nodes, g_free);
}

void
gsk_gl_texture_atlas_mark_unused (GskGLTextureAtlas *self,
                                  int                width,
                                  int                height)
{
  self->unused_pixels += (width * height);
}


void
gsk_gl_texture_atlas_mark_used (GskGLTextureAtlas *self,
                                int                width,
                                int                height)
{
  self->unused_pixels -= (width * height);

  g_assert (self->unused_pixels >= 0);
}

gboolean
gsk_gl_texture_atlas_pack (GskGLTextureAtlas *self,
                           int                width,
                           int                height,
                           int               *out_x,
                           int               *out_y)
{
  stbrp_rect rect;

  g_assert (out_x);
  g_assert (out_y);

  rect.w = width;
  rect.h = height;

  stbrp_pack_rects (&self->context, &rect, 1);

  if (rect.was_packed)
    {
      *out_x = rect.x;
      *out_y = rect.y;
    }

  return rect.was_packed;
}

double
gsk_gl_texture_atlas_get_unused_ratio (const GskGLTextureAtlas *self)
{
  if (self->unused_pixels > 0)
    return (double)(self->unused_pixels) / (double)(self->width * self->height);

  return 0.0;
}

/* Not using gdk_gl_driver_create_texture here, since we want
 * this texture to survive the driver and stay around until
 * the display gets closed.
 */
static guint
create_shared_texture (int width,
                       int height)
{
  guint texture_id;

  glGenTextures (1, &texture_id);
  glBindTexture (GL_TEXTURE_2D, texture_id);

  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

  if (gdk_gl_context_get_use_es (gdk_gl_context_get_current ()))
    glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
  else
    glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);

  glBindTexture (GL_TEXTURE_2D, 0);

  return texture_id;
}

void
gsk_gl_texture_atlas_realize (GskGLTextureAtlas *atlas)
{
  if (atlas->texture_id)
    return;

  atlas->texture_id = create_shared_texture (atlas->width, atlas->height);
  gdk_gl_context_label_object_printf (gdk_gl_context_get_current (),
                                      GL_TEXTURE, atlas->texture_id,
                                      "Glyph atlas %d", atlas->texture_id);
}