Project

General

Profile

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

library / src / main / java / org / distorted / library / main / EffectQueue.java @ 6b0b4f60

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;      // 'ToBe' will be more than mNumEffects if doWork() hasn't
39
  protected int mNumEffectsToBe;  // added them yet (or less if it hasn't removed some yet)
40
  protected float[] mUniforms;
41
  protected long[] mCurrentDuration;
42
  protected Effect[] mEffects;
43
  protected int[] mName;
44
  protected long mTime=0;
45
  protected static int[] mMax = new int[EffectType.LENGTH];
46
  protected ArrayList<EffectListener> mListeners =null;
47
  protected int mNumListeners=0;  // ==mListeners.length(), but we only create mListeners if the first one gets added
48
  protected long mDistortedEffectsID;
49

    
50
  private ArrayList<DistortedNode> mNodes = null;
51
  private long mID;
52
  private static boolean mCreated;
53
  private int mIndex;
54

    
55
  private class Job
56
    {
57
    int type;
58
    boolean notify;
59
    Effect effect;
60

    
61
    Job(int t, boolean n, Effect e)
62
      {
63
      type  = t;
64
      notify= n;
65
      effect= e;
66
      }
67
    }
68

    
69
  private ArrayList<Job> mJobs = new ArrayList<>();
70

    
71
  static
72
    {
73
    onDestroy();
74
    }
75
  
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77
   
78
  EffectQueue(long id, int numUniforms, int index)
79
    {
80
    mID                 = 0;
81
    mNumEffects         = 0;
82
    mNumEffectsToBe     = 0;
83
    mDistortedEffectsID = id;
84
    mIndex              = index;
85

    
86
    int max = mMax[mIndex];
87

    
88
    if( max>0 )
89
      {
90
      mUniforms        = new float[numUniforms*max];
91
      mCurrentDuration = new long[max];
92
      mEffects         = new Effect[max];
93
      mName            = new int[max];
94
      }
95
   
96
    mCreated = true;  
97
    }
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100
// TODO: introduce a Map to return an ID dependant on all Effects in the Queue.
101
// For now this is good enough.
102

    
103
  private void regenerateID()
104
    {
105
    mID = mNumEffects>0 ? mEffects[0].getID() : 0;
106
    }
107

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109

    
110
  void newNode(DistortedNode node)
111
    {
112
    if( mNodes==null ) mNodes = new ArrayList<>();
113

    
114
    mNodes.add(node);
115
    }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

    
119
  @SuppressWarnings("unused")
120
  int getNumEffects()
121
    {
122
    return mNumEffects;  
123
    }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

    
127
  long getID()
128
    {
129
    return mID;
130
    }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133

    
134
  static boolean setMax(int index, int m)
135
    {
136
    if( (!mCreated && !Distorted.isInitialized()) || m<=mMax[index] )
137
      {
138
      mMax[index] = m<0 ? 0:m;
139
      return true;
140
      }
141

    
142
    return false;
143
    }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146

    
147
  static int getMax(int index)
148
    {
149
    return mMax[index];
150
    }
151

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153

    
154
  void registerForMessages(EffectListener el)
155
    {
156
    if( mListeners==null ) mListeners = new ArrayList<>();
157

    
158
    if( !mListeners.contains(el) )
159
      {
160
      mListeners.add(el);
161
      mNumListeners++;
162
      }
163
    }
164
 
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166

    
167
  void deregisterForMessages(EffectListener el)
168
    {
169
    if( mListeners.remove(el) )
170
      {
171
      mNumListeners--;
172
      }
173
    }
174

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176

    
177
  static void onDestroy()
178
    {
179
    EffectType.reset(mMax);
180
    mCreated = false;  
181
    }
182

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184
// this assumes 0<=effect<mNumEffects
185

    
186
  protected void remove(int effect)
187
    {
188
    mNumEffects--;
189

    
190
    long removedID = mEffects[effect].getID();
191

    
192
    for(int j=effect; j<mNumEffects; j++ )
193
      {
194
      mEffects[j]         = mEffects[j+1];
195
      mCurrentDuration[j] = mCurrentDuration[j+1];
196
      mName[j]            = mName[j+1];
197
      }
198

    
199
    mEffects[mNumEffects] = null;
200

    
201
    for(int i=0; i<mNumListeners; i++)
202
      EffectMessageSender.newMessage( mListeners.get(i), EffectMessage.EFFECT_REMOVED, removedID, mDistortedEffectsID);
203
    }
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

    
207
  synchronized int removeByName(EffectName name)
208
    {
209
    int ret = 0;
210

    
211
    for(int i=0; i<mNumEffects; i++)
212
      {
213
      if( mEffects[i].getName() == name )
214
        {
215
        mJobs.add(new Job(DETACH,true,mEffects[i]));
216
        ret++;
217
        }
218
      }
219

    
220
    if( ret>0 )
221
      {
222
      DistortedMaster.newSlave(this);
223
      mNumEffectsToBe-=ret;
224
      }
225

    
226
    return ret;
227
    }
