File: sinkmemory.c

package info (click to toggle)
vips 8.4.5-1%2Bdeb9u1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 25,724 kB
  • sloc: ansic: 102,605; cpp: 57,527; sh: 4,957; xml: 3,317; python: 3,105; makefile: 1,089; perl: 40
file content (351 lines) | stat: -rw-r--r-- 8,684 bytes parent folder | download | duplicates (2)
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
/* SinkMemory an image to a memory buffer, keeping top-to-bottom ordering.
 *
 * For sequential operations we need to keep requests reasonably ordered: we
 * can't let some tiles get very delayed. So we need to stall starting new
 * threads if the last thread gets too far behind.
 * 
 * 17/2/12
 * 	- from sinkdisc.c
 * 23/2/12
 * 	- we could deadlock if generate failed
 */

/*

    This file is part of VIPS.
    
    VIPS 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 of the License, or
    (at your option) any later version.

    This program 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, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301  USA

 */

/*

    These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk

 */

/*
#define VIPS_DEBUG
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <vips/intl.h>

#include <stdio.h>
#include <stdlib.h>

#include <vips/vips.h>
#include <vips/internal.h>
#include <vips/thread.h>
#include <vips/threadpool.h>
#include <vips/debug.h>

#include "sink.h"

/* A part of the image we are writing. 
 */
typedef struct _SinkMemoryArea {
	struct _SinkMemory *memory;

	VipsRect rect;		/* Part of image this area covers */
        VipsSemaphore nwrite; 	/* Number of threads writing to this area */
} SinkMemoryArea;

/* Per-call state.
 */
typedef struct _SinkMemory {
	SinkBase sink_base;

	/* We are current writing tiles to area, we'll delay starting a new
	 * area if old_area (the previous position) hasn't completed. 
	 */
	SinkMemoryArea *area;
	SinkMemoryArea *old_area;

	/* A region covering the whole of the output image ... we write to
	 * this from many workers with vips_region_prepare_to().
	 */
	VipsRegion *region;
} SinkMemory;

/* Our per-thread state ... we need to also track the area that pos is
 * supposed to write to.
 */
typedef struct _SinkMemoryThreadState {
	VipsThreadState parent_object;

        SinkMemoryArea *area;
} SinkMemoryThreadState;

typedef struct _SinkMemoryThreadStateClass {
	VipsThreadStateClass parent_class;

} SinkMemoryThreadStateClass;

G_DEFINE_TYPE( SinkMemoryThreadState, 
	sink_memory_thread_state, VIPS_TYPE_THREAD_STATE );

static void
sink_memory_thread_state_class_init( SinkMemoryThreadStateClass *class )
{
	VipsObjectClass *object_class = VIPS_OBJECT_CLASS( class );

	object_class->nickname = "sinkmemorythreadstate";
	object_class->description = _( "per-thread state for sinkmemory" );
}

static void
sink_memory_thread_state_init( SinkMemoryThreadState *state )
{
}

static VipsThreadState *
sink_memory_thread_state_new( VipsImage *image, void *a )
{
	return( VIPS_THREAD_STATE( vips_object_new( 
		sink_memory_thread_state_get_type(), 
		vips_thread_state_set, image, a ) ) );
}

static void
sink_memory_area_free( SinkMemoryArea *area )
{
	vips_semaphore_destroy( &area->nwrite );
	vips_free( area );
}

static SinkMemoryArea *
sink_memory_area_new( SinkMemory *memory )
{
	SinkMemoryArea *area;

	if( !(area = VIPS_NEW( NULL, SinkMemoryArea )) )
		return( NULL );
	area->memory = memory;
	vips_semaphore_init( &area->nwrite, 0, "nwrite" );

	return( area );
}

/* Move an area to a position.
 */
static void 
sink_memory_area_position( SinkMemoryArea *area, int top, int height )
{
	SinkMemory *memory = area->memory;

	VipsRect all, rect;

	all.left = 0;
	all.top = 0;
	all.width = memory->sink_base.im->Xsize;
	all.height = memory->sink_base.im->Ysize;

	rect.left = 0;
	rect.top = top;
	rect.width = memory->sink_base.im->Xsize;
	rect.height = height;

	vips_rect_intersectrect( &all, &rect, &area->rect );
}

/* Our VipsThreadpoolAllocate function ... move the thread to the next tile
 * that needs doing. If we fill the current area, we block until the previous
 * area is finished, then swap areas. 
 * If all tiles are done, we return FALSE to end
 * iteration.
 */
