1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2016 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of DistortedLibrary. //
|
5
|
// //
|
6
|
// DistortedLibrary 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
|
// DistortedLibrary 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 DistortedLibrary. 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
|
import org.distorted.library.message.EffectListener;
|
28
|
import org.distorted.library.message.EffectMessage;
|
29
|
import org.distorted.library.message.EffectMessageSender;
|
30
|
|
31
|
import java.util.ArrayList;
|
32
|
import java.util.HashMap;
|
33
|
|
34
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
35
|
/**
|
36
|
* Not part of public API, do not document
|
37
|
*
|
38
|
* @y.exclude
|
39
|
*/
|
40
|
public abstract class EffectQueue implements InternalMaster.Slave
|
41
|
{
|
42
|
static final int MAIN_VARIANTS = 3; // Number of Main program variants (ATM 3: MAIN, MAIN OIT, PREPROCESS)
|
43
|
|
44
|
private static final int CREATE = 0;
|
45
|
private static final int ATTACH = 1;
|
46
|
private static final int DETACH = 2;
|
47
|
private static final int DETALL = 3;
|
48
|
|
49
|
int mNumEffects; // 'ToBe' will be more than mNumEffects if doWork() hasn't
|
50
|
private int mNumEffectsToBe; // added them yet (or less if it hasn't removed some yet)
|
51
|
float[] mUniforms;
|
52
|
long[] mCurrentDuration;
|
53
|
Effect[] mEffects;
|
54
|
int[] mName;
|
55
|
long mTime=0;
|
56
|
ArrayList<EffectListener> mListeners =null;
|
57
|
int mNumListeners=0; // ==mListeners.length(), but we only create mListeners if the first one gets added
|
58
|
long mDistortedEffectsID;
|
59
|
|
60
|
private static int[] mMax = new int[EffectType.LENGTH];
|
61
|
private static long mNextID;
|
62
|
private static HashMap<ArrayList<Long>,Long> mMapID = new HashMap<>(); // maps lists of Effect IDs (longs) to a
|
63
|
// single long - the queue ID.
|
64
|
private long mID;
|
65
|
private int mIndex;
|
66
|
private boolean mCreated;
|
67
|
|
68
|
private class Job
|
69
|
{
|
70
|
int type;
|
71
|
int num;
|
72
|
boolean notify;
|
73
|
Effect effect;
|
74
|
|
75
|
Job(int t, int m, boolean n, Effect e)
|
76
|
{
|
77
|
type = t;
|
78
|
num = m;
|
79
|
notify= n;
|
80
|
effect= e;
|
81
|
}
|
82
|
}
|
83
|
|
84
|
private ArrayList<Job> mJobs = new ArrayList<>();
|
85
|
|
86
|
static
|
87
|
{
|
88
|
onDestroy();
|
89
|
}
|
90
|
|
91
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
92
|
|
93
|
EffectQueue(long id, int numUniforms, int index)
|
94
|
{
|
95
|
mCreated = false;
|
96
|
mID = 0;
|
97
|
mNumEffects = 0;
|
98
|
mNumEffectsToBe = 0;
|
99
|
mDistortedEffectsID = id;
|
100
|
mIndex = index;
|
101
|
|
102
|
mJobs.add(new Job(CREATE,numUniforms,false,null)); // create the stuff that depends on max number
|
103
|
InternalMaster.newSlave(this); // of uniforms later, on first render.
|
104
|
}
|
105
|
|
106
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
107
|
|
108
|
public static void allocateQueues(EffectQueue[] queues, EffectQueue[] from, int flags, long id)
|
109
|
{
|
110
|
queues[0] = (flags & DistortedLibrary.CLONE_MATRIX ) != 0 ? from[0] : new EffectQueueMatrix(id);
|
111
|
queues[1] = (flags & DistortedLibrary.CLONE_VERTEX ) != 0 ? from[1] : new EffectQueueVertex(id);
|
112
|
queues[2] = (flags & DistortedLibrary.CLONE_FRAGMENT ) != 0 ? from[2] : new EffectQueueFragment(id);
|
113
|
queues[3] = (flags & DistortedLibrary.CLONE_POSTPROCESS) != 0 ? from[3] : new EffectQueuePostprocess(id);
|
114
|
}
|
115
|
|
116
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
117
|
|
118
|
public static void compute(EffectQueue[] queues, long currTime, float halfW, float halfH, float halfZ )
|
119
|
{
|
120
|
((EffectQueueMatrix )queues[0]).compute(currTime);
|
121
|
((EffectQueueVertex )queues[1]).compute(currTime,halfW,halfH,halfZ);
|
122
|
((EffectQueueFragment )queues[2]).compute(currTime,halfW,halfH,halfZ);
|
123
|
((EffectQueuePostprocess)queues[3]).compute(currTime);
|
124
|
}
|
125
|
|
126
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
127
|
|
128
|
public static void send( EffectQueue[] queues, int width, int height, float distance, float mipmap,
|
129
|
float[] projection, float inflate, float halfW, float halfH, float halfZ, int variant )
|
130
|
{
|
131
|
((EffectQueueMatrix )queues[0]).send(width, height, distance, mipmap, projection, halfW, halfH, halfZ, variant);
|
132
|
((EffectQueueVertex )queues[1]).send(inflate, variant);
|
133
|
((EffectQueueFragment)queues[2]).send(variant);
|
134
|
}
|
135
|
|
136
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
137
|
|
138
|
public static float[] getMVP(EffectQueue[] queues)
|
139
|
{
|
140
|
return ((EffectQueueMatrix)queues[0]).getMVP();
|
141
|
}
|
142
|
|
143
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
144
|
|
145
|
public static void getUniforms(int programH, int variant)
|
146
|
{
|
147
|
EffectQueueFragment.uniforms(programH,variant);
|
148
|
EffectQueueVertex .uniforms(programH,variant);
|
149
|
EffectQueueMatrix .uniforms(programH,variant);
|
150
|
}
|
151
|
|
152
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
153
|
// Every effect queue has an ID, which should be the same iff two queues hold the same effects.
|
154
|
// (this is a speedup: then both queues can be applied once, which seriously speeds up stuff -
|
155
|
// especially important in case of postprocessing)
|
156
|
|
157
|
private void regenerateID()
|
158
|
{
|
159
|
if( mNumEffects>0 )
|
160
|
{
|
161
|
ArrayList<Long> list = new ArrayList<>();
|
162
|
for (int i = 0; i < mNumEffects; i++) list.add(mEffects[i].getID());
|
163
|
Long id = mMapID.get(list);
|
164
|
|
165
|
if( id!=null )
|
166
|
{
|
167
|
mID = id;
|
168
|
}
|
169
|
else
|
170
|
{
|
171
|
mMapID.put(list,mNextID);
|
172
|
mID = mNextID++;
|
173
|
}
|
174
|
}
|
175
|
else
|
176
|
{
|
177
|
mID = 0;
|
178
|
}
|
179
|
}
|
180
|
|
181
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
182
|
|
183
|
public long getID()
|
184
|
{
|
185
|
return mID;
|
186
|
}
|
187
|
|
188
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
189
|
|
190
|
public static boolean setMax(int index, int m)
|
191
|
{
|
192
|
if( !DistortedLibrary.isInitialized() || m<=mMax[index] )
|
193
|
{
|
194
|
mMax[index] = m<0 ? 0:m;
|
195
|
return true;
|
196
|
}
|
197
|
|
198
|
return false;
|
199
|
}
|
200
|
|
201
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
202
|
|
203
|
public static int getMax(int index)
|
204
|
{
|
205
|
return mMax[index];
|
206
|
}
|
207
|
|
208
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
209
|
|
210
|
public void registerForMessages(EffectListener el)
|
211
|
{
|
212
|
if( mListeners==null ) mListeners = new ArrayList<>();
|
213
|
|
214
|
if( !mListeners.contains(el) )
|
215
|
{
|
216
|
mListeners.add(el);
|
217
|
mNumListeners++;
|
218
|
}
|
219
|
}
|
220
|
|
221
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
222
|
|
223
|
public void deregisterForMessages(EffectListener el)
|
224
|
{
|
225
|
if( mListeners.remove(el) )
|
226
|
{
|
227
|
mNumListeners--;
|
228
|
}
|
229
|
}
|
230
|
|
231
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
232
|
|
233
|
public static void onDestroy()
|
234
|
{
|
235
|
mNextID = 1;
|
236
|
mMapID.clear();
|
237
|
EffectType.reset(mMax);
|
238
|
}
|
239
|
|
240
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
241
|
// this assumes 0<=effect<mNumEffects
|
242
|
|
243
|
protected void remove(int effect)
|
244
|
{
|
245
|
mNumEffects--;
|
246
|
|
247
|
long removedID = mEffects[effect].getID();
|
248
|
|
249
|
for(int j=effect; j<mNumEffects; j++ )
|
250
|
{
|
251
|
mEffects[j] = mEffects[j+1];
|
252
|
mCurrentDuration[j] = mCurrentDuration[j+1];
|
253
|
mName[j] = mName[j+1];
|
254
|
}
|
255
|
|
256
|
mEffects[mNumEffects] = null;
|
257
|
|
258
|
for(int i=0; i<mNumListeners; i++)
|
259
|
EffectMessageSender.newMessage( mListeners.get(i), EffectMessage.EFFECT_REMOVED, removedID, mDistortedEffectsID);
|
260
|
}
|
261
|
|
262
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
263
|
|
264
|
public synchronized int removeByName(EffectName name)
|
265
|
{
|
266
|
int ret = 0;
|
267
|
|
268
|
for(int i=0; i<mNumEffects; i++)
|
269
|
{
|
270
|
if( mEffects[i].getName() == name )
|
271
|
{
|
272
|
mJobs.add(new Job(DETACH,0,true,mEffects[i]));
|
273
|
ret++;
|
274
|
}
|
275
|
}
|
276
|
|
277
|
if( ret>0 )
|
278
|
{
|
279
|
InternalMaster.newSlave(this);
|
280
|
mNumEffectsToBe-=ret;
|
281
|
}
|
282
|
|
283
|
return ret;
|
284
|
}
|
285
|
|
286
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
287
|
|
288
|
public synchronized int removeById(long id)
|
289
|
{
|
290
|
for(int i=0; i<mNumEffects; i++)
|
291
|
{
|
292
|
if( mEffects[i].getID() == id )
|
293
|
{
|
294
|
mJobs.add(new Job(DETACH,0,true,mEffects[i]));
|
295
|
InternalMaster.newSlave(this);
|
296
|
mNumEffectsToBe--;
|
297
|
return 1;
|
298
|
}
|
299
|
}
|
300
|
|
301
|
return 0;
|
302
|
}
|
303
|
|
304
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
305
|
|
306
|
public synchronized int removeEffect(Effect effect)
|
307
|
{
|
308
|
for(int i=0; i<mNumEffects; i++)
|
309
|
{
|
310
|
if( mEffects[i]==effect )
|
311
|
{
|
312
|
mJobs.add(new Job(DETACH,0,true,mEffects[i]));
|
313
|
InternalMaster.newSlave(this);
|
314
|
mNumEffectsToBe--;
|
315
|
return 1;
|
316
|
}
|
317
|
}
|
318
|
|
319
|
return 0;
|
320
|
}
|
321
|
|
322
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
323
|
// we do want to notify Listeners if they called 'abortAll' themselves but don't want to notify
|
324
|
// them if it is the library itself which is releasing resources.
|
325
|
|
326
|
public synchronized int abortAll(boolean notify)
|
327
|
{
|
328
|
mJobs.add(new Job(DETALL,0,notify,null));
|
329
|
InternalMaster.newSlave(this);
|
330
|
mNumEffectsToBe = 0;
|
331
|
return mNumEffects;
|
332
|
}
|
333
|
|
334
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
335
|
|
336
|
public boolean add(Effect effect)
|
337
|
{
|
338
|
if( mMax[mIndex]>mNumEffectsToBe || !mCreated )
|
339
|
{
|
340
|
mJobs.add(new Job(ATTACH,0,false,effect));
|
341
|
InternalMaster.newSlave(this);
|
342
|
mNumEffectsToBe++;
|
343
|
return true;
|
344
|
}
|
345
|
|
346
|
return false;
|
347
|
}
|
348
|
|
349
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
350
|
|
351
|
public void doWork()
|
352
|
{
|
353
|
boolean changed = false;
|
354
|
int num = mJobs.size();
|
355
|
Job job;
|
356
|
|
357
|
for(int i=0; i<num; i++)
|
358
|
{
|
359
|
job = mJobs.remove(0);
|
360
|
|
361
|
switch(job.type)
|
362
|
{
|
363
|
case CREATE: int max = mMax[mIndex];
|
364
|
if( max>0 )
|
365
|
{
|
366
|
mUniforms = new float[max*job.num];
|
367
|
mCurrentDuration = new long[max];
|
368
|
mEffects = new Effect[max];
|
369
|
mName = new int[max];
|
370
|
}
|
371
|
mCreated = true;
|
372
|
|
373
|
break;
|
374
|
case ATTACH: if( mMax[mIndex]>mNumEffects ) // it is possible that we have first
|
375
|
{ // added effects and then lowered mMax
|
376
|
mCurrentDuration[mNumEffects] = 0;
|
377
|
mEffects[mNumEffects] = job.effect;
|
378
|
mName[mNumEffects] = job.effect.getName().ordinal();
|
379
|
mNumEffects++;
|
380
|
changed = true;
|
381
|
}
|
382
|
else
|
383
|
{
|
384
|
android.util.Log.e("queue", "failed to add effect "+job.effect.getName());
|
385
|
}
|
386
|
break;
|
387
|
case DETACH: for(int j=0; j<mNumEffects; j++)
|
388
|
{
|
389
|
if (mEffects[j] == job.effect)
|
390
|
{
|
391
|
remove(j);
|
392
|
changed = true;
|
393
|
break;
|
394
|
}
|
395
|
}
|
396
|
break;
|
397
|
case DETALL: for(int j=0; j<mNumEffects; j++ )
|
398
|
{
|
399
|
changed = true;
|
400
|
|
401
|
if( job.notify )
|
402
|
{
|
403
|
for(int k=0; k<mNumListeners; k++)
|
404
|
EffectMessageSender.newMessage( mListeners.get(k), EffectMessage.EFFECT_REMOVED, mEffects[j].getID(), mDistortedEffectsID);
|
405
|
}
|
406
|
|
407
|
mEffects[j] = null;
|
408
|
}
|
409
|
|
410
|
mNumEffects= 0;
|
411
|
break;
|
412
|
}
|
413
|
}
|
414
|
|
415
|
if( changed && mIndex==EffectType.POSTPROCESS.ordinal() ) regenerateID();
|
416
|
}
|
417
|
}
|