Project

General

Profile

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

library / src / main / java / org / distorted / library / main / EffectQueue.java @ 11845a9e

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 CREATE = 0;
37
  private static final int ATTACH = 1;
38
  private static final int DETACH = 2;
39
  private static final int DETALL = 3;
40

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

    
52
  private static int[] mMax = new int[EffectType.LENGTH];
53
  private static long mNextID;
54
  private static HashMap<ArrayList<Long>,Long> mMapID = new HashMap<>(); // maps lists of Effect IDs (longs) to a
55
                                                                         // single long - the queue ID.
56
  private ArrayList<DistortedNode> mNodes = null;
57
  private long mID;
58
  private int mIndex;
59
  private boolean mCreated;
60

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

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

    
77
  private ArrayList<Job> mJobs = new ArrayList<>();
78

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

    
95
    mJobs.add(new Job(CREATE,numUniforms,false,null));  // create the stuff that depends on max number
96
    DistortedMaster.newSlave(this);                     // of uniforms later, on first render.
97
    }
98

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

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

    
112
      if( id!=null )
113
        {
114
        mID = id;
115
        }
116
      else
117
        {
118
        mMapID.put(list,mNextID);
119
        mID = mNextID++;
120
        }
121
      }
122
    else
123
      {
124
      mID = 0;
125
      }
126

    
127
    int numNodes = (mNodes==null ? 0: mNodes.size());
128
    for(int i=0; i<numNodes; i++) mNodes.get(i).sort();
129
    }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132

    
133
  void newNode(DistortedNode node)
134
    {
135
    if( mNodes==null ) mNodes = new ArrayList<>();
136

    
137
    mNodes.add(node);
138
    }
139

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

    
142
  void removeNode(DistortedNode node)
143
    {
144
    mNodes.remove(node);
145
    }
146

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

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

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156

    
157
  long getID()
158
    {
159
    return mID;
160
    }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163

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

    
172
    return false;
173
    }
174

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

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

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

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

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

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

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

    
207
  static void onDestroy()
208
    {
209
    mNextID = 1;
210
    mMapID.clear();
211
    EffectType.reset(mMax);
212
    }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215
// this assumes 0<=effect<mNumEffects
216

    
217
  protected void remove(int effect)
218
    {
219
    mNumEffects--;
220

    
221
    long removedID = mEffects[effect].getID();
222

    
223
    for(int j=effect; j<mNumEffects; j++ )
224
      {
225
      mEffects[j]         = mEffects[j+1];
226
      mCurrentDuration[j] = mCurrentDuration[j+1];
227
      mName[j]            = mName[j+1];
228
      }
229

    
230
    mEffects[mNumEffects] = null;
231

    
232
    for(int i=0; i<mNumListeners; i++)
233
      EffectMessageSender.newMessage( mListeners.get(i), EffectMessage.EFFECT_REMOVED, removedID, mDistortedEffectsID);
234
    }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
  synchronized int removeByName(EffectName name)
239
    {
240
    int ret = 0;
241

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

    
251
    if( ret>0 )
252
      {
253
      DistortedMaster.newSlave(this);
254
      mNumEffectsToBe-=ret;
255
      }
256

    
257
    return ret;
258
    }
259

    
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261

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

    
275
    return 0;
276
    }
277

    
278
///////////////////////////////////////////////////////////////////////////////////////////////////
279

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

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

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

    
308
///////////////////////////////////////////////////////////////////////////////////////////////////
309
  
310
  boolean add(Effect effect)
311
    {
312
    if( mMax[mIndex]>mNumEffectsToBe || !mCreated )
313
      {
314
      mJobs.add(new Job(ATTACH,0,false,effect));
315
      DistortedMaster.newSlave(this);
316
      mNumEffectsToBe++;
317
      return true;
318
      }
319

    
320
    return false;
321
    }
322

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

    
337
    for(int i=0; i<num; i++)
338
      {
339
      job = mJobs.remove(0);
340

    
341
      switch(job.type)
342
        {
343
        case CREATE: int max = mMax[mIndex];
344
                     if( max>0 )
345
                       {
346
                       mUniforms        = new float[max*job.num];
347
                       mCurrentDuration = new long[max];
348
                       mEffects         = new Effect[max];
349
                       mName            = new int[max];
350
                       }
351
                     mCreated = true;
352

    
353
                     break;
354
        case ATTACH: if( mMax[mIndex]>mNumEffects ) // it is possible that we have first
355
                       {                            // added effects and then lowered mMax
356
                       mCurrentDuration[mNumEffects] = 0;
357
                       mEffects[mNumEffects] = job.effect;
358
                       mName[mNumEffects] = job.effect.getName().ordinal();
359
                       mNumEffects++;
360
                       changed = true;
361
                       }
362
                     else
363
                       {
364
                       android.util.Log.e("queue", "failed to add effect "+job.effect.getName());
365
                       }
366
                     break;
367
        case DETACH: for(int j=0; j<mNumEffects; j++)
368
                       {
369
                       if (mEffects[j] == job.effect)
370
                         {
371
                         remove(j);
372
                         changed = true;
373
                         break;
374
                         }
375
                       }
376
                     break;
377
        case DETALL: for(int j=0; j<mNumEffects; j++ )
378
                       {
379
                       changed = true;
380

    
381
                       if( job.notify )
382
                         {
383
                         for(int k=0; k<mNumListeners; k++)
384
                           EffectMessageSender.newMessage( mListeners.get(k), EffectMessage.EFFECT_REMOVED, mEffects[j].getID(), mDistortedEffectsID);
385
                         }
386

    
387
                       mEffects[j] = null;
388
                       }
389

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

    
395
    if( changed && mIndex==EffectType.POSTPROCESS.ordinal() ) regenerateIDandSort();
396
    }
397
  }
(15-15/19)