summaryrefslogtreecommitdiff
path: root/gsk/ngl/gskngltexturelibrary.c
blob: 2de6e492c59cb75c55e192ee712a71ae58fbbc4d (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
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
/* gskngltexturelibrary.c
 *
 * Copyright 2020 Christian Hergert <chergert@redhat.com>
 *
 * 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.1 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 program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include "config.h"

#include <gdk/gdkglcontextprivate.h>
#include <gsk/gskdebugprivate.h>

#include "gsknglcommandqueueprivate.h"
#include "gskngldriverprivate.h"
#include "gskngltexturelibraryprivate.h"

#define MAX_FRAME_AGE 60

G_DEFINE_ABSTRACT_TYPE (GskNglTextureLibrary, gsk_ngl_texture_library, G_TYPE_OBJECT)

enum {
  PROP_0,
  PROP_DRIVER,
  N_PROPS
};

static GParamSpec *properties [N_PROPS];

static void
gsk_ngl_texture_library_constructed (GObject *object)
{
  G_OBJECT_CLASS (gsk_ngl_texture_library_parent_class)->constructed (object);

  g_assert (GSK_NGL_TEXTURE_LIBRARY (object)->hash_table != NULL);
}

static void
gsk_ngl_texture_library_dispose (GObject *object)
{
  GskNglTextureLibrary *self = (GskNglTextureLibrary *)object;

  g_clear_object (&self->driver);

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

static void
gsk_ngl_texture_library_get_property (GObject    *object,
                                      guint       prop_id,
                                      GValue     *value,
                                      GParamSpec *pspec)
{
  GskNglTextureLibrary *self = GSK_NGL_TEXTURE_LIBRARY (object);

  switch (prop_id)
    {
    case PROP_DRIVER:
      g_value_set_object (value, self->driver);
      break;

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

static void
gsk_ngl_texture_library_set_property (GObject      *object,
                                      guint         prop_id,
                                      const GValue *value,
                                      GParamSpec   *pspec)
{
  GskNglTextureLibrary *self = GSK_NGL_TEXTURE_LIBRARY (object);

  switch (prop_id)
    {
    case PROP_DRIVER:
      self->driver = g_value_dup_object (value);
      break;

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

static void
gsk_ngl_texture_library_class_init (GskNglTextureLibraryClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->constructed = gsk_ngl_texture_library_constructed;
  object_class->dispose = gsk_ngl_texture_library_dispose;
  object_class->get_property = gsk_ngl_texture_library_get_property;
  object_class->set_property = gsk_ngl_texture_library_set_property;

  properties [PROP_DRIVER] =
    g_param_spec_object ("driver",
                         "Driver",
                         "Driver",
                         GSK_TYPE_NGL_DRIVER,
                         (G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));

  g_object_class_install_properties (object_class, N_PROPS, properties);
}

static void
gsk_ngl_texture_library_init (GskNglTextureLibrary *self)
{
}

void
gsk_ngl_texture_library_set_funcs (GskNglTextureLibrary *self,
                                   GHashFunc             hash_func,
                                   GEqualFunc            equal_func,
                                   GDestroyNotify        key_destroy,
                                   GDestroyNotify        value_destroy)
{
  g_return_if_fail (GSK_IS_NGL_TEXTURE_LIBRARY (self));
  g_return_if_fail (self->hash_table == NULL);

  self->hash_table = g_hash_table_new_full (hash_func, equal_func,
                                            key_destroy, value_destroy);
}

void
gsk_ngl_texture_library_begin_frame (GskNglTextureLibrary *self,
                                     gint64                frame_id,
                                     GPtrArray            *removed_atlases)
{
  GHashTableIter iter;

  g_return_if_fail (GSK_IS_NGL_TEXTURE_LIBRARY (self));

  if (GSK_NGL_TEXTURE_LIBRARY_GET_CLASS (self)->begin_frame)
    GSK_NGL_TEXTURE_LIBRARY_GET_CLASS (self)->begin_frame (self, frame_id, removed_atlases);

  if (removed_atlases != NULL)
    {
      GskNglTextureAtlasEntry *entry;
      guint dropped = 0;

      g_hash_table_iter_init (&iter, self->hash_table);
      while (g_hash_table_iter_next (&iter, NULL, (gpointer *)&entry))
        {
          if (entry->is_atlased)
            {
              for (guint i = 0; i < removed_atlases->len; i++)
                {
                  GskNglTextureAtlas *atlas = g_ptr_array_index (removed_atlases, i);

                  if (atlas == entry->atlas)
                    {
                      g_hash_table_iter_remove (&iter);
                      dropped++;
                      break;
                    }
                }
            }
        }

      GSK_NOTE (GLYPH_CACHE,
                if (dropped > 0)
                  g_message ("%s: Dropped %d icons",
                             G_OBJECT_TYPE_NAME (self), dropped));
    }

  if (frame_id % MAX_FRAME_AGE == 0)
    {
      GskNglTextureAtlasEntry *entry;

      g_hash_table_iter_init (&iter, self->hash_table);
      while (g_hash_table_iter_next (&iter, NULL, (gpointer *)&entry))
        {
          gsk_ngl_texture_atlas_entry_mark_unused (entry);
          entry->accessed = FALSE;
        }

      GSK_NOTE (GLYPH_CACHE, g_message ("%s: %d atlas items cached",
                                        G_OBJECT_TYPE_NAME (self),
                                        g_hash_table_size (self->hash_table)));
    }
}

static GskNglTexture *
gsk_ngl_texture_library_pack_one (GskNglTextureLibrary *self,
                                  guint                 width,
                                  guint                 height)
{
  GskNglTexture *texture;

  g_assert (GSK_IS_NGL_TEXTURE_LIBRARY (self));

  if (width > self->driver->command_queue->max_texture_size ||
      height > self->driver->command_queue->max_texture_size)
    {
      g_warning ("Clipping requested texture of size %ux%u to maximum allowable size %u.",
                 width, height, self->driver->command_queue->max_texture_size);
      width = MIN (width, self->driver->command_queue->max_texture_size);
      height = MIN (height, self->driver->command_queue->max_texture_size);
    }

  texture = gsk_ngl_driver_create_texture (self->driver, width, height, GL_LINEAR, GL_LINEAR);
  texture->permanent = TRUE;

  return texture;
}

static inline gboolean
gsk_ngl_texture_atlas_pack (GskNglTextureAtlas *self,
                            int                 width,
                            int                 height,
                            int                *out_x,
                            int                *out_y)
{
  stbrp_rect rect;

  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;
}

static void
gsk_ngl_texture_atlas_initialize (GskNglDriver       *driver,
                                  GskNglTextureAtlas *atlas)
{
  /* Insert a single pixel at 0,0 for use in coloring */

  gboolean packed;
  int x, y;
  guint gl_format;
  guint gl_type;
  guint8 pixel_data[4 * 3 * 3];

  gdk_gl_context_push_debug_group_printf (gdk_gl_context_get_current (),
                                          "Initializing Atlas");

  packed = gsk_ngl_texture_atlas_pack (atlas, 3, 3, &x, &y);
  g_assert (packed);
  g_assert (x == 0 && y == 0);

  memset (pixel_data, 255, sizeof pixel_data);

  if (gdk_gl_context_get_use_es (gdk_gl_context_get_current ()))
    {
      gl_format = GL_RGBA;
      gl_type = GL_UNSIGNED_BYTE;
    }
  else
    {
      gl_format = GL_BGRA;
      gl_type = GL_UNSIGNED_INT_8_8_8_8_REV;
    }

  glBindTexture (GL_TEXTURE_2D, atlas->texture_id);

  glTexSubImage2D (GL_TEXTURE_2D, 0,
                   0, 0,
                   3, 3,
                   gl_format, gl_type,
                   pixel_data);

  gdk_gl_context_pop_debug_group (gdk_gl_context_get_current ());

  driver->command_queue->n_uploads++;
}

