Project

General

Profile

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

library / src / main / java / org / distorted / library / effectqueue / EffectQueue.java @ f046b159

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.effectqueue;
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.main.DistortedLibrary;
26
import org.distorted.library.main.InternalMaster;
27
import org.distorted.library.mesh.MeshBase;
28

    
29
import java.util.ArrayList;
30
import java.util.HashMap;
31

    
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33
/**
34
 * Not part of public API, do not document
35
 *
36
 * @y.exclude
37
 */
38
public abstract class EffectQueue implements InternalMaster.Slave
39
  {
40
  static final int MAIN_VARIANTS = 4; // Number of Main program variants (ATM 4: MAIN, MAIN OIT, PREPROCESS, FULL)
41

    
42
  private static final int CREATE = 0;
43
  private static final int ATTACH = 1;
44
  private static final int DETACH = 2;
45
  private static final int DETALL = 3;
46

    
47
  int mNumEffects;              // 'ToBe' will be more than mNumEffects if doWork() hasn't
48
  private int mNumEffectsToBe;  // added them yet (or less if it hasn't removed some yet)
49
  float[] mUniforms;
50
  private int mNumUniforms;
51
  long[] mCurrentDuration;
52
  Effect[] mEffects;
53
  int[] mName;
54
  long mTime;
55

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

    
64
  private static class Job
65
    {
66
    int type;
67
    int num;
68
    boolean notify;
69
    Effect effect;
70

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

    
80
  private ArrayList<Job> mJobs = new ArrayList<>();
81

    
82
  static
83
    {
84
    onDestroy();
85
    }
86
  
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88
   
89
  EffectQueue(int numUniforms, int index)
90
    {
91
    mCreated            = false;
92
    mTime               = 0;
93
    mID                 = 0;
94
    mNumEffects         = 0;
95
    mNumEffectsToBe     = 0;
96
    mIndex              = index;
97
    mNumUniforms        = numUniforms;
98

    
99
    mJobs.add(new Job(CREATE,numUniforms,false,null)); // create the stuff that depends on max number
100
    InternalMaster.newSlave(this);                     // of uniforms later, on first render.
101
    }
102

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104

    
105
  EffectQueue(EffectQueue source)
106
    {
107
    if( !source.mCreated )
108
      {
109
      mCreated            = false;
110
      mTime               = 0;
111
      mID                 = 0;
112
      mNumEffects         = 0;
113
      mNumEffectsToBe     = 0;
114
      mIndex              = source.mIndex;
115
      mNumUniforms        = source.mNumUniforms;
116

    
117
      int numJobs = source.mJobs.size();
118

    
119
      for(int i=0; i<numJobs; i++)
120
        {
121
        Job job = source.mJobs.get(i);
122
        mJobs.add(job);
123
        }
124

    
125
      InternalMaster.newSlave(this);
126
      }
127
    else
128
      {
129
      mCreated            = true;
130
      mTime               = source.mTime;
131
      mID                 = source.mID;
132
      mNumEffects         = source.mNumEffects;
133
      mNumEffectsToBe     = source.mNumEffectsToBe;
134
      mIndex              = source.mIndex;
135
      mNumUniforms        = source.mNumUniforms;
136

    
137
      int max = mMax[mIndex];
138
      if( max>0 )
139
        {
140
        mUniforms        = new float[max*source.mNumUniforms];
141
        mCurrentDuration = new long[max];
142
        mEffects         = new Effect[max];
143
        mName            = new int[max];
144
        }
145

    
146
      for(int i=0; i<mNumEffects; i++ )
147
        {
148
        mEffects[i]         = source.mEffects[i];
149
        mCurrentDuration[i] = source.mCurrentDuration[i];
150
        mName[i]            = source.mName[i];
151
        }
152
      }
153
    }
154

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

    
157
  public static void allocateQueues(EffectQueue[] queues, EffectQueue[] from, int flags)
158
    {
159
    queues[0] = (flags & DistortedLibrary.CLONE_MATRIX     ) != 0 ? from[0] : new EffectQueueMatrix();
160
    queues[1] = (flags & DistortedLibrary.CLONE_VERTEX     ) != 0 ? from[1] : new EffectQueueVertex();
161
    queues[2] = (flags & DistortedLibrary.CLONE_FRAGMENT   ) != 0 ? from[2] : new EffectQueueFragment();
162
    queues[3] = (flags & DistortedLibrary.CLONE_POSTPROCESS) != 0 ? from[3] : new EffectQueuePostprocess();
163
    }
164

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

    
167
  public static void compute(EffectQueue[] queues, long currTime)
168
    {
169
    ((EffectQueueMatrix     )queues[0]).compute(currTime);
170
    ((EffectQueueVertex     )queues[1]).compute(currTime);
171
    ((EffectQueueFragment   )queues[2]).compute(currTime);
172
    ((EffectQueuePostprocess)queues[3]).compute(currTime);
173
    }
174

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

    
177
  public static void send(EffectQueue[] queues, float distance, float mipmap,
178
                          float[] projection, float inflate, MeshBase mesh, int variant )
179
    {
180
    ((EffectQueueMatrix  )queues[0]).send(distance, mipmap, projection, mesh, variant);
181
    ((EffectQueueVertex  )queues[1]).send(inflate, variant);
182
    ((EffectQueueFragment)queues[2]).send(variant);
183
    }
184

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

    
187
  public static float[] getMVP(EffectQueue[] queues)
188
    {
189
    return ((EffectQueueMatrix)queues[0]).getMVP();
190
    }
191

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193
// variant: 0 --> MAIN  1 --> OIT  2 --> prePOST  3 --> FULL
194

    
195
  public static void getUniforms(int programH, int variant)
196
    {
197
    EffectQueueFragment.uniforms(programH,variant);
198
    EffectQueueVertex  .uniforms(programH,variant);
199
    EffectQueueMatrix  .uniforms(programH,variant);
200
    }
201

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

    
207
  private void regenerateID()
208
    {
209
    if( mNumEffects>0 )
210
      {
211
      ArrayList<Long> list = new ArrayList<>();
212
      for (int i = 0; i < mNumEffects; i++) list.add(mEffects[i].getID());
213
      Long id = mMapID.get(list);
214

    
215
      if( id!=null )
216
        {
217
        mID = id;
218
        }
219
      else
220
        {
221
        mMapID.put(list,mNextID);
222
        mID = mNextID++;
223
        }
224
      }
225
    else
226
      {
227
      mID = 0;
228
      }
229
    }
230

    
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232

    
233
  public long getID()
234
    {
235
    return mID;
236
    }
237

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

    
240
  public static boolean setMax(int index, int m)
241
    {
242
    if( !DistortedLibrary.isInitialized() || m<=mMax[index] )
243
      {
244
      mMax[index] = m<0 ? 0:m;
245
      return true;
246
      }
247

    
248
    return false;
249
    }
250

    
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252

    
253
  public static int getMax(int index)
254
    {
255
    return mMax[index];
256
    }
257

    
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259

    
260
  public static void onDestroy()
261
    {
262
    mNextID = 1;
263
    mMapID.clear();
264
    EffectType.reset(mMax);
265
    }
266

    
267
///////////////////////////////////////////////////////////////////////////////////////////////////
268
// this assumes 0<=effect<mNumEffects
269

    
270
  protected void remove(int effect)
271
    {
272
    mNumEffects--;
273

    
274
    for(int j=effect; j<mNumEffects; j++ )
275
      {
276
      mEffects[j]         = mEffects[j+1];
277
      mCurrentDuration[j] = mCurrentDuration[j+1];
278
      mName[j]            = mName[j+1];
279
      }
280

    
281
    mEffects[mNumEffects] = null;
282
    }
283

    
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285

    
286
  public synchronized int removeByName(EffectName name)
287
    {
288
    int ret = 0;
289

    
290
    for(int i=0; i<mNumEffects; i++)
291
      {
292
      if( mEffects[i].getName() == name )
293
        {
294
        mJobs.add(new Job(DETACH,0,true,mEffects[i]));
295
        ret++;
296
        }
297
      }
298

    
299
    if( ret>0 )
300
      {
301
      InternalMaster.newSlave(this);
302
      mNumEffectsToBe-=ret;
303
      }
304

    
305
    return ret;
306
    }
307

    
308
///////////////////////////////////////////////////////////////////////////////////////////////////
309

    
310
  public synchronized int removeById(long id)
311
    {
312
    for(int i=0; i<mNumEffects; i++)
313
      {
314
      if( mEffects[i].getID() == id )
315
        {
316
        mJobs.add(new Job(DETACH,0,true,mEffects[i]));
317
        InternalMaster.newSlave(this);
318
        mNumEffectsToBe--;
319
        return 1;
320
        }
321
      }
322

    
323
    return 0;
324
    }
325

    
326
///////////////////////////////////////////////////////////////////////////////////////////////////
327

    
328
  public synchronized int removeEffect(Effect effect)
329
    {
330
    for(int i=0; i<mNumEffects; i++)
331
      {
332
      if( mEffects[i]==effect )
333
        {
334
        mJobs.add(new Job(DETACH,0,true,mEffects[i]));
335
        InternalMaster.newSlave(this);
336
        mNumEffectsToBe--;
337
        return 1;
338
        }
339
      }
340
   
341
    return 0;
342
    }
343

    
344
///////////////////////////////////////////////////////////////////////////////////////////////////
345
// we do want to notify Listeners if they called 'abortAll' themselves but don't want to notify
346
// them if it is the library itself which is releasing resources.
347

    
348
  public synchronized int abortAll(boolean notify)
349
    {
350
    mJobs.add(new Job(DETALL,0,notify,null));
351
    InternalMaster.newSlave(this);
352
    mNumEffectsToBe = 0;
353
    return mNumEffects;
354
    }
355

    
356
///////////////////////////////////////////////////////////////////////////////////////////////////
357
  
358
  public boolean add(Effect effect)
359
    {
360
    if( mMax[mIndex]>mNumEffectsToBe || !mCreated )
361
      {
362
      mJobs.add(new Job(ATTACH,-1,false,effect));
363
      InternalMaster.newSlave(this);
364
      mNumEffectsToBe++;
365
      return true;
366
      }
367

    
368
    return false;
369
    }
370

    
371
///////////////////////////////////////////////////////////////////////////////////////////////////
372

    
373
  public boolean add(Effect effect, int position)
374
    {
375
    if( mMax[mIndex]>mNumEffectsToBe || !mCreated )
376
      {
377
      mJobs.add(new Job(ATTACH,position,false,effect));
378
      InternalMaster.newSlave(this);
379
      mNumEffectsToBe++;
380
      return true;
381
      }
382

    
383
    return false;
384
    }
385

    
386
///////////////////////////////////////////////////////////////////////////////////////////////////
387

    
388
  public int getNumEffects()
389
    {
390
    return mNumEffects;
391
    }
392

    
393
///////////////////////////////////////////////////////////////////////////////////////////////////
394

    
395
  public Effect getEffect(int position)
396
    {
397
    if( position>=0 && position< mNumEffects )
398
      {
399
      return mEffects[position];
400
      }
401
    else
402
      {
403
      android.util.Log.e("queue", "getEffect: out of range "+position);
404
      return null;
405
      }
406
    }
407

    
408
///////////////////////////////////////////////////////////////////////////////////////////////////
409

    
410
  public void doWork()
411
    {
412
    boolean changed = false;
413
    int num = mJobs.size();
414
    Job job;
415

    
416
    for(int i=0; i<num; i++)
417
      {
418
      job = mJobs.remove(0);
419

    
420
      switch(job.type)
421
        {
422
        case CREATE: int max = mMax[mIndex];
423
                     if( max>0 )
424
                       {
425
                       mUniforms        = new float[max*job.num];
426
                       mCurrentDuration = new long[max];
427
                       mEffects         = new Effect[max];
428
                       mName            = new int[max];
429
                       }
430
                     mCreated = true;
431

    
432
                     break;
433
        case ATTACH: if( mMax[mIndex]>mNumEffects ) // it is possible that we have first
434
                       {                            // added effects and then lowered mMax
435
                       int position = job.num;
436

    
437
                       if( position==-1 )
438
                         {
439
                         mCurrentDuration[mNumEffects] = 0;
440
                         mEffects[mNumEffects] = job.effect;
441
                         mName[mNumEffects] = job.effect.getName().ordinal();
442

    
443
                         mNumEffects++;
444
                         changed = true;
445
                         }
446
                       else if( position>=0 && position<=mNumEffects )
447
                         {
448
                         for(int j=mNumEffects; j>position; j--)
449
                           {
450
                           mCurrentDuration[j] = mCurrentDuration[j-1];
451
                           mEffects[j]         = mEffects[j-1];
452
                           mName[j]            = mName[j-1];
453
                           }
454

    
455
                         mCurrentDuration[position] = 0;
456
                         mEffects[position] = job.effect;
457
                         mName[position] = job.effect.getName().ordinal();
458

    
459
                         mNumEffects++;
460
                         changed = true;
461
                         }
462
                       }
463
                     else
464
                       {
465
                       android.util.Log.e("queue", "failed to add effect "+job.effect.getName());
466
                       }
467
                     break;
468
        case DETACH: for(int j=0; j<mNumEffects; j++)
469
                       {
470
                       if (mEffects[j] == job.effect)
471
                         {
472
                         remove(j);
473
                         changed = true;
474
                         break;
475
                         }
476
                       }
477
                     break;
478
        case DETALL: for(int j=0; j<mNumEffects; j++ )
479
                       {
480
                       changed = true;
481
                       mEffects[j] = null;
482
                       }
483

    
484
                     mNumEffects= 0;
485
                     break;
486
        }
487
      }
488

    
489
    if( changed && mIndex==EffectType.POSTPROCESS.ordinal() ) regenerateID();
490
    }
491
  }
(1-1/5)