Project

General

Profile

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

library / src / main / java / org / distorted / library / effectqueue / EffectQueue.java @ 96e3b88a

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

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

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

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

    
46
  long mTime;
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
  private int mNumFloatUniforms, mNumIntUniforms;
50
  Effect[] mEffects;
51

    
52
  float[] mFloatUniforms;
53
  int[] mIntUniforms;
54

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

    
63
  private static class Job
64
    {
65
    int type;
66
    int num1, num2;
67
    boolean notify;
68
    Effect effect;
69

    
70
    Job(int t, int m1, int m2, boolean n, Effect e)
71
      {
72
      type  = t;
73
      num1  = m1;
74
      num2  = m2;
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 numFloatUniforms, int numIntUniforms, int index)
90
    {
91
    mCreated          = false;
92
    mTime             = 0;
93
    mID               = 0;
94
    mNumEffects       = 0;
95
    mNumEffectsToBe   = 0;
96
    mIndex            = index;
97
    mNumFloatUniforms = numFloatUniforms;
98
    mNumIntUniforms   = numIntUniforms;
99

    
100
    mJobs.add(new Job(CREATE,numFloatUniforms,numIntUniforms, 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
      mNumFloatUniforms = source.mNumFloatUniforms;
118
      mNumIntUniforms   = source.mNumIntUniforms;
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
      mNumFloatUniforms = source.mNumFloatUniforms;
139
      mNumIntUniforms   = source.mNumIntUniforms;
140

    
141
      int max = mMax[mIndex];
142

    
143
      if( max>0 )
144
        {
145
        mEffects       = new Effect[max];
146
        mFloatUniforms = new float[max*source.mNumFloatUniforms];
147
        mIntUniforms   = new int[max*source.mNumIntUniforms];
148

    
149
        if( mNumEffects>=0 )
150
          {
151
          System.arraycopy(source.mEffects, 0, mEffects, 0, mNumEffects);
152
          }
153

    
154
        if( mNumIntUniforms*mNumEffects>=0 )
155
          {
156
          System.arraycopy(source.mIntUniforms, 0, mIntUniforms, 0, mNumIntUniforms*mNumEffects);
157
          }
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, int variant )
186
    {
187
    ((EffectQueueMatrix  )queues[0]).send(distance, mipmap, projection, variant);
188
    ((EffectQueueVertex  )queues[1]).send(inflate, variant);
189
    ((EffectQueueFragment)queues[2]).send(variant);
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
    int max = mMax[mIndex];
275

    
276
    for(int i=effect; i<mNumEffects; i++ )
277
      {
278
      mEffects[i]= mEffects[i+1];
279

    
280
      for(int j=0; j<mNumIntUniforms; j++)
281
        {
282
        mIntUniforms[j*max + i] = mIntUniforms[j*max + i+1];
283
        }
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,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,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,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,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,0,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,0,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
                       mEffects       = new Effect[max];
438
                       mFloatUniforms = new float[max*job.num1];
439
                       mIntUniforms   = new int[max*job.num2];
440
                       }
441
                     mCreated = true;
442

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

    
448
                       if( position==-1 )
449
                         {
450
                         mEffects[mNumEffects]     = job.effect;
451
                         mIntUniforms[mNumEffects] = job.effect.getName().ordinal();
452

    
453
                         mNumEffects++;
454
                         changed = true;
455
                         }
456
                       else if( position>=0 && position<=mNumEffects )
457
                         {
458
                         for(int j=mNumEffects; j>position; j--)
459
                           {
460
                           mEffects[j]     = mEffects[j-1];
461
                           mIntUniforms[j] = mIntUniforms[j-1];
462
                           }
463

    
464
                         mEffects[position]     = job.effect;
465
                         mIntUniforms[position] = job.effect.getName().ordinal();
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)