Project

General

Profile

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

library / src / main / java / org / distorted / library / effectqueue / EffectQueue.java @ 1e672c1d

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
  int[] mAssociation;
55
  long mTime;
56

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

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

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

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

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

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

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105

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

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

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

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

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

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

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

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

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

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

    
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179

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

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189

    
190
  public static float[] getMVP(EffectQueue[] queues)
191
    {
192
    return ((EffectQueueMatrix)queues[0]).getMVP();
193
    }
194

    
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196
// variant: 0 --> MAIN  1 --> OIT  2 --> prePOST  3 --> FULL
197

    
198
  public static void getUniforms(int programH, int variant)
199
    {
200
    EffectQueueFragment.uniforms(programH,variant);
201
    EffectQueueVertex  .uniforms(programH,variant);
202
    EffectQueueMatrix  .uniforms(programH,variant);
203
    }
204

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

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

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

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235

    
236
  public long getID()
237
    {
238
    return mID;
239
    }
240

    
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242

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

    
251
    return false;
252
    }
253

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255

    
256
  public static int getMax(int index)
257
    {
258
    return mMax[index];
259
    }
260

    
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262

    
263
  public static void onDestroy()
264
    {
265
    mNextID = 1;
266
    mMapID.clear();
267
    EffectType.reset(mMax);
268
    }
269

    
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271
// this assumes 0<=effect<mNumEffects
272

    
273
  protected void remove(int effect)
274
    {
275
    mNumEffects--;
276

    
277
    for(int j=effect; j<mNumEffects; j++ )
278
      {
279
      mEffects[j]         = mEffects[j+1];
280
      mCurrentDuration[j] = mCurrentDuration[j+1];
281
      mName[j]            = mName[j+1];
282
      mAssociation[j]     = mAssociation[j+1];
283
      }
284

    
285
    mEffects[mNumEffects] = null;
286
    }
287

    
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289

    
290
  public synchronized int removeByName(EffectName name)
291
    {
292
    int ret = 0;
293

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

    
303
    if( ret>0 )
304
      {
305
      InternalMaster.newSlave(this);
306
      mNumEffectsToBe-=ret;
307
      }
308

    
309
    return ret;
310
    }
311

    
312
///////////////////////////////////////////////////////////////////////////////////////////////////
313

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

    
327
    return 0;
328
    }
329

    
330
///////////////////////////////////////////////////////////////////////////////////////////////////
331

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

    
348
///////////////////////////////////////////////////////////////////////////////////////////////////
349
// we do want to notify Listeners if they called 'abortAll' themselves but don't want to notify
350
// them if it is the library itself which is releasing resources.
351

    
352
  public synchronized int removeAll(boolean notify)
353
    {
354
    mJobs.add(new Job(DETALL,0,notify,null));
355
    InternalMaster.newSlave(this);
356
    mNumEffectsToBe = 0;
357
    return mNumEffects;
358
    }
359

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

    
372
    return false;
373
    }
374

    
375
///////////////////////////////////////////////////////////////////////////////////////////////////
376

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

    
387
    return false;
388
    }
389

    
390
///////////////////////////////////////////////////////////////////////////////////////////////////
391

    
392
  public int getNumEffects()
393
    {
394
    return mNumEffects;
395
    }
396

    
397
///////////////////////////////////////////////////////////////////////////////////////////////////
398

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

    
412
///////////////////////////////////////////////////////////////////////////////////////////////////
413

    
414
  public void doWork()
415
    {
416
    boolean changed = false;
417
    int num = mJobs.size();
418
    Job job;
419

    
420
    for(int i=0; i<num; i++)
421
      {
422
      job = mJobs.remove(0);
423

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

    
437
                     break;
438
        case ATTACH: if( mMax[mIndex]>mNumEffects ) // it is possible that we have first
439
                       {                            // added effects and then lowered mMax
440
                       int position = job.num;
441

    
442
                       if( position==-1 )
443
                         {
444
                         mCurrentDuration[mNumEffects] = 0;
445
                         mEffects[mNumEffects]    = job.effect;
446
                         mName[mNumEffects]       = job.effect.getName().ordinal();
447
                         mAssociation[mNumEffects]= job.effect.getAssociation();
448

    
449
                         mNumEffects++;
450
                         changed = true;
451
                         }
452
                       else if( position>=0 && position<=mNumEffects )
453
                         {
454
                         for(int j=mNumEffects; j>position; j--)
455
                           {
456
                           mCurrentDuration[j] = mCurrentDuration[j-1];
457
                           mEffects[j]         = mEffects[j-1];
458
                           mName[j]            = mName[j-1];
459
                           mAssociation[j]     = mAssociation[j-1];
460
                           }
461

    
462
                         mCurrentDuration[position] = 0;
463
                         mEffects[position]    = job.effect;
464
                         mName[position]       = job.effect.getName().ordinal();
465
                         mAssociation[position]= job.effect.getAssociation();
466

    
467
                         mNumEffects++;
468
                         changed = true;
469
                         }
470
                       }
471
                     else
472
                       {
473
                       android.util.Log.e("queue", "failed to add effect "+job.effect.getName());
474
                       }
475
                     break;
476
        case DETACH: for(int j=0; j<mNumEffects; j++)
477
                       {
478
                       if (mEffects[j] == job.effect)
479
                         {
480
                         remove(j);
481
                         changed = true;
482
                         break;
483
                         }
484
                       }
485
                     break;
486
        case DETALL: for(int j=0; j<mNumEffects; j++ )
487
                       {
488
                       changed = true;
489
                       mEffects[j] = null;
490
                       }
491

    
492
                     mNumEffects= 0;
493
                     break;
494
        }
495
      }
496

    
497
    if( changed && mIndex==EffectType.POSTPROCESS.ordinal() ) regenerateID();
498
    }
499
  }
(1-1/5)