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;
|
21
|
|
22
|
import org.distorted.library.message.EffectListener;
|
23
|
import org.distorted.library.message.EffectMessage;
|
24
|
import org.distorted.library.type.Dynamic;
|
25
|
|
26
|
import java.util.Vector;
|
27
|
|
28
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
29
|
|
30
|
abstract class EffectQueue
|
31
|
{
|
32
|
protected byte mNumEffects; // number of effects at the moment
|
33
|
protected long mTotalEffects; // total number of effects ever created
|
34
|
protected int[] mName;
|
35
|
protected float[] mUniforms;
|
36
|
protected float[] mCache;
|
37
|
protected Dynamic[][] mInter;
|
38
|
protected long[] mCurrentDuration;
|
39
|
protected byte[] mFreeIndexes;
|
40
|
protected byte[] mIDIndex;
|
41
|
protected long[] mID;
|
42
|
protected long mTime=0;
|
43
|
protected static int[] mMax = new int[EffectTypes.LENGTH];
|
44
|
protected int mMaxIndex;
|
45
|
protected Vector<EffectListener> mListeners =null;
|
46
|
protected int mNumListeners=0; // ==mListeners.length(), but we only create mListeners if the first one gets added
|
47
|
protected long mObjectID;
|
48
|
|
49
|
private static boolean mCreated;
|
50
|
|
51
|
static
|
52
|
{
|
53
|
onDestroy();
|
54
|
}
|
55
|
|
56
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
57
|
|
58
|
EffectQueue(long id, int numUniforms, int numCache, int index)
|
59
|
{
|
60
|
mNumEffects = 0;
|
61
|
mTotalEffects = 0;
|
62
|
mMaxIndex = index;
|
63
|
mObjectID = id;
|
64
|
|
65
|
if( mMax[mMaxIndex]>0 )
|
66
|
{
|
67
|
mName = new int[mMax[mMaxIndex]];
|
68
|
mUniforms = new float[numUniforms*mMax[mMaxIndex]];
|
69
|
mInter = new Dynamic[3][mMax[mMaxIndex]];
|
70
|
mCurrentDuration = new long[mMax[mMaxIndex]];
|
71
|
mID = new long[mMax[mMaxIndex]];
|
72
|
mIDIndex = new byte[mMax[mMaxIndex]];
|
73
|
mFreeIndexes = new byte[mMax[mMaxIndex]];
|
74
|
|
75
|
for(byte i=0; i<mMax[mMaxIndex]; i++) mFreeIndexes[i] = i;
|
76
|
|
77
|
if( numCache>0 )
|
78
|
{
|
79
|
mCache = new float[numCache*mMax[mMaxIndex]];
|
80
|
}
|
81
|
}
|
82
|
|
83
|
mCreated = true;
|
84
|
}
|
85
|
|
86
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
87
|
|
88
|
@SuppressWarnings("unused")
|
89
|
int getNumEffects()
|
90
|
{
|
91
|
return mNumEffects;
|
92
|
}
|
93
|
|
94
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
95
|
// Only max Byte.MAX_VALUE concurrent effects per DistortedObject.
|
96
|
// If you want more, change type of the mNumEffects, mIDIndex and mFreeIndexes variables to shorts.
|
97
|
|
98
|
static boolean setMax(int index, int m)
|
99
|
{
|
100
|
if( (!mCreated && !Distorted.isInitialized()) || m<=mMax[index] )
|
101
|
{
|
102
|
if( m<0 ) m = 0;
|
103
|
else if( m>Byte.MAX_VALUE ) m = Byte.MAX_VALUE;
|
104
|
|
105
|
mMax[index] = m;
|
106
|
return true;
|
107
|
}
|
108
|
|
109
|
return false;
|
110
|
}
|
111
|
|
112
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
113
|
|
114
|
static int getMax(int index)
|
115
|
{
|
116
|
return mMax[index];
|
117
|
}
|
118
|
|
119
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
120
|
|
121
|
void addListener(EffectListener el)
|
122
|
{
|
123
|
if( mListeners==null ) mListeners = new Vector<>(2,2);
|
124
|
|
125
|
mListeners.add(el);
|
126
|
mNumListeners++;
|
127
|
}
|
128
|
|
129
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
130
|
|
131
|
void removeListener(EffectListener el)
|
132
|
{
|
133
|
if( mNumListeners>0 )
|
134
|
{
|
135
|
mListeners.remove(el);
|
136
|
mNumListeners--;
|
137
|
}
|
138
|
}
|
139
|
|
140
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
141
|
|
142
|
static void onDestroy()
|
143
|
{
|
144
|
EffectTypes.reset(mMax);
|
145
|
mCreated = false;
|
146
|
}
|
147
|
|
148
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
149
|
|
150
|
synchronized int removeByID(long id)
|
151
|
{
|
152
|
int i = getEffectIndex(id);
|
153
|
|
154
|
if( i>=0 )
|
155
|
{
|
156
|
remove(i);
|
157
|
return 1;
|
158
|
}
|
159
|
|
160
|
return 0;
|
161
|
}
|
162
|
|
163
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
164
|
|
165
|
synchronized int removeByType(EffectNames effect)
|
166
|
{
|
167
|
int ret = 0;
|
168
|
int ord = effect.ordinal();
|
169
|
|
170
|
for(int i=0; i<mNumEffects; i++)
|
171
|
{
|
172
|
if( mName[i]==ord )
|
173
|
{
|
174
|
remove(i);
|
175
|
i--;
|
176
|
ret++;
|
177
|
}
|
178
|
}
|
179
|
|
180
|
return ret;
|
181
|
}
|
182
|
|
183
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
184
|
|
185
|
private synchronized int getEffectIndex(long id)
|
186
|
{
|
187
|
int index = mIDIndex[(int)(id%mMax[mMaxIndex])];
|
188
|
return (index<mNumEffects && mID[index]==id ? index : -1);
|
189
|
}
|
190
|
|
191
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
192
|
// we do want to notify Listeners if they called 'abortAll' themselves but don't want to notify
|
193
|
// them if it is the library itself which is releasing resources.
|
194
|
|
195
|
synchronized int abortAll(boolean notify)
|
196
|
{
|
197
|
int ret = mNumEffects;
|
198
|
long removedID;
|
199
|
int removedName;
|
200
|
|
201
|
for(int i=0; i<ret; i++ )
|
202
|
{
|
203
|
mInter[0][i] = null;
|
204
|
mInter[1][i] = null;
|
205
|
mInter[2][i] = null;
|
206
|
|
207
|
if( notify )
|
208
|
{
|
209
|
removedID = mID[i];
|
210
|
removedName= mName[i];
|
211
|
|
212
|
for(int j=0; j<mNumListeners; j++)
|
213
|
EffectMessageSender.newMessage( mListeners.elementAt(j),
|
214
|
EffectMessage.EFFECT_REMOVED,
|
215
|
(removedID<<EffectTypes.LENGTH)+EffectNames.getType(removedName).type,
|
216
|
removedName,
|
217
|
mObjectID,
|
218
|
null);
|
219
|
}
|
220
|
}
|
221
|
|
222
|
mNumEffects= 0;
|
223
|
|
224
|
return ret;
|
225
|
}
|
226
|
|
227
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
228
|
// this assumes 0<=effect<mNumEffects
|
229
|
|
230
|
protected void remove(int effect)
|
231
|
{
|
232
|
mNumEffects--;
|
233
|
|
234
|
byte removedIndex = (byte)(mID[effect]%mMax[mMaxIndex]);
|
235
|
byte removedPosition = mIDIndex[removedIndex];
|
236
|
mFreeIndexes[mNumEffects] = removedIndex;
|
237
|
|
238
|
long removedID = mID[effect];
|
239
|
int removedName= mName[effect];
|
240
|
|
241
|
for(int j=0; j<mMax[mMaxIndex]; j++)
|
242
|
{
|
243
|
if( mIDIndex[j] > removedPosition ) mIDIndex[j]--;
|
244
|
}
|
245
|
|
246
|
for(int j=effect; j<mNumEffects; j++ )
|
247
|
{
|
248
|
mName[j] = mName[j+1];
|
249
|
mInter[0][j] = mInter[0][j+1];
|
250
|
mInter[1][j] = mInter[1][j+1];
|
251
|
mInter[2][j] = mInter[2][j+1];
|
252
|
mCurrentDuration[j] = mCurrentDuration[j+1];
|
253
|
mID[j] = mID[j+1];
|
254
|
|
255
|
moveEffect(j);
|
256
|
}
|
257
|
|
258
|
mInter[0][mNumEffects] = null;
|
259
|
mInter[1][mNumEffects] = null;
|
260
|
mInter[2][mNumEffects] = null;
|
261
|
|
262
|
for(int i=0; i<mNumListeners; i++)
|
263
|
EffectMessageSender.newMessage( mListeners.elementAt(i),
|
264
|
EffectMessage.EFFECT_REMOVED,
|
265
|
(removedID<<EffectTypes.LENGTH)+EffectNames.getType(removedName).type,
|
266
|
removedName,
|
267
|
mObjectID,
|
268
|
null);
|
269
|
}
|
270
|
|
271
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
272
|
|
273
|
protected long addBase(EffectNames name)
|
274
|
{
|
275
|
mName[mNumEffects] = name.ordinal();
|
276
|
mCurrentDuration[mNumEffects] = 0;
|
277
|
|
278
|
int index = mFreeIndexes[mNumEffects];
|
279
|
long id = mTotalEffects*mMax[mMaxIndex] + index;
|
280
|
mID[mNumEffects] = id;
|
281
|
mIDIndex[index] = mNumEffects;
|
282
|
|
283
|
mNumEffects++;
|
284
|
mTotalEffects++;
|
285
|
|
286
|
return (id<<EffectTypes.LENGTH)+name.getType().type;
|
287
|
}
|
288
|
|
289
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
290
|
// used only for debugging
|
291
|
|
292
|
@SuppressWarnings("unused")
|
293
|
protected String printEffects(int max)
|
294
|
{
|
295
|
long[] indexes = new long[mMax[mMaxIndex]];
|
296
|
|
297
|
for(int g=0; g<mMax[mMaxIndex]; g++)
|
298
|
{
|
299
|
indexes[g] = -1;
|
300
|
}
|
301
|
|
302
|
String ret="(";
|
303
|
int f;
|
304
|
|
305
|
for(int g=0; g<max; g++)
|
306
|
{
|
307
|
f = getEffectIndex(g);
|
308
|
if( f>=0 ) indexes[f] = g;
|
309
|
}
|
310
|
|
311
|
for(int g=0; g<mMax[mMaxIndex]; g++)
|
312
|
{
|
313
|
ret += (g>0 ? ",":"")+(indexes[g]>=0 ? indexes[g] : " ");
|
314
|
}
|
315
|
|
316
|
ret += ")";
|
317
|
|
318
|
return ret;
|
319
|
}
|
320
|
|
321
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
322
|
// Only used for debugging
|
323
|
|
324
|
protected boolean printByID(long id)
|
325
|
{
|
326
|
int index = getEffectIndex(id);
|
327
|
|
328
|
if( index>=0 )
|
329
|
{
|
330
|
boolean inter0 = mInter[0][index]==null;
|
331
|
boolean inter1 = mInter[1][index]==null;
|
332
|
boolean inter2 = mInter[2][index]==null;
|
333
|
|
334
|
android.util.Log.e("EffectQueue", "numEffects="+mNumEffects+" effect id="+id+" index="+index+
|
335
|
" duration="+mCurrentDuration[index]+" inter[0] null="+inter0+" inter[1] null="+inter1+" inter[2] null="+inter2);
|
336
|
|
337
|
if( !inter0 ) android.util.Log.e("EffectQueue","inter[0]: "+mInter[0][index].print());
|
338
|
if( !inter1 ) android.util.Log.e("EffectQueue","inter[1]: "+mInter[1][index].print());
|
339
|
if( !inter2 ) android.util.Log.e("EffectQueue","inter[2]: "+mInter[2][index].print());
|
340
|
|
341
|
return true;
|
342
|
}
|
343
|
|
344
|
android.util.Log.e("EffectQueue", "effect id="+id+" not found");
|
345
|
|
346
|
return false;
|
347
|
}
|
348
|
|
349
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
350
|
|
351
|
abstract void moveEffect(int index);
|
352
|
}
|
353
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|