Project

General

Profile

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

library / src / main / java / org / distorted / library / main / EffectQueue.java @ 06dbccad

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
  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
  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
  @SuppressWarnings("unused")
154
  int getNumEffects()
155
    {
156
    return mNumEffects;  
157
    }
158

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

    
161
  long getID()
162
    {
163
    return mID;
164
    }
165

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

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

    
176
    return false;
177
    }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180

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

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

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

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

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

    
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210

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

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219
// this assumes 0<=effect<mNumEffects
220

    
221
  protected void remove(int effect)
222
    {
223
    mNumEffects--;
224

    
225
    long removedID = mEffects[effect].getID();
226

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

    
234
    mEffects[mNumEffects] = null;
235

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

    
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241

    
242
  synchronized int removeByName(EffectName name)
243
    {
244
    int ret = 0;
245

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

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

    
261
    return ret;
262
    }
263

    
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265

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

    
279
    return 0;
280
    }
281

    
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283

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

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

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

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

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

    
327
    return false;
328
    }
329

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

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

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

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

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

    
390
                       mEffects[j] = null;
391
                       }
392

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

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