static gboolean
sink_memory_area_allocate_fn( VipsThreadState *state, void *a, gboolean *stop )
{
	SinkMemoryThreadState *wstate = (SinkMemoryThreadState *) state;
	SinkMemory *memory = (SinkMemory *) a;
	SinkBase *sink_base = (SinkBase *) memory;

	VipsRect image;
	VipsRect tile;

	VIPS_DEBUG_MSG( "sink_memory_area_allocate_fn: %p\n", g_thread_self() );

	/* Is the state x/y OK? New line or maybe new buffer or maybe even 
	 * all done.
	 */
	if( sink_base->x >= memory->area->rect.width ) {
		sink_base->x = 0;
		sink_base->y += sink_base->tile_height;

		if( sink_base->y >= VIPS_RECT_BOTTOM( &memory->area->rect ) ) {
			/* Block until the previous area is done.
			 */
			if( memory->area->rect.top > 0 ) 
				vips_semaphore_downn( 
					&memory->old_area->nwrite, 0 );

			/* End of image?
			 */
			if( sink_base->y >= sink_base->im->Ysize ) {
				*stop = TRUE;
				return( 0 );
			}

			/* Swap buffers.
			 */
			VIPS_SWAP( SinkMemoryArea *, 
				memory->area, memory->old_area );

			/* Position buf at the new y.
			 */
			sink_memory_area_position( memory->area, 
				sink_base->y, sink_base->nlines );
		}
	}

	/* x, y and buf are good: save params for thread.
	 */
	image.left = 0;
	image.top = 0;
	image.width = sink_base->im->Xsize;
	image.height = sink_base->im->Ysize;
	tile.left = sink_base->x;
	tile.top = sink_base->y;
	tile.width = sink_base->tile_width;
	tile.height = sink_base->tile_height;
	vips_rect_intersectrect( &image, &tile, &state->pos );

	/* The thread needs to know which area it's writing to.
	 */
	wstate->area = memory->area;

	VIPS_DEBUG_MSG( "  %p allocated %d x %d:\n", 
		g_thread_self(), state->pos.left, state->pos.top );

	/* Add to the number of writers on the area.
	 */
	vips_semaphore_upn( &memory->area->nwrite, -1 );

	/* Move state on.
	 */
	sink_base->x += sink_base->tile_width;

	/* Add the number of pixels we've just allocated to progress.
	 */
	sink_base->processed += state->pos.width * state->pos.height;

	return( 0 );
}

/* Our VipsThreadpoolWork function ... generate a tile!
 */
static int
sink_memory_area_work_fn( VipsThreadState *state, void *a )
{
	SinkMemory *memory = (SinkMemory *) a;
	SinkMemoryThreadState *wstate = (SinkMemoryThreadState *) state;
	SinkMemoryArea *area = wstate->area;

	int result;

	VIPS_DEBUG_MSG( "sink_memory_area_work_fn: %p %d x %d\n", 
		g_thread_self(), state->pos.left, state->pos.top );

	result = vips_region_prepare_to( state->reg, memory->region, 
		&state->pos, state->pos.left, state->pos.top );

	VIPS_DEBUG_MSG( "sink_memory_area_work_fn: %p result = %d\n", 
		g_thread_self(), result );

	/* Tell the allocator we're done.
	 */
	vips_semaphore_upn( &area->nwrite, 1 );

	return( result );
}

static void
sink_memory_free( SinkMemory *memory )
{
	VIPS_FREEF( sink_memory_area_free, memory->area );
	VIPS_FREEF( sink_memory_area_free, memory->old_area );
	VIPS_UNREF( memory->region );
}

static int
sink_memory_init( SinkMemory *memory, VipsImage *image )
{
	VipsRect all;

	vips_sink_base_init( &memory->sink_base, image );
	memory->area = NULL;
	memory->old_area = NULL;

	all.left = 0;
	all.top = 0;
	all.width = image->Xsize;
	all.height = image->Ysize;

	if( !(memory->region = vips_region_new( image )) ||
		vips_region_image( memory->region, &all ) ||
		!(memory->area = sink_memory_area_new( memory )) ||
		!(memory->old_area = sink_memory_area_new( memory )) ) {
		sink_memory_free( memory );
		return( -1 );
	}

	return( 0 );
}

/**
 * vips_sink_memory:
 * @im: generate this image to memory
 *
 * Loops over @im, generating it to a memory buffer attached to @im. It is
 * used by vips to implement writing to a memory buffer.
 *
 * See also: vips_sink(), vips_get_tile_size(), vips_image_new_memory().
 *
 * Returns: 0 on success, or -1 on error.
 */
int
vips_sink_memory( VipsImage *image )
{
	SinkMemory memory;
	int result;

	if( sink_memory_init( &memory, image ) )
		return( -1 );

	vips_image_preeval( image );

	result = 0;
	sink_memory_area_position( memory.area, 0, memory.sink_base.nlines );
	if( vips_threadpool_run( image, 
		sink_memory_thread_state_new, 
		sink_memory_area_allocate_fn, 
		sink_memory_area_work_fn, 
		vips_sink_base_progress, 
		&memory ) )  
		result = -1;

	vips_image_posteval( image );

	sink_memory_free( &memory );

	VIPS_DEBUG_MSG( "vips_sink_memory: done\n" );

	return( result );
}