Project

General

Profile

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

library / src / main / java / org / distorted / library / main / EffectQueue.java @ a4b182d4

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 DistortedMaster.Slave
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
  void regenerateIDandSort()
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
    int numNodes = (mNodes==null ? 0: mNodes.size());
130
    for(int i=0; i<numNodes; i++) mNodes.get(i).sort();
131

    
132
/*
133
    if( mIndex == EffectType.MATRIX.ordinal() )
134
      android.util.Log.d("queue", "queueM id="+mID);
135
    if( mIndex == EffectType.VERTEX.ordinal() )
136
      android.util.Log.d("queue", "queueV id="+mID);
137
    if( mIndex == EffectType.FRAGMENT.ordinal() )
138
      android.util.Log.d("queue", "queueF id="+mID);
139
    if( mIndex == EffectType.POSTPROCESS.ordinal() )
140
      android.util.Log.d("queue", "queueP id="+mID);
141
*/
142
    }
143

    
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145

    
146
  void newNode(DistortedNode node)
147
    {
148
    if( mNodes==null ) mNodes = new ArrayList<>();
149

    
150
    mNodes.add(node);
151
    }
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154

    
155
  @SuppressWarnings("unused")
156
  int getNumEffects()
157
    {
158
    return mNumEffects;  
159
    }
160

    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162

    
163
  long getID()
164
    {
165
    return mID;
166
    }
167

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

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

    
178
    return false;
179
    }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182

    
183
  static int getMax(int index)
184
    {
185
    return mMax[index];
186
    }
187

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189

    
190
  void registerForMessages(EffectListener el)
191
    {
192
    if( mListeners==null ) mListeners = new ArrayList<>();
193

    
194
    if( !mListeners.contains(el) )
195
      {
196
      mListeners.add(el);
197
      mNumListeners++;
198
      }
199
    }
200
 
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

    
203
  void deregisterForMessages(EffectListener el)
204
    {
205
    if( mListeners.remove(el) )
206
      {
207
      mNumListeners--;
208
      }
209
    }
210

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212

    
213
  static void onDestroy()
214
    {
215
    mNextID = 1;
216
    mMapID.clear();
217
    EffectType.reset(mMax);
218
    mCreated = false;  
219
    }
220

    
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222
// this assumes 0<=effect<mNumEffects
223

    
224
  protected void remove(int effect)
225
    {
226
    mNumEffects--;
227

    
228
    long removedID = mEffects[effect].getID();
229

    
230
    for(int j=effect; j<mNumEffects; j++ )
231
      {
232
      mEffects[j]         = mEffects[j+1];
233
      mCurrentDuration[j] = mCurrentDuration[j+1];
234
      mName[j]            = mName[j+1];
235
      }
236

    
237
    mEffects[mNumEffects] = null;
238

    
239
    for(int i=0; i<mNumListeners; i++)
240
      EffectMessageSender.newMessage( mListeners.get(i), EffectMessage.EFFECT_REMOVED, removedID, mDistortedEffectsID);
241
    }
242

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

    
245
  synchronized int removeByName(EffectName name)
246
    {
247
    int ret = 0;
248

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

    
258
    if( ret>0 )
259
      {
260
      DistortedMaster.newSlave(this);
261
      mNumEffectsToBe-=ret;
262
      }
263

    
264
    return ret;
265
    }
266

    
267
///////////////////////////////////////////////////////////////////////////////////////////////////
268

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

    
282
    return 0;
283
    }
284

    
285
///////////////////////////////////////////////////////////////////////////////////////////////////
286

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

    
303
///////////////////////////////////////////////////////////////////////////////////////////////////
304
// we do want to notify Listeners if they called 'abortAll' themselves but don't want to notify
305
// them if it is the library itself which is releasing resources.
306

    
307
  synchronized int abortAll(boolean notify)
308
    {
309
    mJobs.add(new Job(DETALL,notify,null));
310
    DistortedMaster.newSlave(this);
311
    mNumEffectsToBe = 0;
312
    return mNumEffects;
313
    }
314

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

    
324
      mJobs.add(new Job(ATTACH,false,effect));
325
      DistortedMaster.newSlave(this);
326
      mNumEffectsToBe++;
327
      return true;
328
      }
329

    
330
    return false;
331
    }
332

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

    
346
    for(int i=0; i<num; i++)
347
      {
348
      job = mJobs.remove(0);
349

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

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

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

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

    
385
                       mEffects[j] = null;
386
                       }
387

    
388
                     mNumEffects= 0;
389
                     break;
390
        }
391
      }
392

    
393
    if( num>0 && mIndex==EffectType.POSTPROCESS.ordinal() ) regenerateIDandSort();
394
    }
395
  }
(15-15/22)