static void
gsk_ngl_texture_atlases_pack (GskNglDriver        *driver,
                              int                  width,
                              int                  height,
                              GskNglTextureAtlas **out_atlas,
                              int                 *out_x,
                              int                 *out_y)
{
  GskNglTextureAtlas *atlas = NULL;
  int x, y;

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

      if (gsk_ngl_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 = gsk_ngl_driver_create_atlas (driver);

      gsk_ngl_texture_atlas_initialize (driver, atlas);

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

  *out_atlas = atlas;
  *out_x = x;
  *out_y = y;
}

gpointer
gsk_ngl_texture_library_pack (GskNglTextureLibrary *self,
                              gpointer              key,
                              gsize                 valuelen,
                              guint                 width,
                              guint                 height,
                              int                   padding,
                              guint                *out_packed_x,
                              guint                *out_packed_y)
{
  GskNglTextureAtlasEntry *entry;
  GskNglTextureAtlas *atlas = NULL;

  g_assert (GSK_IS_NGL_TEXTURE_LIBRARY (self));
  g_assert (key != NULL);
  g_assert (valuelen > sizeof (GskNglTextureAtlasEntry));
  g_assert (out_packed_x != NULL);
  g_assert (out_packed_y != NULL);

  entry = g_slice_alloc0 (valuelen);
  entry->n_pixels = width * height;
  entry->accessed = TRUE;
  entry->used = TRUE;

  /* If our size is invisible then we just want an entry in the
   * cache for faster lookups, but do not actually spend any texture
   * allocations on this entry.
   */
  if (width <= 0 && height <= 0)
    {
      entry->is_atlased = FALSE;
      entry->texture = NULL;
      entry->area.x = 0.0f;
      entry->area.y = 0.0f;
      entry->area.x2 = 0.0f;
      entry->area.y2 = 0.0f;

      *out_packed_x = 0;
      *out_packed_y = 0;
    }
  else if (width <= self->max_entry_size && height <= self->max_entry_size)
    {
      int packed_x;
      int packed_y;

      gsk_ngl_texture_atlases_pack (self->driver,
                                    padding + width + padding,
                                    padding + height + padding,
                                    &atlas,
                                    &packed_x,
                                    &packed_y);

      entry->atlas = atlas;
      entry->is_atlased = TRUE;
      entry->area.x = (float)(packed_x + padding) / atlas->width;
      entry->area.y = (float)(packed_y + padding) / atlas->height;
      entry->area.x2 = entry->area.x + (float)width / atlas->width;
      entry->area.y2 = entry->area.y + (float)height / atlas->height;

      *out_packed_x = packed_x;
      *out_packed_y = packed_y;
    }
  else
    {
      GskNglTexture *texture = gsk_ngl_texture_library_pack_one (self,
                                                                 padding + width + padding,
                                                                 padding + height + padding);

      entry->texture = texture;
      entry->is_atlased = FALSE;
      entry->accessed = TRUE;
      entry->area.x = 0.0f;
      entry->area.y = 0.0f;
      entry->area.x2 = 1.0f;
      entry->area.y2 = 1.0f;

      *out_packed_x = padding;
      *out_packed_y = padding;
    }

  g_hash_table_insert (self->hash_table, key, entry);

  return entry;
}