Debian Codegraph @

core/libs/dngwriter/extra/dng_sdk/dng_area_task.cpp

raw
1 /*****************************************************************************/
2 // Copyright 2006-2019 Adobe Systems Incorporated
3 // All Rights Reserved.
4 //
5 // NOTICE: Adobe permits you to use, modify, and distribute this file in
6 // accordance with the terms of the Adobe license agreement accompanying it.
7 /*****************************************************************************/
8
9 #include "dng_area_task.h"
10
11 #include "dng_abort_sniffer.h"
12 #include "dng_assertions.h"
13 #include "dng_auto_ptr.h"
14 #include "dng_flags.h"
15 #include "dng_globals.h"
16 #include "dng_host.h"
17 #include "dng_image.h"
18 #include "dng_pixel_buffer.h"
19 #include "dng_sdk_limits.h"
20 #include "dng_tile_iterator.h"
21 #include "dng_utils.h"
22
23 /*****************************************************************************/
24
25 dng_area_task::dng_area_task (const char *name)
26
27 : fMaxThreads (kMaxMPThreads)
28
29 , fMinTaskArea (256 * 256)
30
31 , fUnitCell (1, 1)
32
33 , fMaxTileSize (256, 256)
34
35 , fName ()
36
37 {
38
39 if (!name)
40 {
41 name = "dng_area_task";
42 }
43
44 fName.Set (name);
45
46 }
47
48 /*****************************************************************************/
49
50 dng_area_task::~dng_area_task ()
51 {
52
53 }
54
55 /*****************************************************************************/
56
57 dng_rect dng_area_task::RepeatingTile1 () const
58 {
59
60 return dng_rect ();
61
62 }
63
64 /*****************************************************************************/
65
66 dng_rect dng_area_task::RepeatingTile2 () const
67 {
68
69 return dng_rect ();
70
71 }
72
73 /*****************************************************************************/
74
75 dng_rect dng_area_task::RepeatingTile3 () const
76 {
77
78 return dng_rect ();
79
80 }
81
82 /*****************************************************************************/
83
84 void dng_area_task::Start (uint32 /* threadCount */,
85 const dng_rect & /* dstArea */,
86 const dng_point & /* tileSize */,
87 dng_memory_allocator * /* allocator */,
88 dng_abort_sniffer * /* sniffer */)
89 {
90
91 }
92
93 /*****************************************************************************/
94
95 void dng_area_task::Finish (uint32 /* threadCount */)
96 {
97
98 }
99
100 /*****************************************************************************/
101
102 dng_point dng_area_task::FindTileSize (const dng_rect &area) const
103 {
104
105 dng_rect repeatingTile1 = RepeatingTile1 ();
106 dng_rect repeatingTile2 = RepeatingTile2 ();
107 dng_rect repeatingTile3 = RepeatingTile3 ();
108
109 if (repeatingTile1.IsEmpty ())
110 {
111 repeatingTile1 = area;
112 }
113
114 if (repeatingTile2.IsEmpty ())
115 {
116 repeatingTile2 = area;
117 }
118
119 if (repeatingTile3.IsEmpty ())
120 {
121 repeatingTile3 = area;
122 }
123
124 uint32 repeatV = Min_uint32 (Min_uint32 (repeatingTile1.H (),
125 repeatingTile2.H ()),
126 repeatingTile3.H ());
127
128 uint32 repeatH = Min_uint32 (Min_uint32 (repeatingTile1.W (),
129 repeatingTile2.W ()),
130 repeatingTile3.W ());
131
132 dng_point maxTileSize = MaxTileSize ();
133
134 dng_point tileSize;
135
136 tileSize.v = Min_int32 (repeatV, maxTileSize.v);
137 tileSize.h = Min_int32 (repeatH, maxTileSize.h);
138
139 // Make Xcode happy (div by zero).
140
141 tileSize.v = Max_int32 (tileSize.v, 1);
142 tileSize.h = Max_int32 (tileSize.h, 1);
143
144 // What this is doing is, if the smallest repeating image tile is larger than the
145 // maximum tile size, adjusting the tile size down so that the tiles are as small
146 // as possible while still having the same number of tiles covering the
147 // repeat area. This makes the areas more equal in size, making MP
148 // algorithms work better.
149
150 // The image core team did not understand this code, and disabled it.
151 // Really stupid idea to turn off code you don't understand!
152 // I'm undoing this removal, because I think the code is correct and useful.
153
154 uint32 countV = (repeatV + tileSize.v - 1) / tileSize.v;
155 uint32 countH = (repeatH + tileSize.h - 1) / tileSize.h;
156
157 // Make Xcode happy (div by zero).
158
159 countV = Max_uint32 (countV, 1);
160 countH = Max_uint32 (countH, 1);
161
162 tileSize.v = (repeatV + countV - 1) / countV;
163 tileSize.h = (repeatH + countH - 1) / countH;
164
165 // Round up to unit cell size.
166
167 dng_point unitCell = UnitCell ();
168
169 if (unitCell.h != 1 || unitCell.v != 1)
170 {
171 tileSize.v = ((tileSize.v + unitCell.v - 1) / unitCell.v) * unitCell.v;
172 tileSize.h = ((tileSize.h + unitCell.h - 1) / unitCell.h) * unitCell.h;
173 }
174
175 // But if that is larger than maximum tile size, round down to unit cell size.
176
177 if (tileSize.v > maxTileSize.v)
178 {
179 tileSize.v = (maxTileSize.v / unitCell.v) * unitCell.v;
180 }
181
182 if (tileSize.h > maxTileSize.h)
183 {
184 tileSize.h = (maxTileSize.h / unitCell.h) * unitCell.h;
185 }
186
187 if (gPrintTimings)
188 {
189 fprintf (stdout,
190 "\nRender tile for below: %d x %d\n",
191 (int32) tileSize.h,
192 (int32) tileSize.v);
193 }
194
195 return tileSize;
196
197 }
198
199 /*****************************************************************************/
200
201 void dng_area_task::ProcessOnThread (uint32 threadIndex,
202 const dng_rect &area,
203 const dng_point &tileSize,
204 dng_abort_sniffer *sniffer,
205 dng_area_task_progress *progress)
206 {
207
208 dng_rect repeatingTile1 = RepeatingTile1 ();
209 dng_rect repeatingTile2 = RepeatingTile2 ();
210 dng_rect repeatingTile3 = RepeatingTile3 ();
211
212 if (repeatingTile1.IsEmpty ())
213 {
214 repeatingTile1 = area;
215 }
216
217 if (repeatingTile2.IsEmpty ())
218 {
219 repeatingTile2 = area;
220 }
221
222 if (repeatingTile3.IsEmpty ())
223 {
224 repeatingTile3 = area;
225 }
226
227 dng_rect tile1;
228
229 // TODO_EP: Review & document case where these dynamic allocations appeared to have significant overhead
230 AutoPtr<dng_base_tile_iterator> iter1
231 (MakeTileIterator (threadIndex,
232 repeatingTile3,
233 area));
234
235 while (iter1->GetOneTile (tile1))
236 {
237
238 dng_rect tile2;
239
240 AutoPtr<dng_base_tile_iterator> iter2
241 (MakeTileIterator (threadIndex,
242 repeatingTile2,
243 tile1));
244
245 while (iter2->GetOneTile (tile2))
246 {
247
248 dng_rect tile3;
249
250 AutoPtr<dng_base_tile_iterator> iter3
251 (MakeTileIterator (threadIndex,
252 repeatingTile1,
253 tile2));
254
255 while (iter3->GetOneTile (tile3))
256 {
257
258 dng_rect tile4;
259
260 AutoPtr<dng_base_tile_iterator> iter4
261 (MakeTileIterator (threadIndex,
262 tileSize,
263 tile3));
264
265 while (iter4->GetOneTile (tile4))
266 {
267
268 dng_abort_sniffer::SniffForAbort (sniffer);
269
270 Process (threadIndex, tile4, sniffer);
271
272 if (progress)
273 {
274 progress->FinishedTile (tile4);
275 }
276
277 }
278
279 }
280
281 }
282
283 }
284
285 }
286
287 /*****************************************************************************/
288
289 dng_base_tile_iterator * dng_area_task::MakeTileIterator (uint32 /* threadIndex */,
290 const dng_rect &tile,
291 const dng_rect &area) const
292 {
293
294 return new dng_tile_forward_iterator (tile, area);
295
296 }
297
298 /*****************************************************************************/
299
300 dng_base_tile_iterator * dng_area_task::MakeTileIterator (uint32 /* threadIndex */,
301 const dng_point &tileSize,
302 const dng_rect &area) const
303 {
304
305 return new dng_tile_forward_iterator (tileSize, area);
306
307 }
308
309 /*****************************************************************************/
310
311 void dng_area_task::Perform (dng_area_task &task,
312 const dng_rect &area,
313 dng_memory_allocator *allocator,
314 dng_abort_sniffer *sniffer,
315 dng_area_task_progress *progress)
316 {
317
318 dng_point tileSize (task.FindTileSize (area));
319
320 task.Start (1, area, tileSize, allocator, sniffer);
321
322 task.ProcessOnThread (0, area, tileSize, sniffer, progress);
323
324 task.Finish (1);
325
326 }
327
328 /*****************************************************************************/
329 /*****************************************************************************/
330 /*****************************************************************************/
331
332 dng_range_parallel_task::dng_range_parallel_task (dng_host &host,
333 int32 startIndex,
334 int32 stopIndex,
335 const char *name)
336
337 : dng_area_task ((name != NULL) ? name : "dng_range_parallel_task")
338
339 , fHost (host)
340 , fStartIndex (startIndex)
341 , fStopIndex (stopIndex)
342 , fIndices ()
343
344 {
345
346 DNG_REQUIRE (stopIndex > startIndex,
347 "Invalid start/stop index values");
348
349 fMinTaskArea = kDummySize * kDummySize;
350 fUnitCell = dng_point (kDummySize, kDummySize);
351 fMaxTileSize = dng_point (kDummySize, kDummySize);
352
353 }
354
355 /*****************************************************************************/
356
357 uint32 dng_range_parallel_task::RecommendedThreadCount () const
358 {
359
360 return fHost.PerformAreaTaskThreads ();
361
362 }
363
364 /******************************************************************************/
365
366 int32 dng_range_parallel_task::MinIndicesPerThread () const
367 {
368
369 return 1;
370
371 }
372
373 /******************************************************************************/
374
375 void dng_range_parallel_task::Run ()
376 {
377
378 uint32 threadCount = fHost.PerformAreaTaskThreads ();
379
380 threadCount = Min_uint32 (threadCount,
381 RecommendedThreadCount ());
382
383 const int32 items = fStopIndex - fStartIndex;
384
385 // Find minimum number of items (indices) for each task.
386
387 int32 minItemsPerTask = Max_int32 (1, MinIndicesPerThread ());
388
389 // Find the number of tasks. Has to be at least 1.
390
391 int32 tasks = Max_int32 (items / minItemsPerTask, 1);
392
393 // Limit thread count to # of tasks.
394
395 threadCount = Min_uint32 (threadCount,
396 (uint32) tasks);
397
398 // Find the number of items to process per task. In general this will not
399 // divide evenly, so compute this as a floating-point value. However, it
400 // will be at least 1.0.
401
402 real64 itemsPerThread64 = items / (real64) threadCount;
403
404 // Allocate a vector to hold index ranges.
405
406 fIndices.resize ((size_t) (threadCount + 1));
407
408 // Populate the index ranges for each task.
409
410 real64 idx64 = 0.0;
411
412 for (uint32 i = 0; i <= threadCount; i++)
413 {
414
415 int32 idx = fStartIndex + Round_int32 (idx64);
416
417 fIndices [i] = idx;
418
419 idx64 += itemsPerThread64;
420
421 }
422
423 fHost.PerformAreaTask (*this,
424 dng_rect (0,
425 0,
426 kDummySize,
427 kDummySize * threadCount));
428
429 }
430
431 /******************************************************************************/
432
433 void dng_range_parallel_task::Prepare (uint32 /* threadCount */,
434 dng_memory_allocator * /* allocator */,
435 dng_abort_sniffer * /* sniffer */)
436 {
437
438 }
439
440 /******************************************************************************/
441
442 void dng_range_parallel_task::Start (uint32 threadCount,
443 const dng_rect & /* dstArea */,
444 const dng_point & /* tileSize */,
445 dng_memory_allocator *allocator,
446 dng_abort_sniffer *sniffer)
447 {
448
449 Prepare (threadCount,
450 allocator,
451 sniffer);
452
453 }
454
455 /******************************************************************************/
456
457 void dng_range_parallel_task::Process (uint32 /* threadIndex */,
458 const dng_rect &tile,
459 dng_abort_sniffer *sniffer)
460 {
461
462 // Note: important to use the tile area (not the given threadIndex in the
463 // above parameter) to retrieve the index range for this task.
464
465 int32 index0 = tile.l / kDummySize;
466 int32 index1 = index0 + 1;
467
468 // This should never happen, just being safe.
469
470 if (index0 < 0 || index1 >= (int32) fIndices.size ())
471 {
472 return;
473 }
474
475 int32 startIndex = fIndices [index0];
476 int32 stopIndex = fIndices [index1];
477
478 uint32 threadIndex = (uint32) index0;
479
480 // Do the actual work.
481
482 ProcessRange (threadIndex,
483 startIndex,
484 stopIndex,
485 sniffer);
486
487 }
488
489 /*****************************************************************************/
490
491 class dng_range_parallel_func_task: public dng_range_parallel_task
492 {
493
494 private:
495
496 const uint32 fMinIndicesPerThread;
497
498 const uint32 fRecommendedThreadCount;
499
500 const dng_range_parallel_task::function_t &fFunc;
501
502 public:
503
504 dng_range_parallel_func_task (dng_host &host,
505 const info &params,
506 const char *taskName,
507 const dng_range_parallel_task::function_t &func)
508
509 : dng_range_parallel_task (host,
510 params.fBegin,
511 params.fEnd,
512 taskName)
513
514 , fMinIndicesPerThread (Max_uint32 (params.fMinIndicesPerThread, 1))
515
516 , fRecommendedThreadCount (params.fRecommendedThreadCount)
517
518 , fFunc (func)
519
520 {
521
522 }
523
524 virtual uint32 RecommendedThreadCount () const
525 {
526
527 if (fRecommendedThreadCount > 0)
528 {
529 return fRecommendedThreadCount;
530 }
531
532 return dng_range_parallel_task::RecommendedThreadCount ();
533
534 }
535
536 virtual int32 MinIndicesPerThread () const
537 {
538
539 return fMinIndicesPerThread;
540
541 }
542
543 virtual void ProcessRange (uint32 threadIndex,
544 int32 startIndex,
545 int32 stopIndex,
546 dng_abort_sniffer *sniffer)
547 {
548
549 dng_range_parallel_task::range r;
550
551 r.fThreadIndex = threadIndex;
552 r.fBegin = startIndex;
553 r.fEnd = stopIndex;
554 r.fSniffer = sniffer;
555
556 fFunc (r);
557
558 }
559
560 };
561
562 /*****************************************************************************/
563
564 void dng_range_parallel_task::Do (dng_host &host,
565 const info &params,
566 const char *taskName,
567 const function_t &func)
568 {
569
570 dng_range_parallel_func_task task (host,
571 params,
572 taskName,
573 func);
574
575 task.Run ();
576
577 }
578
579 /*****************************************************************************/
580 /*****************************************************************************/
581 /*****************************************************************************/
582
583 dng_rect dng_get_buffer_task::RepeatingTile1 () const
584 {
585
586 return fSrc.RepeatingTile ();
587
588 }
589
590 /*****************************************************************************/
591
592 void dng_get_buffer_task::Process (uint32 /* threadIndex */,
593 const dng_rect &tile,
594 dng_abort_sniffer * /* sniffer */)
595 {
596
597 dng_pixel_buffer temp = fDst;
598
599 temp.fData = (void *) fDst.DirtyPixel (tile.t, tile.l, temp.fPlane);
600
601 temp.fArea = tile;
602
603 fSrc.Get (temp,
604 fEdgeOption);
605
606 }
607
608 /*****************************************************************************/
609 /*****************************************************************************/
610 /*****************************************************************************/
611
612 dng_copy_buffer_task::dng_copy_buffer_task (const dng_pixel_buffer &src,
613 dng_pixel_buffer &dst)
614
615 : dng_area_task ("dng_copy_buffer_task")
616
617 , fSrc (src)
618 , fDst (dst)
619
620 {
621
622 DNG_REQUIRE (src.Planes () == dst.Planes (),
623 "Mismatched planes");
624
625 }
626
627 /*****************************************************************************/
628
629 dng_rect dng_copy_buffer_task::RepeatingTile1 () const
630 {
631
632 return dng_rect (128, 128);
633
634 }
635
636 /*****************************************************************************/
637
638 void dng_copy_buffer_task::Process (uint32 /* threadIndex */,
639 const dng_rect &tile,
640 dng_abort_sniffer * /* sniffer */)
641 {
642
643 fDst.CopyArea (fSrc,
644 tile,
645 0,
646 fDst.Planes ());
647
648 }
649
650 /*****************************************************************************/
651 /*****************************************************************************/
652 /*****************************************************************************/
653
654 dng_rect dng_put_buffer_task::RepeatingTile1 () const
655 {
656
657 return fDst.RepeatingTile ();
658
659 }
660
661 /*****************************************************************************/
662
663 void dng_put_buffer_task::Process (uint32 /* threadIndex */,
664 const dng_rect &tile,
665 dng_abort_sniffer * /* sniffer */)
666 {
667
668 dng_pixel_buffer temp = fSrc;
669
670 temp.fData = (void *) fSrc.ConstPixel (tile.t,
671 tile.l,
672 temp.fPlane);
673
674 temp.fArea = tile;
675
676 fDst.Put (temp);
677
678 }
679
680 /*****************************************************************************/