~ubuntu-branches/ubuntu/jaunty/gimp/jaunty-security

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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * pixel_processor.c: Copyright (C) 1999 Jay Cox <jaycox@gimp.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "config.h"

#ifdef ENABLE_MP
#include <string.h>
#endif

#include <glib-object.h>

#include "base-types.h"

#include "config/gimpbaseconfig.h"

#include "pixel-processor.h"
#include "pixel-region.h"

#include "tile.h"


#define TILES_PER_THREAD  8
#define PROGRESS_TIMEOUT  64


static GThreadPool *pool       = NULL;
static GMutex      *pool_mutex = NULL;
static GCond       *pool_cond  = NULL;


typedef void  (* p1_func) (gpointer      data,
                           PixelRegion  *region1);
typedef void  (* p2_func) (gpointer      data,
                           PixelRegion  *region1,
                           PixelRegion  *region2);
typedef void  (* p3_func) (gpointer      data,
                           PixelRegion  *region1,
                           PixelRegion  *region2,
                           PixelRegion  *region3);
typedef void  (* p4_func) (gpointer      data,
                           PixelRegion  *region1,
                           PixelRegion  *region2,
                           PixelRegion  *region3,
                           PixelRegion  *region4);


typedef struct _PixelProcessor PixelProcessor;

struct _PixelProcessor
{
  PixelProcessorFunc   func;
  gpointer             data;

#ifdef ENABLE_MP
  GMutex              *mutex;
  gint                 threads;
  gboolean             first;
#endif

  PixelRegionIterator *PRI;
  gint                 num_regions;
  PixelRegion         *regions[4];

  gulong               progress;
};


#ifdef ENABLE_MP
static void
do_parallel_regions (PixelProcessor *processor)
{
  PixelRegion tr[4];
  gint        i;

  g_mutex_lock (processor->mutex);

  /*  the first thread getting here must not call pixel_regions_process()  */
  if (!processor->first && processor->PRI)
    processor->PRI = pixel_regions_process (processor->PRI);
  else
    processor->first = FALSE;

  while (processor->PRI)
    {
      guint pixels = (processor->PRI->portion_width *
                      processor->PRI->portion_height);

      for (i = 0; i < processor->num_regions; i++)
        if (processor->regions[i])
          {
            memcpy (&tr[i], processor->regions[i], sizeof (PixelRegion));
            if (tr[i].tiles)
              tile_lock (tr[i].curtile);
          }

      g_mutex_unlock (processor->mutex);

      switch (processor->num_regions)
        {
        case 1:
          ((p1_func) processor->func) (processor->data,
                                       processor->regions[0] ? &tr[0] : NULL);
          break;

        case 2:
          ((p2_func) processor->func) (processor->data,
                                       processor->regions[0] ? &tr[0] : NULL,
                                       processor->regions[1] ? &tr[1] : NULL);
          break;

        case 3:
          ((p3_func) processor->func) (processor->data,
                                       processor->regions[0] ? &tr[0] : NULL,
                                       processor->regions[1] ? &tr[1] : NULL,
                                       processor->regions[2] ? &tr[2] : NULL);
          break;

        case 4:
          ((p4_func) processor->func) (processor->data,
                                       processor->regions[0] ? &tr[0] : NULL,
                                       processor->regions[1] ? &tr[1] : NULL,
                                       processor->regions[2] ? &tr[2] : NULL,
                                       processor->regions[3] ? &tr[3] : NULL);
          break;

        default:
          g_warning ("do_parallel_regions: Bad number of regions %d\n",
                     processor->num_regions);
          break;
        }

      g_mutex_lock (processor->mutex);

      for (i = 0; i < processor->num_regions; i++)
        if (processor->regions[i])
          {
            if (tr[i].tiles)
              tile_release (tr[i].curtile, tr[i].dirty);
          }

      processor->progress += pixels;

      if (processor->PRI)
        processor->PRI = pixel_regions_process (processor->PRI);
    }

  processor->threads--;

  if (processor->threads == 0)
    {
      g_mutex_unlock (processor->mutex);

      g_mutex_lock (pool_mutex);
      g_cond_signal  (pool_cond);
      g_mutex_unlock (pool_mutex);
    }
  else
    {
      g_mutex_unlock (processor->mutex);
    }
}
#endif

