Project

General

Profile

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

library / src / main / java / org / distorted / library / effectqueue / EffectQueue.java @ 0876b482

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
  public 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
  int[] mAndAssociation;
55
  int[] mEquAssociation;
56
  long mTime;
57

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

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

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

    
82
  private ArrayList<Job> mJobs = new ArrayList<>();
83

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

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

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106
// this is not thread safe! The 'source' might change while we're copying it.
107

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

    
120
      int numJobs = source.mJobs.size();
121

    
122
      for(int i=0; i<numJobs; i++)
123
        {
124
        Job job = source.mJobs.get(i);
125
        mJobs.add(job);
126
        }
127

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

    
140
      int max = mMax[mIndex];
141
      if( max>0 )
142
        {
143
        mUniforms        = new float[max*source.mNumUniforms];
144
        mCurrentDuration = new long[max];
145
        mEffects         = new Effect[max];
146
        mName            = new int[max];
147
        mAndAssociation  = new int[max];
148
        mEquAssociation  = new int[max];
149
        }
150

    
151
      for(int i=0; i<mNumEffects; i++ )
152
        {
153
        mEffects[i]         = source.mEffects[i];
154
        mCurrentDuration[i] = source.mCurrentDuration[i];
155
        mName[i]            = source.mName[i];
156
        mAndAssociation[i]  = source.mAndAssociation[i];
157
        mEquAssociation[i]  = source.mEquAssociation[i];
158
        }
159
      }
160
    }
161

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

    
164
  public static void allocateQueues(EffectQueue[] queues, EffectQueue[] from, int flags)
165
    {
166
    queues[0] = (flags & DistortedLibrary.CLONE_MATRIX     ) != 0 ? from[0] : new EffectQueueMatrix();
167
    queues[1] = (flags & DistortedLibrary.CLONE_VERTEX     ) != 0 ? from[1] : new EffectQueueVertex();
168
    queues[2] = (flags & DistortedLibrary.CLONE_FRAGMENT   ) != 0 ? from[2] : new EffectQueueFragment();
169
    queues[3] = (flags & DistortedLibrary.CLONE_POSTPROCESS) != 0 ? from[3] : new EffectQueuePostprocess();
170
    }
171

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

    
174
  public static void compute(EffectQueue[] queues, long currTime)
175
    {
176
    ((EffectQueueMatrix     )queues[0]).compute(currTime);
177
    ((EffectQueueVertex     )queues[1]).compute(currTime);
178
    ((EffectQueueFragment   )queues[2]).compute(currTime);
179
    ((EffectQueuePostprocess)queues[3]).compute(currTime);
180
    }
181

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

    
184
  public static void send(EffectQueue[] queues, float distance, float mipmap,
185
                          float[] projection, float inflate, MeshBase mesh, int variant )
186
    {
187
    ((EffectQueueMatrix  )queues[0]).send(distance, mipmap, projection, mesh, variant);
188
    ((EffectQueueVertex  )queues[1]).send(inflate, variant);
189
    ((EffectQueueFragment)queues[2]).send(variant);
190
    }
191

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193

    
194
  public static float[] getMVP(EffectQueue[] queues)
195
    {
196
    return ((EffectQueueMatrix)queues[0]).getMVP();
197
    }
198

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200
// variant: 0 --> MAIN  1 --> OIT  2 --> prePOST  3 --> FULL
201

    
202
  public static void getUniforms(int programH, int variant)
203
    {
204
    EffectQueueFragment.uniforms(programH,variant);
205
    EffectQueueVertex  .uniforms(programH,variant);
206
    EffectQueueMatrix  .uniforms(programH,variant);
207
    }
208

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

    
214
  private void regenerateID()
215
    {
216
    if( mNumEffects>0 )
217
      {
218
      ArrayList<Long> list = new ArrayList<>();
219
      for (int i = 0; i < mNumEffects; i++) list.add(mEffects[i].getID());
220
      Long id = mMapID.get(list);
221

    
222
      if( id!=null )
223
        {
224
        mID = id;
225
        }
226
      else
227
        {
228
        mMapID.put(list,mNextID);
229
        mID = mNextID++;
230
        }
231
      }
232
    else
233
      {
234
      mID = 0;
235
      }
236
    }
237

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

    
240
  public long getID()
