Project

General

Profile

Download (11.1 KB) Statistics
| Branch: | Revision:

library / src / main / java / org / distorted / library / EffectQueue.java @ 63b6561a

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