228

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230

    
231
  synchronized int removeById(long id)
232
    {
233
    for(int i=0; i<mNumEffects; i++)
234
      {
235
      if( mEffects[i].getID() == id )
236
        {
237
        mJobs.add(new Job(DETACH,true,mEffects[i]));
238
        DistortedMaster.newSlave(this);
239
        mNumEffectsToBe--;
240
        return 1;
241
        }
242
      }
243

    
244
    return 0;
245
    }
246

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248

    
249
  synchronized int removeEffect(Effect effect)
250
    {
251
    for(int i=0; i<mNumEffects; i++)
252
      {
253
      if( mEffects[i]==effect )
254
        {
255
        mJobs.add(new Job(DETACH,true,mEffects[i]));
256
        DistortedMaster.newSlave(this);
257
        mNumEffectsToBe--;
258
        return 1;
259
        }
260
      }
261
   
262
    return 0;
263
    }
264

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266
// we do want to notify Listeners if they called 'abortAll' themselves but don't want to notify
267
// them if it is the library itself which is releasing resources.
268

    
269
  synchronized int abortAll(boolean notify)
270
    {
271
    mJobs.add(new Job(DETALL,notify,null));
272
    DistortedMaster.newSlave(this);
273
    mNumEffectsToBe = 0;
274
    return mNumEffects;
275
    }
276

    
277
///////////////////////////////////////////////////////////////////////////////////////////////////
278
  
279
  boolean add(Effect effect)
280
    {
281
    if( mMax[mIndex]>mNumEffectsToBe )
282
      {
283
      //android.util.Log.e("queue", "scheduling future add of "+effect.getName().name()+" to "+mNumEffectsToBe+" id="+effect.getID());
284
      //android.util.Log.e("queue", "queue id="+mDistortedEffectsID);
285

    
286
      mJobs.add(new Job(ATTACH,false,effect));
287
      DistortedMaster.newSlave(this);
288
      mNumEffectsToBe++;
289
      return true;
290
      }
291

    
292
    return false;
293
    }
294

    
295
///////////////////////////////////////////////////////////////////////////////////////////////////
296
/**
297
 * This is not really part of the public API. Has to be public only because it is a part of the
298
 * DistortedSlave interface, which should really be a class that we extend here instead but
299
 * Java has no multiple inheritance.
300
 *
301
 * @y.exclude
302
 */
303
  public void doWork()
304
    {
305
    int num = mJobs.size();
306
    Job job;
307

    
308
    for(int i=0; i<num; i++)
309
      {
310
      job = mJobs.remove(0);
311

    
312
      switch(job.type)
313
        {
314
        case ATTACH: //android.util.Log.e("queue", "DisEffects ID: "+mDistortedEffectsID+" bank:"+mNumEffects+
315
                     //                   " attaching effectID="+job.effect.getID()+" ("+job.effect.getName().name()+")");
316

    
317
                     mCurrentDuration[mNumEffects] = 0;
318
                     mEffects[mNumEffects] = job.effect;
319
                     mName[mNumEffects] = job.effect.getName().ordinal();
320
                     mNumEffects++;
321
                     //android.util.Log.d("queue", "DisEffects ID: "+mDistortedEffectsID+
322
                     //        " success attaching, num to be:"+mNumEffectsToBe+" num:"+mNumEffects);
323

    
324
                     break;
325
        case DETACH: //android.util.Log.e("queue", "DisEffects ID: "+mDistortedEffectsID+" detaching effect "+
326
                     //                             job.effect.getID());
327
                     for(int j=0; j<mNumEffects; j++)
328
                       {
329
                       if (mEffects[j] == job.effect)
330
                         {
331
                         remove(j);
332
                         //android.util.Log.d("queue", "DisEffects ID: "+mDistortedEffectsID+
333
                         //    " success detaching, num to be:"+mNumEffectsToBe+" num:"+mNumEffects);
334

    
335
                         break;
336
                         }
337
                       }
338
                     break;
339
        case DETALL: for(int j=0; j<mNumEffects; j++ )
340
                       {
341
                       if( job.notify )
342
                         {
343
                         for(int k=0; k<mNumListeners; k++)
344
                           EffectMessageSender.newMessage( mListeners.get(k), EffectMessage.EFFECT_REMOVED, mEffects[j].getID(), mDistortedEffectsID);
345
                         }
346

    
347
                       mEffects[j] = null;
348
                       }
349

    
350
                     mNumEffects= 0;
351
                     break;
352
        }
353
      }
354

    
355
    if( num>0 )
356
      {
357
      regenerateID();
358

    
359
      if( mIndex==EffectType.POSTPROCESS.ordinal() )
360
        {
361
        int numNodes = (mNodes==null ? 0: mNodes.size());
362

    
363
        for(int i=0; i<numNodes; i++)
364
          {
365
          mNodes.get(i).sort();
366
          }
367
        }
368
      }
369
    }
370
  }
(16-16/23)