1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2016 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Distorted. //
|
5
|
// //
|
6
|
// Distorted is free software: you can redistribute it and/or modify //
|
7
|
// it under the terms of the GNU General Public License as published by //
|
8
|
// the Free Software Foundation, either version 2 of the License, or //
|
9
|
// (at your option) any later version. //
|
10
|
// //
|
11
|
// Distorted is distributed in the hope that it will be useful, //
|
12
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
14
|
// GNU General Public License for more details. //
|
15
|
// //
|
16
|
// You should have received a copy of the GNU General Public License //
|
17
|
// along with Distorted. If not, see <http://www.gnu.org/licenses/>. //
|
18
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19
|
|
20
|
package org.distorted.library.effectqueue;
|
21
|
|
22
|
import org.distorted.library.effect.Effect;
|
23
|
import org.distorted.library.effect.EffectName;
|
24
|
import org.distorted.library.effect.EffectType;
|
25
|
import org.distorted.library.main.DistortedLibrary;
|
26
|
import org.distorted.library.main.InternalMaster;
|
27
|
|
28
|
import java.util.ArrayList;
|
29
|
import java.util.HashMap;
|
30
|
|
31
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
32
|
/**
|
33
|
* Not part of public API, do not document
|
34
|
*
|
35
|
* @y.exclude
|
36
|
*/
|
37
|
public abstract class EffectQueue implements InternalMaster.Slave
|
38
|
{
|
39
|
public static final int MAIN_VARIANTS = 4; // Number of Main program variants (ATM 4: MAIN, MAIN OIT, PREPROCESS, FULL)
|
40
|
|
41
|
private static final int CREATE = 0;
|
42
|
private static final int ATTACH = 1;
|
43
|
private static final int DETACH = 2;
|
44
|
private static final int DETALL = 3;
|
45
|
|
46
|
long mTime;
|
47
|
int mNumEffects; // 'ToBe' will be more than mNumEffects if doWork() hasn't
|
48
|
private int mNumEffectsToBe; // added them yet (or less if it hasn't removed some yet)
|
49
|
private int mNumFloatUniforms, mNumIntUniforms;
|
50
|
Effect[] mEffects;
|
51
|
|
52
|
float[] mFloatUniforms;
|
53
|
int[] mIntUniforms;
|
54
|
|
55
|
static int[] mMax = new int[EffectType.LENGTH];
|
56
|
private static long mNextID;
|
57
|
private static HashMap<ArrayList<Long>,Long> mMapID = new HashMap<>(); // maps lists of Effect IDs (longs) to a
|
58
|
// single long - the queue ID.
|
59
|
private long mID;
|
60
|
private int mIndex;
|
61
|
private boolean mCreated;
|
62
|
|
63
|
private static class Job
|
64
|
{
|
65
|
int type;
|
66
|
int num1, num2;
|
67
|
boolean notify;
|
68
|
Effect effect;
|
69
|
|
70
|
Job(int t, int m1, int m2, boolean n, Effect e)
|
71
|
{
|
72
|
type = t;
|
73
|
num1 = m1;
|
74
|
num2 = m2;
|
75
|
notify= n;
|
76
|
effect= e;
|
77
|
}
|
78
|
}
|
79
|
|
80
|
private ArrayList<Job> mJobs = new ArrayList<>();
|
81
|
|
82
|
static
|
83
|
{
|
84
|
onDestroy();
|
85
|
}
|
86
|
|
87
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
88
|
|
89
|
EffectQueue(int numFloatUniforms, int numIntUniforms, int index)
|
90
|
{
|
91
|
mCreated = false;
|
92
|
mTime = 0;
|
93
|
mID = 0;
|
94
|
mNumEffects = 0;
|
95
|
mNumEffectsToBe = 0;
|
96
|
mIndex = index;
|
97
|
mNumFloatUniforms = numFloatUniforms;
|
98
|
mNumIntUniforms = numIntUniforms;
|
99
|
|
100
|
mJobs.add(new Job(CREATE,numFloatUniforms,numIntUniforms, false,null)); // create the stuff that depends on max number
|
101
|
InternalMaster.newSlave(this); // of uniforms later, on first render.
|
102
|
}
|
103
|
|
104
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
105
|
// this is not thread safe! The 'source' might change while we're copying it.
|
106
|
|
107
|
EffectQueue(EffectQueue source)
|
108
|
{
|
109
|
if( !source.mCreated )
|
110
|
{
|
111
|
mCreated = false;
|
112
|
mTime = 0;
|
113
|
mID = 0;
|
114
|
mNumEffects = 0;
|
115
|
mNumEffectsToBe = 0;
|
116
|
mIndex = source.mIndex;
|
117
|
mNumFloatUniforms = source.mNumFloatUniforms;
|
118
|
mNumIntUniforms = source.mNumIntUniforms;
|
119
|
|
120
|
int numJobs = source.mJobs.size();
|
121
|
|
122
|
for(int i=0; i<numJobs; i++)
|
123
|
{
|
124
|
Job job = source.mJobs.get(i);
|
125
|
mJobs.add(job);
|
126
|
}
|
127
|
|
128
|
InternalMaster.newSlave(this);
|
129
|
}
|
130
|
else
|
131
|
{
|
132
|
mCreated = true;
|
133
|
mTime = source.mTime;
|
134
|
mID = source.mID;
|
135
|
mNumEffects = source.mNumEffects;
|
136
|
mNumEffectsToBe = source.mNumEffectsToBe;
|
137
|
mIndex = source.mIndex;
|
138
|
mNumFloatUniforms = source.mNumFloatUniforms;
|
139
|
mNumIntUniforms = source.mNumIntUniforms;
|
140
|
|
141
|
int max = mMax[mIndex];
|
142
|
|
143
|
if( max>0 )
|
144
|
{
|
145
|
mEffects = new Effect[max];
|
146
|
mFloatUniforms = new float[max*source.mNumFloatUniforms];
|
147
|
mIntUniforms = new int[max*source.mNumIntUniforms];
|
148
|
|
149
|
if( mNumEffects>=0 )
|
150
|
{
|
151
|
System.arraycopy(source.mEffects, 0, mEffects, 0, mNumEffects);
|
152
|
}
|
153
|
|
154
|
if( mNumIntUniforms*mNumEffects>=0 )
|
155
|
{
|
156
|
System.arraycopy(source.mIntUniforms, 0, mIntUniforms, 0, mNumIntUniforms*mNumEffects);
|
157
|
}
|
158
|
}
|
159
|
}
|
160
|
}
|
161
|
|
162
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
163
|
|
164
|
public static void allocateQueues(EffectQueue[] queues, EffectQueue[] from, int flags)
|
165
|
{
|
166
|
queues[0] = (flags & DistortedLibrary.CLONE_MATRIX ) != 0 ? from[0] : new EffectQueueMatrix();
|
167
|
queues[1] = (flags & DistortedLibrary.CLONE_VERTEX ) != 0 ? from[1] : new EffectQueueVertex();
|
168
|
queues[2] = (flags & DistortedLibrary.CLONE_FRAGMENT ) != 0 ? from[2] : new EffectQueueFragment();
|
169
|
queues[3] = (flags & DistortedLibrary.CLONE_POSTPROCESS) != 0 ? from[3] : new EffectQueuePostprocess();
|
170
|
}
|
171
|
|
172
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
173
|
|
174
|
public static void compute(EffectQueue[] queues, long currTime)
|
175
|
{
|
176
|
((EffectQueueMatrix )queues[0]).compute(currTime);
|
177
|
((EffectQueueVertex )queues[1]).compute(currTime);
|
178
|
((EffectQueueFragment )queues[2]).compute(currTime);
|
179
|
((EffectQueuePostprocess)queues[3]).compute(currTime);
|
180
|
}
|
181
|
|
182
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
183
|
|
184
|
public static void send(EffectQueue[] queues, float distance, float mipmap,
|
185
|
float[] projection, float inflate, int variant )
|
186
|
{
|
187
|
((EffectQueueMatrix )queues[0]).send(distance, mipmap, projection, variant);
|
188
|
((EffectQueueVertex )queues[1]).send(inflate, variant);
|
189
|
((EffectQueueFragment)queues[2]).send(variant);
|
190
|
}
|
191
|
|
192
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
193
|
// variant: 0 --> MAIN 1 --> OIT 2 --> prePOST 3 --> FULL
|
194
|
|
195
|
public static void getUniforms(int programH, int variant)
|
196
|
{
|
197
|
EffectQueueFragment.uniforms(programH,variant);
|
198
|
EffectQueueVertex .uniforms(programH,variant);
|
199
|
EffectQueueMatrix .uniforms(programH,variant);
|
200
|
}
|
201
|
|
202
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
203
|
// Every effect queue has an ID, which should be the same iff two queues hold the same effects.
|
204
|
// (this is a speedup: then both queues can be applied once, which seriously speeds up stuff -
|
205
|
// especially important in case of postprocessing)
|
206
|
|
207
|
private void regenerateID()
|
208
|
{
|
209
|
if( mNumEffects>0 )
|
210
|
{
|
211
|
ArrayList<Long> list = new ArrayList<>();
|
212
|
for (int i = 0; i < mNumEffects; i++) list.add(mEffects[i].getID());
|
213
|
Long id = mMapID.get(list);
|
214
|
|
215
|
if( id!=null )
|
216
|
{
|
217
|
mID = id;
|
218
|
}
|
219
|
else
|
220
|
{
|
221
|
mMapID.put(list,mNextID);
|
222
|
mID = mNextID++;
|
223
|
}
|
224
|
}
|
225
|
else
|
226
|
{
|
227
|
mID = 0;
|
228
|
}
|
229
|
}
|
230
|
|
231
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
232
|
|
233
|
public long getID()
|
234
|
{
|
235
|
return mID;
|
236
|
}
|
237
|
|
238
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
239
|
|
240
|
public static boolean setMax(int index, int m)
|
241
|
{
|
242
|
if( !DistortedLibrary.isInitialized() || m<=mMax[index] )
|
243
|
{
|
244
|
mMax[index] = m<0 ? 0:m;
|
245
|
return true;
|
246
|
}
|
247
|
|
248
|
return false;
|
249
|
}
|
250
|
|
251
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
252
|
|
253
|
public static int getMax(int index)
|
254
|
{
|
255
|
return mMax[index];
|
256
|
}
|
257
|
|
258
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
259
|
|
260
|
public static void onDestroy()
|
261
|
{
|
262
|
mNextID = 1;
|
263
|
mMapID.clear();
|
264
|
EffectType.reset(mMax);
|
265
|
}
|
266
|
|
267
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
268
|
// this assumes 0<=effect
|
269
|
|
270
|
private void removeNow(int pos)
|
271
|
{
|
272
|
if( mNumEffects>pos )
|
273
|
{
|
274
|
mNumEffects--;
|
275
|
System.arraycopy(mEffects, pos+1, mEffects, pos, mNumEffects-pos);
|
276
|
System.arraycopy(mIntUniforms, mNumIntUniforms*(pos+1), mIntUniforms, mNumIntUniforms*pos, mNumIntUniforms*(mNumEffects-pos) );
|
277
|
mEffects[mNumEffects] = null;
|
278
|
}
|
279
|
}
|
280
|
|
281
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
282
|
|
283
|
private void addNow(int pos, Effect effect)
|
284
|
{
|
285
|
mEffects[pos] = effect;
|
286
|
mIntUniforms[mNumIntUniforms*pos] = effect.getName().ordinal();
|
287
|
|
288
|
if( mIndex==EffectType.VERTEX.ordinal() )
|
289
|
{
|
290
|
effect.writeAssociations(mIntUniforms, mNumIntUniforms*pos+1, mNumIntUniforms*pos+3);
|
291
|
}
|
292
|
}
|
293
|
|
294
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
295
|
|
296
|
public synchronized int removeByName(EffectName name)
|
297
|
{
|
298
|
int ret = 0;
|
299
|
|
300
|
for(int i=0; i<mNumEffects; i++)
|
301
|
{
|
302
|
if( mEffects[i].getName() == name )
|
303
|
{
|
304
|
mJobs.add(new Job(DETACH,0,0,true,mEffects[i]));
|
305
|
ret++;
|
306
|
}
|
307
|
}
|
308
|
|
309
|
if( ret>0 )
|
310
|
{
|
311
|
InternalMaster.newSlave(this);
|
312
|
mNumEffectsToBe-=ret;
|
313
|
}
|
314
|
|
315
|
return ret;
|
316
|
}
|
317
|
|
318
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
319
|
|
320
|
public synchronized int removeById(long id)
|
321
|
{
|
322
|
for(int i=0; i<mNumEffects; i++)
|
323
|
{
|
324
|
if( mEffects[i].getID() == id )
|
325
|
{
|
326
|
mJobs.add(new Job(DETACH,0,0,true,mEffects[i]));
|
327
|
InternalMaster.newSlave(this);
|
328
|
mNumEffectsToBe--;
|
329
|
return 1;
|
330
|
}
|
331
|
}
|
332
|
|
333
|
return 0;
|
334
|
}
|
335
|
|
336
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
337
|
|
338
|
public synchronized int removeEffect(Effect effect)
|
339
|
{
|
340
|
for(int i=0; i<mNumEffects; i++)
|
341
|
{
|
342
|
if( mEffects[i]==effect )
|
343
|
{
|
344
|
mJobs.add(new Job(DETACH,0,0,true,mEffects[i]));
|
345
|
InternalMaster.newSlave(this);
|
346
|
mNumEffectsToBe--;
|
347
|
return 1;
|
348
|
}
|
349
|
}
|
350
|
|
351
|
return 0;
|
352
|
}
|
353
|
|
354
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
355
|
// we do want to notify Listeners if they called 'abortAll' themselves but don't want to notify
|
356
|
// them if it is the library itself which is releasing resources.
|
357
|
|
358
|
public synchronized int removeAll(boolean notify)
|
359
|
{
|
360
|
mJobs.add(new Job(DETALL,0,0,notify,null));
|
361
|
InternalMaster.newSlave(this);
|
362
|
mNumEffectsToBe = 0;
|
363
|
return mNumEffects;
|
364
|
}
|
365
|
|
366
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
367
|
|
368
|
public boolean add(Effect effect)
|
369
|
{
|
370
|
if( mMax[mIndex]>mNumEffectsToBe || !mCreated )
|
371
|
{
|
372
|
mJobs.add(new Job(ATTACH,-1,0,false,effect));
|
373
|
InternalMaster.newSlave(this);
|
374
|
mNumEffectsToBe++;
|
375
|
return true;
|
376
|
}
|
377
|
|
378
|
return false;
|
379
|
}
|
380
|
|
381
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
382
|
|
383
|
public boolean add(Effect effect, int position)
|
384
|
{
|
385
|
if( mMax[mIndex]>mNumEffectsToBe || !mCreated )
|
386
|
{
|
387
|
mJobs.add(new Job(ATTACH,position,0,false,effect));
|
388
|
InternalMaster.newSlave(this);
|
389
|
mNumEffectsToBe++;
|
390
|
return true;
|
391
|
}
|
392
|
|
393
|
return false;
|
394
|
}
|
395
|
|
396
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
397
|
|
398
|
public int getNumEffects()
|
399
|
{
|
400
|
return mNumEffects;
|
401
|
}
|
402
|
|
403
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
404
|
|
405
|
public int getNumEffectsToBe()
|
406
|
{
|
407
|
return mNumEffectsToBe;
|
408
|
}
|
409
|
|
410
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
411
|
|
412
|
public Effect getEffect(int position)
|
413
|
{
|
414
|
if( position>=0 && position< mNumEffects )
|
415
|
{
|
416
|
return mEffects[position];
|
417
|
}
|
418
|
else
|
419
|
{
|
420
|
android.util.Log.e("queue", "getEffect: out of range "+position);
|
421
|
return null;
|
422
|
}
|
423
|
}
|
424
|
|
425
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
426
|
|
427
|
public void doWork()
|
428
|
{
|
429
|
boolean changed = false;
|
430
|
int num = mJobs.size();
|
431
|
Job job;
|
432
|
|
433
|
for(int i=0; i<num; i++)
|
434
|
{
|
435
|
job = mJobs.remove(0);
|
436
|
|
437
|
switch(job.type)
|
438
|
{
|
439
|
case CREATE: int max = mMax[mIndex];
|
440
|
if( max>0 )
|
441
|
{
|
442
|
mEffects = new Effect[max];
|
443
|
mFloatUniforms = new float[max*job.num1];
|
444
|
mIntUniforms = new int[max*job.num2];
|
445
|
}
|
446
|
mCreated = true;
|
447
|
|
448
|
break;
|
449
|
case ATTACH: if( mMax[mIndex]>mNumEffects ) // it is possible that we have first
|
450
|
{ // added effects and then lowered mMax
|
451
|
int position = job.num1;
|
452
|
|
453
|
if( position<0 )
|
454
|
{
|
455
|
addNow(mNumEffects,job.effect);
|
456
|
mNumEffects++;
|
457
|
changed = true;
|
458
|
}
|
459
|
else if( position<=mNumEffects )
|
460
|
{
|
461
|
System.arraycopy(mEffects , position, mEffects , position+1, mNumEffects-position);
|
462
|
System.arraycopy(mIntUniforms, mNumIntUniforms*position, mIntUniforms, mNumIntUniforms*(position+1), mNumIntUniforms*(mNumEffects-position) );
|
463
|
addNow(position,job.effect);
|
464
|
mNumEffects++;
|
465
|
changed = true;
|
466
|
}
|
467
|
}
|
468
|
else
|
469
|
{
|
470
|
android.util.Log.e("queue", "failed to add effect "+job.effect.getName());
|
471
|
}
|
472
|
break;
|
473
|
case DETACH: for(int j=0; j<mNumEffects; j++)
|
474
|
{
|
475
|
if (mEffects[j] == job.effect)
|
476
|
{
|
477
|
removeNow(j);
|
478
|
changed = true;
|
479
|
break;
|
480
|
}
|
481
|
}
|
482
|
break;
|
483
|
case DETALL: for(int j=0; j<mNumEffects; j++ )
|
484
|
{
|
485
|
changed = true;
|
486
|
mEffects[j] = null;
|
487
|
}
|
488
|
|
489
|
mNumEffects= 0;
|
490
|
break;
|
491
|
}
|
492
|
}
|
493
|
|
494
|
if( changed && mIndex==EffectType.POSTPROCESS.ordinal() ) regenerateID();
|
495
|
}
|
496
|
}
|