Skip to content

Commit d2c967a

Browse files
committed
prefer gdk_memory_texture_new()
thanks kleis!
1 parent 49bea4a commit d2c967a

File tree

3 files changed

+31
-46
lines changed

3 files changed

+31
-46
lines changed

src/tile.c

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

5151
VIPS_UNREF(tile->texture);
52-
VIPS_UNREF(tile->pixbuf);
53-
VIPS_FREE(tile->data_copy);
52+
VIPS_FREEF(g_bytes_unref, tile->bytes);
5453
VIPS_UNREF(tile->region);
5554

5655
G_OBJECT_CLASS(tile_parent_class)->dispose(object);
@@ -144,31 +143,28 @@ tile_get_texture(Tile *tile)
144143
* 1. We must make a copy of the pixel data from libvips, to stop
145144
* it being changed under our feet.
146145
*
147-
* 2. Wrap a pixbuf around that copy.
146+
* 2. Wrap a GBytes around that copy.
148147
*
149148
* 3. Tag it as a texture that may need upload to the GPU.
150149
*/
151150
if (!tile->texture) {
152-
VIPS_FREE(tile->data_copy);
153-
tile->data_copy = g_memdup2(
154-
VIPS_REGION_ADDR(tile->region,
155-
tile->region->valid.left,
156-
tile->region->valid.top),
157-
VIPS_REGION_SIZEOF_LINE(tile->region) *
158-
tile->region->valid.height);
159-
160-
VIPS_UNREF(tile->pixbuf);
161-
tile->pixbuf = gdk_pixbuf_new_from_data(
162-
tile->data_copy,
163-
GDK_COLORSPACE_RGB,
164-
tile->region->im->Bands == 4,
165-
8,
166-
tile->region->valid.width,
167-
tile->region->valid.height,
168-
VIPS_REGION_LSKIP(tile->region),
169-
NULL, NULL);
170-
171-
tile->texture = gdk_texture_new_for_pixbuf(tile->pixbuf);
151+
gpointer copy = g_memdup2(
152+
VIPS_REGION_ADDR(tile->region,
153+
tile->region->valid.left,
154+
tile->region->valid.top),
155+
VIPS_REGION_SIZEOF_LINE(tile->region) * tile->region->valid.height);
156+
157+
VIPS_FREEF(g_bytes_unref, tile->bytes);
158+
tile->bytes = g_bytes_new_take(copy,
159+
VIPS_REGION_SIZEOF_LINE(tile->region) * tile->region->valid.height);
160+
161+
tile->texture = gdk_memory_texture_new(
162+
tile->region->valid.width,
163+
tile->region->valid.height,
164+
tile->region->im->Bands == 4 ?
165+
GDK_MEMORY_R8G8B8A8 : GDK_MEMORY_R8G8B8,
166+
tile->bytes,
167+
VIPS_REGION_LSKIP(tile->region));
172168
}
173169

174170
return tile->texture;
@@ -183,5 +179,5 @@ tile_free_texture(Tile *tile)
183179
g_assert(tile->valid);
184180

185181
VIPS_UNREF(tile->texture);
186-
VIPS_UNREF(tile->pixbuf);
182+
VIPS_FREEF(g_bytes_unref, tile->bytes);
187183
}

src/tile.h

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,14 @@ typedef struct _Tile {
6868
*/
6969
gboolean valid;
7070

71-
/* Pixels going out to the scene graph.
72-
*
73-
* pixbuf and texture won't make a copy of the data, so we must make a
74-
* copy ourselves, in case a later fetch from the same region produces
75-
* invalid data.
76-
*/
77-
VipsPel *data_copy;
78-
GdkPixbuf *pixbuf;
79-
GdkTexture *texture;
80-
71+
/* Pixels going out to the scene graph.
72+
*
73+
* bytes and texture won't make a copy of the data, so we must make a
74+
* copy ourselves, in case a later fetch from the same region produces
75+
* invalid data.
76+
*/
77+
GBytes *bytes;
78+
GdkTexture *texture;
8179
} Tile;
8280

8381
typedef struct _TileClass {

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(TILE_SIZE, TILE_SIZE, GDK_MEMORY_R8G8B8,
159+
bytes, TILE_SIZE * 3);
169160
}
170161

171162
static void

0 commit comments

Comments
 (0)