241
    {
242
    return mID;
243
    }
244

    
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246

    
247
  public static boolean setMax(int index, int m)
248
    {
249
    if( !DistortedLibrary.isInitialized() || m<=mMax[index] )
250
      {
251
      mMax[index] = m<0 ? 0:m;
252
      return true;
253
      }
254

    
255
    return false;
256
    }
257

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

    
260
  public static int getMax(int index)
261
    {
262
    return mMax[index];
263
    }
264

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266

    
267
  public static void onDestroy()
268
    {
269
    mNextID = 1;
270
    mMapID.clear();
271
    EffectType.reset(mMax);
272
    }
273

    
274
///////////////////////////////////////////////////////////////////////////////////////////////////
275
// this assumes 0<=effect<mNumEffects
276

    
277
  protected void remove(int effect)
278
    {
279
    mNumEffects--;
280

    
281
    for(int j=effect; j<mNumEffects; j++ )
282
      {
283
      mEffects[j]         = mEffects[j+1];
284
      mCurrentDuration[j] = mCurrentDuration[j+1];
285
      mName[j]            = mName[j+1];
286
      mAndAssociation[j]  = mAndAssociation[j+1];
287
      mEquAssociation[j]  = mEquAssociation[j+1];
288
      }
289

    
290
    mEffects[mNumEffects] = null;
291
    }
292

    
293
///////////////////////////////////////////////////////////////////////////////////////////////////
294

    
295
  public synchronized int removeByName(EffectName name)
296
    {
297
    int ret = 0;
298

    
299
    for(int i=0; i<mNumEffects; i++)
300
      {
301
      if( mEffects[i].getName() == name )
302
        {
303
        mJobs.add(new Job(DETACH,0,true,mEffects[i]));
304
        ret++;
305
        }
306
      }
307

    
308
    if( ret>0 )
309
      {
310
      InternalMaster.newSlave(this);
311
      mNumEffectsToBe-=ret;
312
      }
313

    
314
    return ret;
315
    }
316

    
317
///////////////////////////////////////////////////////////////////////////////////////////////////
318

    
319
  public synchronized int removeById(long id)
320
    {
321
    for(int i=0; i<mNumEffects; i++)
322
      {
323
      if( mEffects[i].getID() == id )
324
        {
325
        mJobs.add(new Job(DETACH,0,true,mEffects[i]));
326
        InternalMaster.newSlave(this);
327
        mNumEffectsToBe--;
328
        return 1;
329
        }
330
      }
331

    
332
    return 0;
333
    }
334

    
335
///////////////////////////////////////////////////////////////////////////////////////////////////
336

    
337
  public synchronized int removeEffect(Effect effect)
338
    {
339
    for(int i=0; i<mNumEffects; i++)
340
      {
341
      if( mEffects[i]==effect )
342
        {
343
        mJobs.add(new Job(DETACH,0,true,mEffects[i]));
344
        InternalMaster.newSlave(this);
345
        mNumEffectsToBe--;
346
        return 1;
347
        }
348
      }
349
   
350
    return 0;
351
    }
352

    
353
///////////////////////////////////////////////////////////////////////////////////////////////////
354
// we do want to notify Listeners if they called 'abortAll' themselves but don't want to notify
355
// them if it is the library itself which is releasing resources.
356

    
357
  public synchronized int removeAll(boolean notify)
358
    {
359
    mJobs.add(new Job(DETALL,0,notify,null));
360
    InternalMaster.newSlave(this);
361
    mNumEffectsToBe = 0;
362
    return mNumEffects;
363
    }
364

    
365
///////////////////////////////////////////////////////////////////////////////////////////////////
366
  
367
  public boolean add(Effect effect)
368
    {
369
    if( mMax[mIndex]>mNumEffectsToBe || !mCreated )
370
      {
371
      mJobs.add(new Job(ATTACH,-1,false,effect));
372
      InternalMaster.newSlave(this);
373
      mNumEffectsToBe++;
374
      return true;
375
      }
376

    
377
    return false;
378
    }
379

    
380
///////////////////////////////////////////////////////////////////////////////////////////////////
381

    
382
  public boolean add(Effect effect, int position)
