Project

General

Profile

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

library / src / main / java / org / distorted / library / effectqueue / EffectQueue.java @ 0273ef2a

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
  Effect[] mEffects;
52
  int[] mName;
53
  int[] mAndAssociation;
54
  int[] mEquAssociation;
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
// this is not thread safe! The 'source' might change while we're copying it.
106

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

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

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

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

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

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

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

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

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170

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

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

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

    
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190

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

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

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

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

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

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

    
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236

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

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

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

    
252
    return false;
253
    }
254

    
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256

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

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

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

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

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

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

    
286
    mEffects[mNumEffects] = null;
287
    }
288

    
289
///////////////////////////////////////////////////////////////////////////////////////////////////
290

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

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

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

    
310
    return ret;
311
    }
312

    
313
///////////////////////////////////////////////////////////////////////////////////////////////////
314

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

    
328
    return 0;
329
    }
330

    
331
///////////////////////////////////////////////////////////////////////////////////////////////////
332

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

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

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

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

    
373
    return false;
374
    }
375

    
376
///////////////////////////////////////////////////////////////////////////////////////////////////
377

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

    
388
    return false;
389
    }
390

    
391
///////////////////////////////////////////////////////////////////////////////////////////////////
392

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

    
398
///////////////////////////////////////////////////////////////////////////////////////////////////
399

    
400
  public int getNumEffectsToBe()
401
    {
402
    return mNumEffectsToBe;
403
    }
404

    
405
///////////////////////////////////////////////////////////////////////////////////////////////////
406

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

    
420
///////////////////////////////////////////////////////////////////////////////////////////////////
421

    
422
  public void doWork()
423
    {
424
    boolean changed = false;
425
    int num = mJobs.size();
426
    Job job;
427

    
428
    for(int i=0; i<num; i++)
429
      {
430
      job = mJobs.remove(0);
431

    
432
      switch(job.type)
433
        {
434
        case CREATE: int max = mMax[mIndex];
435
                     if( max>0 )
436
                       {
437
                       mUniforms        = new float[max*job.num];
438
                       mEffects         = new Effect[max];
439
                       mName            = new int[max];
440
                       mAndAssociation  = new int[max];
441
                       mEquAssociation  = new int[max];
442
                       }
443
                     mCreated = true;
444

    
445
                     break;
446
        case ATTACH: if( mMax[mIndex]>mNumEffects ) // it is possible that we have first
447
                       {                            // added effects and then lowered mMax
448
                       int position = job.num;
449

    
450
                       if( position==-1 )
451
                         {
452
                         mEffects[mNumEffects]        = job.effect;
453
                         mName[mNumEffects]           = job.effect.getName().ordinal();
454
                         mAndAssociation[mNumEffects] = job.effect.getAndAssociation();
455
                         mEquAssociation[mNumEffects] = job.effect.getEquAssociation();
456

    
457
                         mNumEffects++;
458
                         changed = true;
459
                         }
460
                       else if( position>=0 && position<=mNumEffects )
461
                         {
462
                         for(int j=mNumEffects; j>position; j--)
463
                           {
464
                           mEffects[j]         = mEffects[j-1];
465
                           mName[j]            = mName[j-1];
466
                           mAndAssociation[j]  = mAndAssociation[j-1];
467
                           mEquAssociation[j]  = mEquAssociation[j-1];
468
                           }
469

    
470
                         mEffects[position]        = job.effect;
471
                         mName[position]           = job.effect.getName().ordinal();
472
                         mAndAssociation[position] = job.effect.getAndAssociation();
473
                         mEquAssociation[position] = job.effect.getEquAssociation();
474

    
475
                         mNumEffects++;
476
                         changed = true;
477
                         }
478
                       }
479
                     else
480
                       {
481
                       android.util.Log.e("queue", "failed to add effect "+job.effect.getName());
482
                       }
483
                     break;
484
        case DETACH: for(int j=0; j<mNumEffects; j++)
485
                       {
486
                       if (mEffects[j] == job.effect)
487
                         {
488
                         remove(j);
489
                         changed = true;
490
                         break;
491
                         }
492
                       }
493
                     break;
494
        case DETALL: for(int j=0; j<mNumEffects; j++ )
495
                       {
496
                       changed = true;
497
                       mEffects[j] = null;
498
                       }
499

    
500
                     mNumEffects= 0;
501
                     break;
502
        }
503
      }
504

    
505
    if( changed && mIndex==EffectType.POSTPROCESS.ordinal() ) regenerateID();
506
    }
507
  }
(1-1/5)