Project

General

Profile

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

library / src / main / java / org / distorted / library / main / EffectQueue.java @ 1418a5eb

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
  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

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

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

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

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

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

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

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

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

    
125
    int numNodes = (mNodes==null ? 0: mNodes.size());
126
    for(int i=0; i<numNodes; i++) mNodes.get(i).sort();
127

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

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

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

    
146
    mNodes.add(node);
147
    }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150

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

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

    
159
  long getID()
160
    {
161
    return mID;
162
    }
163

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

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

    
174
    return false;
175
    }
176

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178

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

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

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

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

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

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

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

    
216
///////////////////////////////////////////////////////////////////////////////////////////////////
217
// this assumes 0<=effect<mNumEffects
218

    
219
  protected void remove(int effect)
220
    {
221
    mNumEffects--;
222

    
223
    long removedID = mEffects[effect].getID();
224

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

    
232
    mEffects[mNumEffects] = null;
233

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

    
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239

    
240
  synchronized int removeByName(EffectName name)
241
    {
242
    int ret = 0;
243

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

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

    
259
    return ret;
260
    }
261

    
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263

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

    
277
    return 0;
278
    }
279

    
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281

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

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

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

    
310
///////////////////////////////////////////////////////////////////////////////////////////////////
311
  
312
  boolean add(Effect effect)
313
    {
314
    if( mMax[mIndex]>mNumEffectsToBe )
315
      {
316
      //android.util.Log.e("queue", "scheduling future add of "+effect.getName().name()+" to "+mNumEffectsToBe+" id="+effect.getID());
317
      //android.util.Log.e("queue", "queue id="+mDistortedEffectsID);
318

    
319
      mJobs.add(new Job(ATTACH,0,false,effect));
320
      DistortedMaster.newSlave(this);
321
      mNumEffectsToBe++;
322
      return true;
323
      }
324

    
325
    return false;
326
    }
327

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

    
341
    for(int i=0; i<num; i++)
342
      {
343
      job = mJobs.remove(0);
344

    
345
      switch(job.type)
346
        {
347
        case CREATE: int max = mMax[mIndex];
348

    
349
                     if( max>0 )
350
                       {
351
                       mUniforms        = new float[max*job.num];
352
                       mCurrentDuration = new long[max];
353
                       mEffects         = new Effect[max];
354
                       mName            = new int[max];
355
                       }
356
                     break;
357
        case ATTACH: if( mMax[mIndex]>mNumEffects ) // it is possible that we have first
358
                       {                            // added effects and then lowered mMax
359
                       mCurrentDuration[mNumEffects] = 0;
360
                       mEffects[mNumEffects] = job.effect;
361
                       mName[mNumEffects] = job.effect.getName().ordinal();
362
                       mNumEffects++;
363
                       }
364
                     else
365
                       {
366
                       android.util.Log.e("queue", "failed to add effect "+job.effect.getName());
367
                       }
368
                     break;
369
        case DETACH: for(int j=0; j<mNumEffects; j++)
370
                       {
371
                       if (mEffects[j] == job.effect)
372
                         {
373
                         remove(j);
374
                         break;
375
                         }
376
                       }
377
                     break;
378
        case DETALL: for(int j=0; j<mNumEffects; j++ )
379
                       {
380
                       if( job.notify )
381
                         {
382
                         for(int k=0; k<mNumListeners; k++)
383
                           EffectMessageSender.newMessage( mListeners.get(k), EffectMessage.EFFECT_REMOVED, mEffects[j].getID(), mDistortedEffectsID);
384
                         }
385

    
386
                       mEffects[j] = null;
387
                       }
388

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

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