Project

General

Profile

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

library / src / main / java / org / distorted / library / main / EffectQueue.java @ 7a5e538a

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
import org.distorted.library.message.EffectMessageSender;
28

    
29
import java.util.ArrayList;
30
import java.util.HashMap;
31

    
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33

    
34
abstract class EffectQueue implements DistortedMaster.Slave
35
  {
36
  private static final int ATTACH = 0;
37
  private static final int DETACH = 1;
38
  private static final int DETALL = 2;
39

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

    
51
  private static int[] mMax = new int[EffectType.LENGTH];
52
  private static long mNextID;
53
  private static HashMap<ArrayList<Long>,Long> mMapID = new HashMap<>(); // maps lists of Effect IDs (longs) to a
54
                                                                         // single long - the queue ID.
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
// Every effect queue has an ID, which should be the same iff two queues hold the same effects.
106
// (this is a speedup: then both queues can be applied once, which seriously speeds up stuff -
107
// especially important in case of postprocessing)
108

    
109
  void regenerateIDandSort()
110
    {
111
    if( mNumEffects>0 )
112
      {
113
      ArrayList<Long> list = new ArrayList<>();
114
      for (int i = 0; i < mNumEffects; i++) list.add(mEffects[i].getID());
115
      Long id = mMapID.get(list);
116

    
117
      if( id!=null )
118
        {
119
        mID = id;
120
        }
121
      else
122
        {
123
        mMapID.put(list,mNextID);
124
        mID = mNextID++;
125
        }
126
      }
127
    else
128
      {
129
      mID = 0;
130
      }
131

    
132
    int numNodes = (mNodes==null ? 0: mNodes.size());
133
    for(int i=0; i<numNodes; i++) mNodes.get(i).sort();
134

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

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148

    
149
  void newNode(DistortedNode node)
150
    {
151
    if( mNodes==null ) mNodes = new ArrayList<>();
152

    
153
    mNodes.add(node);
154
    }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

    
158
  @SuppressWarnings("unused")
159
  int getNumEffects()
160
    {
161
    return mNumEffects;  
162
    }
163

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

    
166
  long getID()
167
    {
168
    return mID;
169
    }
170

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172

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

    
181
    return false;
182
    }
183

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

    
186
  static int getMax(int index)
187
    {
188
    return mMax[index];
189
    }
190

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192

    
193
  void registerForMessages(EffectListener el)
194
    {
195
    if( mListeners==null ) mListeners = new ArrayList<>();
196

    
197
    if( !mListeners.contains(el) )
198
      {
199
      mListeners.add(el);
200
      mNumListeners++;
201
      }
202
    }
203
 
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205

    
206
  void deregisterForMessages(EffectListener el)
207
    {
208
    if( mListeners.remove(el) )
209
      {
210
      mNumListeners--;
211
      }
212
    }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

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

    
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225
// this assumes 0<=effect<mNumEffects
226

    
227
  protected void remove(int effect)
228
    {
229
    mNumEffects--;
230

    
231
    long removedID = mEffects[effect].getID();
232

    
233
    for(int j=effect; j<mNumEffects; j++ )
234
      {
235
      mEffects[j]         = mEffects[j+1];
236
      mCurrentDuration[j] = mCurrentDuration[j+1];
237
      mName[j]            = mName[j+1];
238
      }
239

    
240
    mEffects[mNumEffects] = null;
241

    
242
    for(int i=0; i<mNumListeners; i++)
243
      EffectMessageSender.newMessage( mListeners.get(i), EffectMessage.EFFECT_REMOVED, removedID, mDistortedEffectsID);
244
    }
245

    
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247

    
248
  synchronized int removeByName(EffectName name)
249
    {
250
    int ret = 0;
251

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

    
261
    if( ret>0 )
262
      {
263
      DistortedMaster.newSlave(this);
264
      mNumEffectsToBe-=ret;
265
      }
266

    
267
    return ret;
268
    }
269

    
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271

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

    
285
    return 0;
286
    }
287

    
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289

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

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

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

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

    
327
      mJobs.add(new Job(ATTACH,false,effect));
328
      DistortedMaster.newSlave(this);
329
      mNumEffectsToBe++;
330
      return true;
331
      }
332

    
333
    return false;
334
    }
335

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

    
349
    for(int i=0; i<num; i++)
350
      {
351
      job = mJobs.remove(0);
352

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

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

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

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

    
388
                       mEffects[j] = null;
389
                       }
390

    
391
                     mNumEffects= 0;
392
                     break;
393
        }
394
      }
395

    
396
    if( num>0 && mIndex==EffectType.POSTPROCESS.ordinal() ) regenerateIDandSort();
397
    }
398
  }
(13-13/17)