Project

General

Profile

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

library / src / main / java / org / distorted / library / effectqueue / EffectQueue.java @ 24804c15

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
269

    
270
  protected void remove(int effect)
271
    {
272
    if( mNumEffects>effect )
273
      {
274
      mNumEffects--;
275
      System.arraycopy(mEffects, effect+1, mEffects, effect, mNumEffects-effect);
276
      System.arraycopy(mIntUniforms, mNumIntUniforms*(effect+1), mIntUniforms, mNumIntUniforms*effect, mNumIntUniforms*(mNumEffects-effect) );
277
      mEffects[mNumEffects] = null;
278
      }
279
    }
280

    
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282

    
283
  public synchronized int removeByName(EffectName name)
284
    {
285
    int ret = 0;
286

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

    
296
    if( ret>0 )
297
      {
298
      InternalMaster.newSlave(this);
299
      mNumEffectsToBe-=ret;
300
      }
301

    
302
    return ret;
303
    }
304

    
305
///////////////////////////////////////////////////////////////////////////////////////////////////
306

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

    
320
    return 0;
321
    }
322

    
323
///////////////////////////////////////////////////////////////////////////////////////////////////
324

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

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

    
345
  public synchronized int removeAll(boolean notify)
346
    {
347
    mJobs.add(new Job(DETALL,0,0,notify,null));
348
    InternalMaster.newSlave(this);
349
    mNumEffectsToBe = 0;
350
    return mNumEffects;
351
    }
352

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

    
365
    return false;
366
    }
367

    
368
///////////////////////////////////////////////////////////////////////////////////////////////////
369

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

    
380
    return false;
381
    }
382

    
383
///////////////////////////////////////////////////////////////////////////////////////////////////
384

    
385
  public int getNumEffects()
386
    {
387
    return mNumEffects;
388
    }
389

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

    
392
  public int getNumEffectsToBe()
393
    {
394
    return mNumEffectsToBe;
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
                       mEffects       = new Effect[max];
430
                       mFloatUniforms = new float[max*job.num1];
431
                       mIntUniforms   = new int[max*job.num2];
432
                       }
433
                     mCreated = true;
434

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

    
440
                       if( position==-1 )
441
                         {
442
                         mEffects[mNumEffects]                     = job.effect;
443
                         mIntUniforms[mNumIntUniforms*mNumEffects] = job.effect.getName().ordinal();
444

    
445
                         mNumEffects++;
446
                         changed = true;
447
                         }
448
                       else if( position>=0 && position<=mNumEffects )
449
                         {
450
                         System.arraycopy(mEffects    , position, mEffects    , position+1, mNumEffects-position);
451
                         System.arraycopy(mIntUniforms, mNumIntUniforms*position, mIntUniforms, mNumIntUniforms*(position+1), mNumIntUniforms*(mNumEffects-position) );
452

    
453
                         mEffects[position]                     = job.effect;
454
                         mIntUniforms[mNumIntUniforms*position] = job.effect.getName().ordinal();
455

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

    
481
                     mNumEffects= 0;
482
                     break;
483
        }
484
      }
485

    
486
    if( changed && mIndex==EffectType.POSTPROCESS.ordinal() ) regenerateID();
487
    }
488
  }
(1-1/5)