383
    {
384
    if( mMax[mIndex]>mNumEffectsToBe || !mCreated )
385
      {
386
      mJobs.add(new Job(ATTACH,position,false,effect));
387
      InternalMaster.newSlave(this);
388
      mNumEffectsToBe++;
389
      return true;
390
      }
391

    
392
    return false;
393
    }
394

    
395
///////////////////////////////////////////////////////////////////////////////////////////////////
396

    
397
  public int getNumEffects()
398
    {
399
    return mNumEffects;
400
    }
401

    
402
///////////////////////////////////////////////////////////////////////////////////////////////////
403

    
404
  public int getNumEffectsToBe()
405
    {
406
    return mNumEffectsToBe;
407
    }
408

    
409
///////////////////////////////////////////////////////////////////////////////////////////////////
410

    
411
  public Effect getEffect(int position)
412
    {
413
    if( position>=0 && position< mNumEffects )
414
      {
415
      return mEffects[position];
416
      }
417
    else
418
      {
419
      android.util.Log.e("queue", "getEffect: out of range "+position);
420
      return null;
421
      }
422
    }
423

    
424
///////////////////////////////////////////////////////////////////////////////////////////////////
425

    
426
  public void doWork()
427
    {
428
    boolean changed = false;
429
    int num = mJobs.size();
430
    Job job;
431

    
432
    for(int i=0; i<num; i++)
433
      {
434
      job = mJobs.remove(0);
435

    
436
      switch(job.type)
437
        {
438
        case CREATE: int max = mMax[mIndex];
439
                     if( max>0 )
440
                       {
441
                       mUniforms        = new float[max*job.num];
442
                       mCurrentDuration = new long[max];
443
                       mEffects         = new Effect[max];
444
                       mName            = new int[max];
445
                       mAndAssociation  = new int[max];
446
                       mEquAssociation  = new int[max];
447
                       }
448
                     mCreated = true;
449

    
450
                     break;
451
        case ATTACH: if( mMax[mIndex]>mNumEffects ) // it is possible that we have first
452
                       {                            // added effects and then lowered mMax
453
                       int position = job.num;
454

    
455
                       if( position==-1 )
456
                         {
457
                         mCurrentDuration[mNumEffects]= 0;
458
                         mEffects[mNumEffects]        = job.effect;
459
                         mName[mNumEffects]           = job.effect.getName().ordinal();
460
                         mAndAssociation[mNumEffects] = job.effect.getAndAssociation();
461
                         mEquAssociation[mNumEffects] = job.effect.getEquAssociation();
462

    
463
                         mNumEffects++;
464
                         changed = true;
465
                         }
466
                       else if( position>=0 && position<=mNumEffects )
467
                         {
468
                         for(int j=mNumEffects; j>position; j--)
469
                           {
470
                           mCurrentDuration[j] = mCurrentDuration[j-1];
471
                           mEffects[j]         = mEffects[j-1];
472
                           mName[j]            = mName[j-1];
473
                           mAndAssociation[j]  = mAndAssociation[j-1];
474
                           mEquAssociation[j]  = mEquAssociation[j-1];
475
                           }
476

    
477
                         mCurrentDuration[position]= 0;
478
                         mEffects[position]        = job.effect;
479
                         mName[position]           = job.effect.getName().ordinal();
480
                         mAndAssociation[position] = job.effect.getAndAssociation();
481
                         mEquAssociation[position] = job.effect.getEquAssociation();
482

    
483
                         mNumEffects++;
484
                         changed = true;
485
                         }
486
                       }
487
                     else
488
                       {
489
                       android.util.Log.e("queue", "failed to add effect "+job.effect.getName());
490
                       }
491
                     break;
492
        case DETACH: for(int j=0; j<mNumEffects; j++)
493
                       {
494
                       if (mEffects[j] == job.effect)
495
                         {
496
                         remove(j);
497
                         changed = true;
498
                         break;
499
                         }
500
                       }
501
                     break;
502
        case DETALL: for(int j=0; j<mNumEffects; j++ )
503
                       {
504
                       changed = true;
505
                       mEffects[j] = null;
506
                       }
507

    
508
                     mNumEffects= 0;
509
                     break;
510
        }
511
      }
512

    
513
    if( changed && mIndex==EffectType.POSTPROCESS.ordinal() ) regenerateID();
514
    }
515
  }
(1-1/5)