/*  do_parallel_regions_single is just like do_parallel_regions
 *   except that all the mutex and tile locks have been removed
 *
 * If we are processing with only a single thread we don't need to do
 * the mutex locks etc. and aditional tile locks even if we were
 * configured --with-mp
 */

static gpointer
do_parallel_regions_single (PixelProcessor             *processor,
                            PixelProcessorProgressFunc  progress_func,
                            gpointer                    progress_data,
                            gulong                      total)
{
  GTimeVal  last_time;

  if (progress_func)
    g_get_current_time (&last_time);

  do
    {
      switch (processor->num_regions)
        {
        case 1:
          ((p1_func) processor->func) (processor->data,
                                       processor->regions[0]);
          break;

        case 2:
          ((p2_func) processor->func) (processor->data,
                                       processor->regions[0],
                                       processor->regions[1]);
          break;

        case 3:
          ((p3_func) processor->func) (processor->data,
                                       processor->regions[0],
                                       processor->regions[1],
                                       processor->regions[2]);
          break;

        case 4:
          ((p4_func) processor->func) (processor->data,
                                       processor->regions[0],
                                       processor->regions[1],
                                       processor->regions[2],
                                       processor->regions[3]);
          break;

        default:
          g_warning ("do_parallel_regions_single: Bad number of regions %d\n",
                     processor->num_regions);
          break;
        }

      if (progress_func)
        {
          GTimeVal  now;

          processor->progress += (processor->PRI->portion_width *
                                  processor->PRI->portion_height);

          g_get_current_time (&now);

          if (((now.tv_sec - last_time.tv_sec) * 1024 +
               (now.tv_usec - last_time.tv_usec) / 1024) > PROGRESS_TIMEOUT)
            {
              progress_func (progress_data,
                             (gdouble) processor->progress / (gdouble) total);

              last_time = now;
            }
        }
    }
  while (processor->PRI &&
         (processor->PRI = pixel_regions_process (processor->PRI)));

  return NULL;
}

static void
pixel_regions_do_parallel (PixelProcessor             *processor,
                           PixelProcessorProgressFunc  progress_func,
                           gpointer                    progress_data)
{
  gulong pixels = (processor->PRI->region_width *
                   processor->PRI->region_height);
  gulong tiles  = pixels / (TILE_WIDTH * TILE_HEIGHT);

#ifdef ENABLE_MP
  if (pool && tiles > TILES_PER_THREAD)
    {
      GError *error = NULL;
      gint    tasks = MIN (tiles / TILES_PER_THREAD,
                           g_thread_pool_get_max_threads (pool));

      /*
       * g_printerr ("pushing %d tasks into the thread pool (for %lu tiles)\n",
       *             tasks, tiles);
       */

      processor->first   = TRUE;
      processor->threads = tasks;
      processor->mutex   = g_mutex_new();

      g_mutex_lock (pool_mutex);

      while (tasks--)
        {
          g_thread_pool_push (pool, processor, &error);

          if (G_UNLIKELY (error))
            {
              g_warning ("thread creation failed: %s", error->message);
              g_clear_error (&error);
              processor->threads--;
            }
        }

      if (progress_func)
        {
          while (processor->threads != 0)
            {
              GTimeVal timeout;
              gulong   progress;

              g_get_current_time (&timeout);
              g_time_val_add (&timeout, PROGRESS_TIMEOUT * 1024);

              g_cond_timed_wait (pool_cond, pool_mutex, &timeout);

              g_mutex_lock (processor->mutex);
              progress = processor->progress;
              g_mutex_unlock (processor->mutex);

              progress_func (progress_data,
                             (gdouble) progress / (gdouble) pixels);
            }
        }
      else
        {
          while (processor->threads != 0)
            g_cond_wait (pool_cond, pool_mutex);
        }

      g_mutex_unlock (pool_mutex);

      g_mutex_free (processor->mutex);
    }
  else
#endif
    {
      do_parallel_regions_single (processor,
                                  progress_func, progress_data, pixels);
    }

  if (progress_func)
    progress_func (progress_data, 1.0);
}

