Project

General

Profile

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

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

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

    
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

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

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

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

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

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

    
92
    int max = mMax[mIndex];
93

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

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106
// Every effect queue has an ID, which should be the same iff two queues hold the same effects.
107
// (this is a speedup: then both queues can be applied once, which seriously speeds up stuff -
108
// especially important in case of postprocessing)
109

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

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

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

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

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

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

    
154
    mNodes.add(node);
155
    }
156

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

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

    
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166

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

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

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

    
182
    return false;
183
    }
184

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186

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

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193

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

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

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

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

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

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

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

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

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

    
241
    mEffects[mNumEffects] = null;
242

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

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

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

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

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

    
268
    return ret;
269
    }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

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

    
286
    return 0;
287
    }
288

    
289
///////////////////////////////////////////////////////////////////////////////////////////////////
290

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

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

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

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

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

    
334
    return false;
335
    }
336

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

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

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

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

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

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

    
389
                       mEffects[j] = null;
390
                       }
391

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

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