Project

General

Profile

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

library / src / main / java / org / distorted / library / main / EffectQueue.java @ 3417ab4e

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
import java.util.HashMap;
30

    
31
///////////////////////////////////////////////////////////////////////////////////////////////////
32

    
33
abstract class EffectQueue implements DistortedSlave
34
  {
35
  private static final int ATTACH = 0;
36
  private static final int DETACH = 1;
37
  private static final int DETALL = 2;
38

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

    
51
  private static long mNextID;
52
  private static HashMap<ArrayList<Long>,Long> mMapID = new HashMap<>(); // maps lists of Effect IDs (longs) to a
53
                                                                         // single long - the queue ID.
54

    
55
  private ArrayList<DistortedNode> mNodes = null;
56
  private long mID;
57
  private static boolean mCreated;
58
  private int mIndex;
59

    
60
  private class Job
61
    {
62
    int type;
63
    boolean notify;
64
    Effect effect;
65

    
66
    Job(int t, boolean n, Effect e)
67
      {
68
      type  = t;
69
      notify= n;
70
      effect= e;
71
      }
72
    }
73

    
74
  private ArrayList<Job> mJobs = new ArrayList<>();
75

    
76
  static
77
    {
78
    onDestroy();
79
    }
80
  
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82
   
83
  EffectQueue(long id, int numUniforms, int index)
84
    {
85
    mID                 = 0;
86
    mNumEffects         = 0;
87
    mNumEffectsToBe     = 0;
88
    mDistortedEffectsID = id;
89
    mIndex              = index;
90

    
91
    int max = mMax[mIndex];
92

    
93
    if( max>0 )
94
      {
95
      mUniforms        = new float[numUniforms*max];
96
      mCurrentDuration = new long[max];
97
      mEffects         = new Effect[max];
98
      mName            = new int[max];
99
      }
100
   
101
    mCreated = true;  
102
    }
103

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105

    
106
  private void regenerateID()
107
    {
108
    if( mNumEffects>0 )
109
      {
110
      ArrayList<Long> list = new ArrayList<>();
111
      for (int i = 0; i < mNumEffects; i++) list.add(mEffects[i].getID());
112
      Long id = mMapID.get(list);
113

    
114
      if( id!=null )
115
        {
116
        mID = id;
117
        }
118
      else
119
        {
120
        mMapID.put(list,mNextID);
121
        mID = mNextID++;
122
        }
123
      }
124
    else
125
      {
126
      mID = 0;
127
      }
128
/*
129
    if( mIndex == EffectType.MATRIX.ordinal() )
130
      android.util.Log.d("queue", "queueM id="+mID);
131
    if( mIndex == EffectType.VERTEX.ordinal() )
132
      android.util.Log.d("queue", "queueV id="+mID);
133
    if( mIndex == EffectType.FRAGMENT.ordinal() )
134
      android.util.Log.d("queue", "queueF id="+mID);
135
    if( mIndex == EffectType.POSTPROCESS.ordinal() )
136
      android.util.Log.d("queue", "queueP id="+mID);
137
*/
138
    }
139

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141

    
142
  void newNode(DistortedNode node)
143
    {
144
    if( mNodes==null ) mNodes = new ArrayList<>();
145

    
146
    mNodes.add(node);
147
    }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150

    
151
  @SuppressWarnings("unused")
152
  int getNumEffects()
153
    {
154
    return mNumEffects;  
155
    }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

    
159
  long getID()
160
    {
161
    return mID;
162
    }
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

    
166
  static boolean setMax(int index, int m)
167
    {
168
    if( (!mCreated && !Distorted.isInitialized()) || m<=mMax[index] )
169
      {
170
      mMax[index] = m<0 ? 0:m;
171
      return true;
172
      }
173

    
174
    return false;
175
    }
176

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178

    
179
  static int getMax(int index)
180
    {
181
    return mMax[index];
182
    }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

    
186
  void registerForMessages(EffectListener el)
187
    {
188
    if( mListeners==null ) mListeners = new ArrayList<>();
189

    
190
    if( !mListeners.contains(el) )
191
      {
192
      mListeners.add(el);
193
      mNumListeners++;
194
      }
195
    }
196
 
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198

    
199
  void deregisterForMessages(EffectListener el)
200
    {
201
    if( mListeners.remove(el) )
202
      {
203
      mNumListeners--;
204
      }
205
    }
206

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

    
209
  static void onDestroy()
210
    {
211
    mNextID = 1;
212
    mMapID.clear();
213
    EffectType.reset(mMax);
214
    mCreated = false;  
215
    }
216

    
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218
// this assumes 0<=effect<mNumEffects
219

    
220
  protected void remove(int effect)
221
    {
222
    mNumEffects--;
223

    
224
    long removedID = mEffects[effect].getID();
225

    
226
    for(int j=effect; j<mNumEffects; j++ )
227
      {
228
      mEffects[j]         = mEffects[j+1];
229
      mCurrentDuration[j] = mCurrentDuration[j+1];
230
      mName[j]            = mName[j+1];
231
      }
232

    
233
    mEffects[mNumEffects] = null;
234

    
235
    for(int i=0; i<mNumListeners; i++)
236
      EffectMessageSender.newMessage( mListeners.get(i), EffectMessage.EFFECT_REMOVED, removedID, mDistortedEffectsID);
237
    }
238

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240

    
241
  synchronized int removeByName(EffectName name)
242
    {
243
    int ret = 0;
244

    
245
    for(int i=0; i<mNumEffects; i++)
246
      {
247
      if( mEffects[i].getName() == name )
248
        {
249
        mJobs.add(new Job(DETACH,true,mEffects[i]));
250
        ret++;
251
        }
252
      }
253

    
254
    if( ret>0 )
255
      {
256
      DistortedMaster.newSlave(this);
257
      mNumEffectsToBe-=ret;
258
      }
259

    
260
    return ret;
261
    }
262

    
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264

    
265
  synchronized int removeById(long id)
266
    {
267
    for(int i=0; i<mNumEffects; i++)
268
      {
269
      if( mEffects[i].getID() == id )
270
        {
271
        mJobs.add(new Job(DETACH,true,mEffects[i]));
272
        DistortedMaster.newSlave(this);
273
        mNumEffectsToBe--;
274
        return 1;
275
        }
276
      }
277

    
278
    return 0;
279
    }
280

    
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282

    
283
  synchronized int removeEffect(Effect effect)
284
    {
285
    for(int i=0; i<mNumEffects; i++)
286
      {
287
      if( mEffects[i]==effect )
288
        {
289
        mJobs.add(new Job(DETACH,true,mEffects[i]));
290
        DistortedMaster.newSlave(this);
291
        mNumEffectsToBe--;
292
        return 1;
293
        }
294
      }
295
   
296
    return 0;
297
    }
298

    
299
///////////////////////////////////////////////////////////////////////////////////////////////////
300
// we do want to notify Listeners if they called 'abortAll' themselves but don't want to notify
301
// them if it is the library itself which is releasing resources.
302

    
303
  synchronized int abortAll(boolean notify)
304
    {
305
    mJobs.add(new Job(DETALL,notify,null));
306
    DistortedMaster.newSlave(this);
307
    mNumEffectsToBe = 0;
308
    return mNumEffects;
309
    }
310

    
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312
  
313
  boolean add(Effect effect)
314
    {
315
    if( mMax[mIndex]>mNumEffectsToBe )
316
      {
317
      //android.util.Log.e("queue", "scheduling future add of "+effect.getName().name()+" to "+mNumEffectsToBe+" id="+effect.getID());
318
      //android.util.Log.e("queue", "queue id="+mDistortedEffectsID);
319

    
320
      mJobs.add(new Job(ATTACH,false,effect));
321
      DistortedMaster.newSlave(this);
322
      mNumEffectsToBe++;
323
      return true;
324
      }
325

    
326
    return false;
327
    }
328

    
329
///////////////////////////////////////////////////////////////////////////////////////////////////
330
/**
331
 * This is not really part of the public API. Has to be public only because it is a part of the
332
 * DistortedSlave interface, which should really be a class that we extend here instead but
333
 * Java has no multiple inheritance.
334
 *
335
 * @y.exclude
336
 */
337
  public void doWork()
338
    {
339
    int num = mJobs.size();
340
    Job job;
341

    
342
    for(int i=0; i<num; i++)
343
      {
344
      job = mJobs.remove(0);
345

    
346
      switch(job.type)
347
        {
348
        case ATTACH: //android.util.Log.e("queue", "DisEffects ID: "+mDistortedEffectsID+" bank:"+mNumEffects+
349
                     //                   " attaching effectID="+job.effect.getID()+" ("+job.effect.getName().name()+")");
350

    
351
                     mCurrentDuration[mNumEffects] = 0;
352
                     mEffects[mNumEffects] = job.effect;
353
                     mName[mNumEffects] = job.effect.getName().ordinal();
354
                     mNumEffects++;
355
                     //android.util.Log.d("queue", "DisEffects ID: "+mDistortedEffectsID+
356
                     //        " success attaching, num to be:"+mNumEffectsToBe+" num:"+mNumEffects);
357

    
358
                     break;
359
        case DETACH: //android.util.Log.e("queue", "DisEffects ID: "+mDistortedEffectsID+" detaching effect "+
360
                     //                             job.effect.getID());
361
                     for(int j=0; j<mNumEffects; j++)
362
                       {
363
                       if (mEffects[j] == job.effect)
364
                         {
365
                         remove(j);
366
                         //android.util.Log.d("queue", "DisEffects ID: "+mDistortedEffectsID+
367
                         //    " success detaching, num to be:"+mNumEffectsToBe+" num:"+mNumEffects);
368

    
369
                         break;
370
                         }
371
                       }
372
                     break;
373
        case DETALL: for(int j=0; j<mNumEffects; j++ )
374
                       {
375
                       if( job.notify )
376
                         {
377
                         for(int k=0; k<mNumListeners; k++)
378
                           EffectMessageSender.newMessage( mListeners.get(k), EffectMessage.EFFECT_REMOVED, mEffects[j].getID(), mDistortedEffectsID);
379
                         }
380

    
381
                       mEffects[j] = null;
382
                       }
383

    
384
                     mNumEffects= 0;
385
                     break;
386
        }
387
      }
388

    
389
    if( num>0 && mIndex==EffectType.POSTPROCESS.ordinal() )
390
      {
391
      regenerateID();
392
      int numNodes = (mNodes==null ? 0: mNodes.size());
393
      for(int i=0; i<numNodes; i++) mNodes.get(i).sort();
394
      }
395
    }
396
  }
(16-16/23)