Project

General

Profile

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

library / src / main / java / org / distorted / library / main / EffectQueue.java @ 8bfefd68

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

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143

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

    
148
    mNodes.add(node);
149
    }
150

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152

    
153
  void removeNode(DistortedNode node)
154
    {
155
    mNodes.remove(node);
156
    }
157

    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159

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

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

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

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

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

    
183
    return false;
184
    }
185

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187

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

    
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194

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

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

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

    
216
///////////////////////////////////////////////////////////////////////////////////////////////////
217

    
218
  static void onDestroy()
219
    {
220
    mNextID = 1;
221
    mMapID.clear();
222
    EffectType.reset(mMax);
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,0,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,0,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,0,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,0,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 || !mCreated )
324
      {
325
      mJobs.add(new Job(ATTACH,0,false,effect));
326
      DistortedMaster.newSlave(this);
327
      mNumEffectsToBe++;
328
      return true;
329
      }
330

    
331
    return false;
332
    }
333

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

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

    
351
      switch(job.type)
352
        {
353
        case CREATE: int max = mMax[mIndex];
354

    
355
                     if( max>0 )
356
                       {
357
                       mUniforms        = new float[max*job.num];
358
                       mCurrentDuration = new long[max];
359
                       mEffects         = new Effect[max];
360
                       mName            = new int[max];
361
                       }
362
                     mCreated = true;
363

    
364
                     break;
365
        case ATTACH: if( mMax[mIndex]>mNumEffects ) // it is possible that we have first
366
                       {                            // added effects and then lowered mMax
367
                       mCurrentDuration[mNumEffects] = 0;
368
                       mEffects[mNumEffects] = job.effect;
369
                       mName[mNumEffects] = job.effect.getName().ordinal();
370
                       mNumEffects++;
371
                       }
372
                     else
373
                       {
374
                       android.util.Log.e("queue", "failed to add effect "+job.effect.getName());
375
                       }
376
                     break;
377
        case DETACH: for(int j=0; j<mNumEffects; j++)
378
                       {
379
                       if (mEffects[j] == job.effect)
380
                         {
381
                         remove(j);
382
                         break;
383
                         }
384
                       }
385
                     break;
386
        case DETALL: for(int j=0; j<mNumEffects; j++ )
387
                       {
388
                       if( job.notify )
389
                         {
390
                         for(int k=0; k<mNumListeners; k++)
391
                           EffectMessageSender.newMessage( mListeners.get(k), EffectMessage.EFFECT_REMOVED, mEffects[j].getID(), mDistortedEffectsID);
392
                         }
393

    
394
                       mEffects[j] = null;
395
                       }
396

    
397
                     mNumEffects= 0;
398
                     break;
399
        }
400
      }
401

    
402
    if( num>0 && mIndex==EffectType.POSTPROCESS.ordinal() ) regenerateIDandSort();
403
    }
404
  }
(14-14/18)