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.main;
|
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.message.EffectListener;
|
26
|
import org.distorted.library.message.EffectMessage;
|
27
|
|
28
|
import java.util.ArrayList;
|
29
|
|
30
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
31
|
|
32
|
abstract class EffectQueue implements DistortedSlave
|
33
|
{
|
34
|
private static final int ATTACH = 0;
|
35
|
private static final int DETACH = 1;
|
36
|
private static final int DETALL = 2;
|
37
|
|
38
|
protected int mNumEffects;
|
39
|
protected float[] mUniforms;
|
40
|
protected long[] mCurrentDuration;
|
41
|
protected Effect[] mEffects;
|
42
|
protected int[] mName;
|
43
|
protected long mTime=0;
|
44
|
protected static int[] mMax = new int[EffectType.LENGTH];
|
45
|
protected ArrayList<EffectListener> mListeners =null;
|
46
|
protected int mNumListeners=0; // ==mListeners.length(), but we only create mListeners if the first one gets added
|
47
|
protected long mDistortedEffectsID;
|
48
|
|
49
|
private ArrayList<DistortedNode> mNodes = null;
|
50
|
private long mID;
|
51
|
private int mNumEffectsToBe; // this will be more than mNumEffects if doWork hasn't really added them yet
|
52
|
private static boolean mCreated;
|
53
|
private int mIndex;
|
54
|
private int mNumUniforms;
|
55
|
|
56
|
private class Job
|
57
|
{
|
58
|
int type;
|
59
|
int index;
|
60
|
boolean notify;
|
61
|
Effect effect;
|
62
|
|
63
|
Job(int t, int i, boolean n, Effect e)
|
64
|
{
|
65
|
type = t;
|
66
|
index = i;
|
67
|
notify= n;
|
68
|
effect= e;
|
69
|
}
|
70
|
}
|
71
|
|
72
|
private ArrayList<Job> mJobs = new ArrayList<>();
|
73
|
|
74
|
static
|
75
|
{
|
76
|
onDestroy();
|
77
|
}
|
78
|
|
79
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
80
|
|
81
|
EffectQueue(long id, int numUniforms, int index)
|
82
|
{
|
83
|
mID = 0;
|
84
|
mNumEffects = 0;
|
85
|
mNumEffectsToBe = 0;
|
86
|
mDistortedEffectsID = id;
|
87
|
mIndex = index;
|
88
|
mNumUniforms = numUniforms;
|
89
|
|
90
|
int max = mMax[mIndex];
|
91
|
|
92
|
if( max>0 )
|
93
|
{
|
94
|
mUniforms = new float[mNumUniforms*max];
|
95
|
mCurrentDuration = new long[max];
|
96
|
mEffects = new Effect[max];
|
97
|
mName = new int[max];
|
98
|
}
|
99
|
|
100
|
mCreated = true;
|
101
|
}
|
102
|
|
103
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
104
|
// TODO: introduce a Map to return an ID dependant on all Effects in the Queue.
|
105
|
// For now this is good enough.
|
106
|
|
107
|
private void regenerateID()
|
108
|
{
|
109
|
mID = mNumEffects>0 ? mEffects[0].getID() : 0;
|
110
|
}
|
111
|
|
112
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
113
|
|
114
|
void newNode(DistortedNode node)
|
115
|
{
|
116
|
if( mNodes==null ) mNodes = new ArrayList<>();
|
117
|
|
118
|
mNodes.add(node);
|
119
|
}
|
120
|
|
121
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
122
|
|
123
|
@SuppressWarnings("unused")
|
124
|
int getNumEffects()
|
125
|
{
|
126
|
return mNumEffects;
|
127
|
}
|
128
|
|
129
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
130
|
|
131
|
long getID()
|
132
|
{
|
133
|
return mID;
|
134
|
}
|
135
|
|
136
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
137
|
|
138
|
static boolean setMax(int index, int m)
|
139
|
{
|
140
|
if( (!mCreated && !Distorted.isInitialized()) || m<=mMax[index] )
|
141
|
{
|
142
|
mMax[index] = m<0 ? 0:m;
|
143
|
return true;
|
144
|
}
|
145
|
|
146
|
return false;
|
147
|
}
|
148
|
|
149
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
150
|
|
151
|
static int getMax(int index)
|
152
|
{
|
153
|
return mMax[index];
|
154
|
}
|
155
|
|
156
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
157
|
|
158
|
void registerForMessages(EffectListener el)
|
159
|
{
|
160
|
if( mListeners==null ) mListeners = new ArrayList<>();
|
161
|
|
162
|
if( !mListeners.contains(el) )
|
163
|
{
|
164
|
mListeners.add(el);
|
165
|
mNumListeners++;
|
166
|
}
|
167
|
}
|
168
|
|
169
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
170
|
|
171
|
void deregisterForMessages(EffectListener el)
|
172
|
{
|
173
|
if( mListeners.remove(el) )
|
174
|
{
|
175
|
mNumListeners--;
|
176
|
}
|
177
|
}
|
178
|
|
179
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
180
|
|
181
|
static void onDestroy()
|
182
|
{
|
183
|
EffectType.reset(mMax);
|
184
|
mCreated = false;
|
185
|
}
|
186
|
|
187
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
188
|
// this assumes 0<=effect<mNumEffects
|
189
|
|
190
|
protected void remove(int effect)
|
191
|
{
|
192
|
mNumEffects--;
|
193
|
|
194
|
long removedID = mEffects[effect].getID();
|
195
|
|
196
|
for(int j=effect; j<mNumEffects; j++ )
|
197
|
{
|
198
|
mEffects[j] = mEffects[j+1];
|
199
|
mCurrentDuration[j] = mCurrentDuration[j+1];
|
200
|
mName[j] = mName[j+1];
|
201
|
|
202
|
for(int k=0; k<mNumUniforms; k++)
|
203
|
mUniforms[mNumUniforms*j+k] = mUniforms[mNumUniforms*(j+1)+k];
|
204
|
}
|
205
|
|
206
|
mEffects[mNumEffects] = null;
|
207
|
|
208
|
for(int i=0; i<mNumListeners; i++)
|
209
|
EffectMessageSender.newMessage( mListeners.get(i), EffectMessage.EFFECT_REMOVED, removedID, mDistortedEffectsID);
|
210
|
}
|
211
|
|
212
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
213
|
|
214
|
synchronized int removeByName(EffectName name)
|
215
|
{
|
216
|
for(int i=0; i<mNumEffects; i++)
|
217
|
{
|
218
|
if( mEffects[i].getName() == name )
|
219
|
{
|
220
|
mJobs.add(new Job(DETACH,i,true,null));
|
221
|
DistortedMaster.newSlave(this);
|
222
|
mNumEffectsToBe--;
|
223
|
return 1;
|
224
|
}
|
225
|
}
|
226
|
|
227
|
return 0;
|
228
|
}
|
229
|
|
230
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
231
|
|
232
|
synchronized int removeById(long id)
|
233
|
{
|
234
|
for(int i=0; i<mNumEffects; i++)
|
235
|
{
|
236
|
if( mEffects[i].getID() == id )
|
237
|
{
|
238
|
mJobs.add(new Job(DETACH,i,true,null));
|
239
|
DistortedMaster.newSlave(this);
|
240
|
mNumEffectsToBe--;
|
241
|
return 1;
|
242
|
}
|
243
|
}
|
244
|
|
245
|
return 0;
|
246
|
}
|
247
|
|
248
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
249
|
|
250
|
synchronized int removeEffect(Effect effect)
|
251
|
{
|
252
|
for(int i=0; i<mNumEffects; i++)
|
253
|
{
|
254
|
if( mEffects[i]==effect )
|
255
|
{
|
256
|
mJobs.add(new Job(DETACH,i,true,null));
|
257
|
DistortedMaster.newSlave(this);
|
258
|
mNumEffectsToBe--;
|
259
|
return 1;
|
260
|
}
|
261
|
}
|
262
|
|
263
|
return 0;
|
264
|
}
|
265
|
|
266
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
267
|
// we do want to notify Listeners if they called 'abortAll' themselves but don't want to notify
|
268
|
// them if it is the library itself which is releasing resources.
|
269
|
|
270
|
synchronized int abortAll(boolean notify)
|
271
|
{
|
272
|
mJobs.add(new Job(DETALL,0,notify,null));
|
273
|
DistortedMaster.newSlave(this);
|
274
|
mNumEffectsToBe = 0;
|
275
|
return mNumEffects;
|
276
|
}
|
277
|
|
278
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
279
|
|
280
|
boolean add(Effect effect)
|
281
|
{
|
282
|
if( mMax[mIndex]>mNumEffectsToBe )
|
283
|
{
|
284
|
//android.util.Log.e("queue", "scheduling future add of "+effect.getName().name()+" to "+mNumEffectsToBe+" id="+effect.getID());
|
285
|
//android.util.Log.e("queue", "queue id="+mDistortedEffectsID);
|
286
|
|
287
|
mJobs.add(new Job(ATTACH,mNumEffectsToBe,false,effect));
|
288
|
DistortedMaster.newSlave(this);
|
289
|
mNumEffectsToBe++;
|
290
|
return true;
|
291
|
}
|
292
|
|
293
|
return false;
|
294
|
}
|
295
|
|
296
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
297
|
/**
|
298
|
* This is not really part of the public API. Has to be public only because it is a part of the
|
299
|
* DistortedSlave interface, which should really be a class that we extend here instead but
|
300
|
* Java has no multiple inheritance.
|
301
|
*
|
302
|
* @y.exclude
|
303
|
*/
|
304
|
public void doWork()
|
305
|
{
|
306
|
int num = mJobs.size();
|
307
|
Job job;
|
308
|
|
309
|
for(int i=0; i<num; i++)
|
310
|
{
|
311
|
job = mJobs.remove(0);
|
312
|
|
313
|
switch(job.type)
|
314
|
{
|
315
|
case ATTACH: android.util.Log.e("queue", "DistortedEffects ID: "+mDistortedEffectsID+" bank:"+job.index+
|
316
|
" attaching effectID="+job.effect.getID()+" ("+job.effect.getName().name()+")");
|
317
|
|
318
|
mCurrentDuration[job.index] = 0;
|
319
|
mEffects[job.index] = job.effect;
|
320
|
mName[job.index] = job.effect.getName().ordinal();
|
321
|
mNumEffects++;
|
322
|
break;
|
323
|
case DETACH: remove(job.index);
|
324
|
break;
|
325
|
case DETALL: for(int j=0; j<mNumEffects; j++ )
|
326
|
{
|
327
|
if( job.notify )
|
328
|
{
|
329
|
for(int k=0; k<mNumListeners; k++)
|
330
|
EffectMessageSender.newMessage( mListeners.get(k), EffectMessage.EFFECT_REMOVED, mEffects[j].getID(), mDistortedEffectsID);
|
331
|
}
|
332
|
|
333
|
mEffects[j] = null;
|
334
|
}
|
335
|
|
336
|
mNumEffects= 0;
|
337
|
break;
|
338
|
}
|
339
|
}
|
340
|
|
341
|
if( num>0 )
|
342
|
{
|
343
|
regenerateID();
|
344
|
|
345
|
if( mIndex==EffectType.POSTPROCESS.ordinal() )
|
346
|
{
|
347
|
int numNodes = (mNodes==null ? 0: mNodes.size());
|
348
|
|
349
|
for(int i=0; i<numNodes; i++)
|
350
|
{
|
351
|
mNodes.get(i).sort();
|
352
|
}
|
353
|
}
|
354
|
}
|
355
|
}
|
356
|
}
|