Skip to content

Commit 13560a8

Browse files
authored
Prefer use of gdk_memory_texture_new() (#46)
As the gdk-pixbuf dependency is expected to be dropped in GTK5.
1 parent 49afadf commit 13560a8

File tree

3 files changed

+21
-31
lines changed

3 files changed

+21
-31
lines changed

src/tile.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ tile_dispose(GObject *object)
5050
#endif /*DEBUG*/
5151

5252
VIPS_UNREF(tile->texture);
53-
VIPS_UNREF(tile->pixbuf);
54-
VIPS_FREE(tile->data_copy);
53+
VIPS_FREEF(g_bytes_unref, tile->bytes);
5554
VIPS_UNREF(tile->region);
5655

5756
G_OBJECT_CLASS(tile_parent_class)->dispose(object);
@@ -145,31 +144,32 @@ tile_get_texture(Tile *tile)
145144
* 1. We must make a copy of the pixel data from libvips, to stop
146145
* it being changed under our feet.
147146
*
148-
* 2. Wrap a pixbuf around that copy.
147+
* 2. Wrap a GBytes around that copy.
149148
*
150149
* 3. Tag it as a texture that may need upload to the GPU.
151150
*/
152151
if (!tile->texture) {
153-
VIPS_FREE(tile->data_copy);
154-
tile->data_copy = g_memdup2(
152+
gpointer copy = g_memdup2(
155153
VIPS_REGION_ADDR(tile->region,
156154
tile->region->valid.left,
157155
tile->region->valid.top),
158156
VIPS_REGION_SIZEOF_LINE(tile->region) *
159157
tile->region->valid.height);
160158

161-
VIPS_UNREF(tile->pixbuf);
162-
tile->pixbuf = gdk_pixbuf_new_from_data(
163-
tile->data_copy,
164-
GDK_COLORSPACE_RGB,
165-
tile->region->im->Bands == 4,
166-
8,
159+
VIPS_FREEF(g_bytes_unref, tile->bytes);
160+
tile->bytes = g_bytes_new_take(
161+
copy,
162+
VIPS_REGION_SIZEOF_LINE(tile->region) *
163+
tile->region->valid.height);
164+
165+
tile->texture = gdk_memory_texture_new(
167166
tile->region->valid.width,
168167
tile->region->valid.height,
169-
VIPS_REGION_LSKIP(tile->region),
170-
NULL, NULL);
171-
172-
tile->texture = gdk_texture_new_for_pixbuf(tile->pixbuf);
168+
tile->region->im->Bands == 4
169+
? GDK_MEMORY_R8G8B8A8
170+
: GDK_MEMORY_R8G8B8,
171+
tile->bytes,
172+
VIPS_REGION_LSKIP(tile->region));
173173
}
174174

175175
return tile->texture;
@@ -184,5 +184,5 @@ tile_free_texture(Tile *tile)
184184
g_assert(tile->valid);
185185

186186
VIPS_UNREF(tile->texture);
187-
VIPS_UNREF(tile->pixbuf);
187+
VIPS_FREEF(g_bytes_unref, tile->bytes);
188188
}

src/tile.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,11 @@ typedef struct _Tile {
7070

7171
/* Pixels going out to the scene graph.
7272
*
73-
* pixbuf and texture won't make a copy of the data, so we must make a
73+
* bytes and texture won't make a copy of the data, so we must make a
7474
* copy ourselves, in case a later fetch from the same region produces
7575
* invalid data.
7676
*/
77-
VipsPel *data_copy;
78-
GdkPixbuf *pixbuf;
77+
GBytes *bytes;
7978
GdkTexture *texture;
8079

8180
} Tile;

src/tilecache.c

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,6 @@ tilecache_area_changed(Tilecache *tilecache, VipsRect *dirty, int z)
122122
g_signal_emit(tilecache, tilecache_signals[SIG_AREA_CHANGED], 0, dirty, z);
123123
}
124124

125-
static void
126-
tilecache_checkerboard_destroy_notify(guchar *pixels, gpointer data)
127-
{
128-
g_free(pixels);
129-
}
130-
131125
/* Make a GdkTexture for the checkerboard pattern we use for compositing.
132126
*/
133127
static GdkTexture *
@@ -159,13 +153,10 @@ tilecache_texture(TilecacheBackground background)
159153
data[y * TILE_SIZE * 3 + x * 3 + z] = v;
160154
}
161155

162-
g_autoptr(GdkPixbuf) pixbuf = gdk_pixbuf_new_from_data(data,
163-
GDK_COLORSPACE_RGB,
164-
FALSE, 8,
165-
TILE_SIZE, TILE_SIZE, TILE_SIZE * 3,
166-
tilecache_checkerboard_destroy_notify, NULL);
156+
g_autoptr(GBytes) bytes = g_bytes_new_take(data, TILE_SIZE * TILE_SIZE * 3);
167157

168-
return gdk_texture_new_for_pixbuf(pixbuf);
158+
return gdk_memory_texture_new(
159+
TILE_SIZE, TILE_SIZE, GDK_MEMORY_R8G8B8, bytes, TILE_SIZE * 3);
169160
}
170161

171162
static void

0 commit comments

Comments
 (0)