summaryrefslogtreecommitdiff
path: root/gpr/source/lib/dng_sdk/dng_area_task.cpp
blob: 25b100ea37fdc14ac723fda63a4f1961df309b01 (plain)
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
/*****************************************************************************/
// Copyright 2006-2012 Adobe Systems Incorporated
// All Rights Reserved.
//
// NOTICE:  Adobe permits you to use, modify, and distribute this file in
// accordance with the terms of the Adobe license agreement accompanying it.
/*****************************************************************************/

/* $Id: //mondo/dng_sdk_1_4/dng_sdk/source/dng_area_task.cpp#1 $ */ 
/* $DateTime: 2012/05/30 13:28:51 $ */
/* $Change: 832332 $ */
/* $Author: tknoll $ */

/*****************************************************************************/

#include "dng_area_task.h"

#include "dng_abort_sniffer.h"
#include "dng_flags.h"
#include "dng_sdk_limits.h"
#include "dng_tile_iterator.h"
#include "dng_utils.h"

#if qImagecore
extern bool gPrintTimings;
#endif

/*****************************************************************************/

dng_area_task::dng_area_task ()

	:	fMaxThreads   (kMaxMPThreads)
	
	,	fMinTaskArea  (256 * 256)
	
	,	fUnitCell	  (1, 1)
	
	,	fMaxTileSize  (256, 256)
	
	{
	
	}

/*****************************************************************************/

dng_area_task::~dng_area_task ()
	{
	
	}

/*****************************************************************************/

dng_rect dng_area_task::RepeatingTile1 () const
	{
	
	return dng_rect ();
	
	}
		
/*****************************************************************************/

dng_rect dng_area_task::RepeatingTile2 () const
	{
	
	return dng_rect ();
	
	}
		
/*****************************************************************************/

dng_rect dng_area_task::RepeatingTile3 () const
	{
	
	return dng_rect ();
	
	}
		
/*****************************************************************************/

void dng_area_task::Start (uint32 /* threadCount */,
						   const dng_point & /* tileSize */,
						   dng_memory_allocator * /* allocator */,
						   dng_abort_sniffer * /* sniffer */)
	{
	
	}

/*****************************************************************************/

void dng_area_task::Finish (uint32 /* threadCount */)
	{
	
	}
	
/*****************************************************************************/

dng_point dng_area_task::FindTileSize (const dng_rect &area) const
	{
	
	dng_rect repeatingTile1 = RepeatingTile1 ();
	dng_rect repeatingTile2 = RepeatingTile2 ();
	dng_rect repeatingTile3 = RepeatingTile3 ();
	
	if (repeatingTile1.IsEmpty ())
		{
		repeatingTile1 = area;
		}
	
	if (repeatingTile2.IsEmpty ())
		{
		repeatingTile2 = area;
		}
	
	if (repeatingTile3.IsEmpty ())
		{
		repeatingTile3 = area;
		}
		
	uint32 repeatV = Min_uint32 (Min_uint32 (repeatingTile1.H (),
											 repeatingTile2.H ()),
											 repeatingTile3.H ());
	
	uint32 repeatH = Min_uint32 (Min_uint32 (repeatingTile1.W (),
											 repeatingTile2.W ()),
											 repeatingTile3.W ());
	
	dng_point maxTileSize = MaxTileSize ();

	dng_point tileSize;
		
	tileSize.v = Min_int32 (repeatV, maxTileSize.v);
	tileSize.h = Min_int32 (repeatH, maxTileSize.h);
	
	// What this is doing is, if the smallest repeating image tile is larger than the 
	// maximum tile size, adjusting the tile size down so that the tiles are as small
	// as possible while still having the same number of tiles covering the
	// repeat area.  This makes the areas more equal in size, making MP
	// algorithms work better.
						
	// The image core team did not understand this code, and disabled it.
	// Really stupid idea to turn off code you don't understand!
	// I'm undoing this removal, because I think the code is correct and useful.

	uint32 countV = (repeatV + tileSize.v - 1) / tileSize.v;
	uint32 countH = (repeatH + tileSize.h - 1) / tileSize.h;
	
	tileSize.v = (repeatV + countV - 1) / countV;
	tileSize.h = (repeatH + countH - 1) / countH;
	
	// Round up to unit cell size.
	
	dng_point unitCell = UnitCell ();
	
	if (unitCell.h != 1 || unitCell.v != 1)
		{
		tileSize.v = ((tileSize.v + unitCell.v - 1) / unitCell.v) * unitCell.v;
		tileSize.h = ((tileSize.h + unitCell.h - 1) / unitCell.h) * unitCell.h;
		}
		
	// But if that is larger than maximum tile size, round down to unit cell size.
	
	if (tileSize.v > maxTileSize.v)
		{
		tileSize.v = (maxTileSize.v / unitCell.v) * unitCell.v;
		}

	if (tileSize.h > maxTileSize.h)
		{
		tileSize.h = (maxTileSize.h / unitCell.h) * unitCell.h;
		}
		
	#if qImagecore	
    if (gPrintTimings)
		{
        fprintf (stdout, "\nRender tile for below: %d x %d\n", (int32) tileSize.h, (int32) tileSize.v);
		}	
	#endif

	return tileSize;
	
	}
		
/*****************************************************************************/

void dng_area_task::ProcessOnThread (uint32 threadIndex,
									 const dng_rect &area,
									 const dng_point &tileSize,
									 dng_abort_sniffer *sniffer)
	{
	
	dng_rect repeatingTile1 = RepeatingTile1 ();
	dng_rect repeatingTile2 = RepeatingTile2 ();
	dng_rect repeatingTile3 = RepeatingTile3 ();
	
	if (repeatingTile1.IsEmpty ())
		{
		repeatingTile1 = area;
		}
	
	if (repeatingTile2.IsEmpty ())
		{
		repeatingTile2 = area;
		}
	
	if (repeatingTile3.IsEmpty ())
		{
		repeatingTile3 = area;
		}
		
	dng_rect tile1;
	
	dng_tile_iterator iter1 (repeatingTile3, area);
	
	while (iter1.GetOneTile (tile1))
		{
		
		dng_rect tile2;
		
		dng_tile_iterator iter2 (repeatingTile2, tile1);
		
		while (iter2.GetOneTile (tile2))
			{
			
			dng_rect tile3;
			
			dng_tile_iterator iter3 (repeatingTile1, tile2);
			
			while (iter3.GetOneTile (tile3))
				{
				
				dng_rect tile4;
				
				dng_tile_iterator iter4 (tileSize, tile3);
				
				while (iter4.GetOneTile (tile4))
					{
					
					dng_abort_sniffer::SniffForAbort (sniffer);
					
					Process (threadIndex, tile4, sniffer);
					
					}
					
				}
				
			}
		
		}
	
	}
		
/*****************************************************************************/

void dng_area_task::Perform (dng_area_task &task,
				  			 const dng_rect &area,
				  			 dng_memory_allocator *allocator,
				  			 dng_abort_sniffer *sniffer)
	{
	
	dng_point tileSize (task.FindTileSize (area));
		
	task.Start (1, tileSize, allocator, sniffer);
	
	task.ProcessOnThread (0, area, tileSize, sniffer);
			
	task.Finish (1);
	
	}

/*****************************************************************************/