static void
pixel_regions_process_parallel_valist (PixelProcessorFunc         func,
                                       gpointer                   data,
                                       PixelProcessorProgressFunc progress_func,
                                       gpointer                   progress_data,
                                       gint                       num_regions,
                                       va_list                    ap)
{
  PixelProcessor  processor = { NULL, };
  gint            i;

  for (i = 0; i < num_regions; i++)
    processor.regions[i] = va_arg (ap, PixelRegion *);

  switch (num_regions)
    {
    case 1:
      processor.PRI = pixel_regions_register (num_regions,
                                              processor.regions[0]);
      break;

    case 2:
      processor.PRI = pixel_regions_register (num_regions,
                                              processor.regions[0],
                                              processor.regions[1]);
      break;

    case 3:
      processor.PRI = pixel_regions_register (num_regions,
                                              processor.regions[0],
                                              processor.regions[1],
                                              processor.regions[2]);
      break;

    case 4:
      processor.PRI = pixel_regions_register (num_regions,
                                              processor.regions[0],
                                              processor.regions[1],
                                              processor.regions[2],
                                              processor.regions[3]);
      break;

    default:
      g_warning ("pixel_regions_process_parallel: "
                 "bad number of regions (%d)", processor.num_regions);
      break;
    }

  if (! processor.PRI)
    return;

  processor.func        = func;
  processor.data        = data;
  processor.num_regions = num_regions;

#ifdef ENABLE_MP
  processor.threads     = 0;
#endif

  processor.progress    = 0;

  pixel_regions_do_parallel (&processor, progress_func, progress_data);
}

void
pixel_processor_init (gint num_threads)
{
  pixel_processor_set_num_threads (num_threads);
}

void
pixel_processor_set_num_threads (gint num_threads)
{
#ifdef ENABLE_MP

  g_return_if_fail (num_threads > 0 && num_threads <= GIMP_MAX_NUM_THREADS);

  if (num_threads < 2)
    {
      if (pool)
        {
          g_thread_pool_free (pool, TRUE, TRUE);
          pool = NULL;

          g_cond_free (pool_cond);
          pool_cond = NULL;

          g_mutex_free (pool_mutex);
          pool_mutex = NULL;
        }
    }
  else
    {
      GError *error = NULL;

      if (pool)
        {
          g_thread_pool_set_max_threads (pool, num_threads, &error);
        }
      else
        {
          pool = g_thread_pool_new ((GFunc) do_parallel_regions, NULL,
                                    num_threads, TRUE, &error);

          pool_mutex = g_mutex_new ();
          pool_cond  = g_cond_new ();
        }

      if (G_UNLIKELY (error))
        {
          g_warning ("changing the number of threads to %d failed: %s",
                     num_threads, error->message);
          g_clear_error (&error);
        }
    }
#endif
}

void
pixel_processor_exit (void)
{
  pixel_processor_set_num_threads (1);
}

void
pixel_regions_process_parallel (PixelProcessorFunc  func,
                                gpointer            data,
                                gint                num_regions,
                                ...)
{
  va_list va;

  va_start (va, num_regions);

  pixel_regions_process_parallel_valist (func, data,
                                         NULL, NULL,
                                         num_regions, va);

  va_end (va);
}

void
pixel_regions_process_parallel_progress (PixelProcessorFunc          func,
                                         gpointer                    data,
                                         PixelProcessorProgressFunc  progress_func,
                                         gpointer                    progress_data,
                                         gint                        num_regions,
                                         ...)
{
  va_list va;

  va_start (va, num_regions);

  pixel_regions_process_parallel_valist (func, data,
                                         progress_func, progress_data,
                                         num_regions, va);

  va_end (va);
}