Project

General

Profile

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

library / src / main / java / org / distorted / library / effectqueue / EffectQueue.java @ b7074bc6

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 = 3; // Number of Main program variants (ATM 3: MAIN, MAIN OIT, PREPROCESS)
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
  long[] mCurrentDuration;
51
  Effect[] mEffects;
52
  int[] mName;
53
  long mTime=0;
54

    
55
  private 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 class Job
64
    {
65
    int type;
66
    int num;
67
    boolean notify;
68
    Effect effect;
69

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

    
79
  private ArrayList<Job> mJobs = new ArrayList<>();
80

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

    
96
    mJobs.add(new Job(CREATE,numUniforms,false,null));  // create the stuff that depends on max number
97
    InternalMaster.newSlave(this);                     // of uniforms later, on first render.
98
    }
99

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101

    
102
  public static void allocateQueues(EffectQueue[] queues, EffectQueue[] from, int flags)
103
    {
104
    queues[0] = (flags & DistortedLibrary.CLONE_MATRIX     ) != 0 ? from[0] : new EffectQueueMatrix();
105
    queues[1] = (flags & DistortedLibrary.CLONE_VERTEX     ) != 0 ? from[1] : new EffectQueueVertex();
106
    queues[2] = (flags & DistortedLibrary.CLONE_FRAGMENT   ) != 0 ? from[2] : new EffectQueueFragment();
107
    queues[3] = (flags & DistortedLibrary.CLONE_POSTPROCESS) != 0 ? from[3] : new EffectQueuePostprocess();
108
    }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

    
112
  public static void compute(EffectQueue[] queues, long currTime)
113
    {
114
    ((EffectQueueMatrix     )queues[0]).compute(currTime);
115
    ((EffectQueueVertex     )queues[1]).compute(currTime);
116
    ((EffectQueueFragment   )queues[2]).compute(currTime);
117
    ((EffectQueuePostprocess)queues[3]).compute(currTime);
118
    }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

    
122
  public static void send(EffectQueue[] queues, float distance, float mipmap,
123
                          float[] projection, float inflate, MeshBase mesh, int variant )
124
    {
125
    ((EffectQueueMatrix  )queues[0]).send(distance, mipmap, projection, mesh, variant);
126
    ((EffectQueueVertex  )queues[1]).send(inflate, variant);
127
    ((EffectQueueFragment)queues[2]).send(variant);
128
    }
129

    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131

    
132
  public static float[] getMVP(EffectQueue[] queues)
133
    {
134
    return ((EffectQueueMatrix)queues[0]).getMVP();
135
    }
136

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138

    
139
  public static void getUniforms(int programH, int variant)
140
    {
141
    EffectQueueFragment.uniforms(programH,variant);
142
    EffectQueueVertex  .uniforms(programH,variant);
143
    EffectQueueMatrix  .uniforms(programH,variant);
144
    }
145

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

    
151
  private void regenerateID()
152
    {
153
    if( mNumEffects>0 )
154
      {
155
      ArrayList<Long> list = new ArrayList<>();
156
      for (int i = 0; i < mNumEffects; i++) list.add(mEffects[i].getID());
157
      Long id = mMapID.get(list);
158

    
159
      if( id!=null )
160
        {
161
        mID = id;
162
        }
163
      else
164
        {
165
        mMapID.put(list,mNextID);
166
        mID = mNextID++;
167
        }
168
      }
169
    else
170
      {
171
      mID = 0;
172
      }
173
    }
174

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176

    
177
  public long getID()
178
    {
179
    return mID;
180
    }
181

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

    
184
  public static boolean setMax(int index, int m)
185
    {
186
    if( !DistortedLibrary.isInitialized() || m<=mMax[index] )
187
      {
188
      mMax[index] = m<0 ? 0:m;
189
      return true;
190
      }
191

    
192
    return false;
193
    }
194

    
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196

    
197
  public static int getMax(int index)
198
    {
199
    return mMax[index];
200
    }
201

    
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203

    
204
  public static void onDestroy()
205
    {
206
    mNextID = 1;
207
    mMapID.clear();
208
    EffectType.reset(mMax);
209
    }
210

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212
// this assumes 0<=effect<mNumEffects
213

    
214
  protected void remove(int effect)
215
    {
216
    mNumEffects--;
217

    
218
    for(int j=effect; j<mNumEffects; j++ )
219
      {
220
      mEffects[j]         = mEffects[j+1];
221
      mCurrentDuration[j] = mCurrentDuration[j+1];
222
      mName[j]            = mName[j+1];
223
      }
224

    
225
    mEffects[mNumEffects] = null;
226
    }
227

    
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229

    
230
  public synchronized int removeByName(EffectName name)
231
    {
232
    int ret = 0;
233

    
234
    for(int i=0; i<mNumEffects; i++)
235
      {
236
      if( mEffects[i].getName() == name )
237
        {
238
        mJobs.add(new Job(DETACH,0,true,mEffects[i]));
239
        ret++;
240
        }
241
      }
242

    
243
    if( ret>0 )
244
      {
245
      InternalMaster.newSlave(this);
246
      mNumEffectsToBe-=ret;
247
      }
248

    
249
    return ret;
250
    }
251

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253

    
254
  public synchronized int removeById(long id)
255
    {
256
    for(int i=0; i<mNumEffects; i++)
257
      {
258
      if( mEffects[i].getID() == id )
259
        {
260
        mJobs.add(new Job(DETACH,0,true,mEffects[i]));
261
        InternalMaster.newSlave(this);
262
        mNumEffectsToBe--;
263
        return 1;
264
        }
265
      }
266

    
267
    return 0;
268
    }
269

    
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271

    
272
  public synchronized int removeEffect(Effect effect)
273
    {
274
    for(int i=0; i<mNumEffects; i++)
275
      {
276
      if( mEffects[i]==effect )
277
        {
278
        mJobs.add(new Job(DETACH,0,true,mEffects[i]));
279
        InternalMaster.newSlave(this);
280
        mNumEffectsToBe--;
281
        return 1;
282
        }
283
      }
284
   
285
    return 0;
286
    }
287

    
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289
// we do want to notify Listeners if they called 'abortAll' themselves but don't want to notify
290
// them if it is the library itself which is releasing resources.
291

    
292
  public synchronized int abortAll(boolean notify)
293
    {
294
    mJobs.add(new Job(DETALL,0,notify,null));
295
    InternalMaster.newSlave(this);
296
    mNumEffectsToBe = 0;
297
    return mNumEffects;
298
    }
299

    
300
///////////////////////////////////////////////////////////////////////////////////////////////////
301
  
302
  public boolean add(Effect effect)
303
    {
304
    if( mMax[mIndex]>mNumEffectsToBe || !mCreated )
305
      {
306
      mJobs.add(new Job(ATTACH,-1,false,effect));
307
      InternalMaster.newSlave(this);
308
      mNumEffectsToBe++;
309
      return true;
310
      }
311

    
312
    return false;
313
    }
314

    
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316

    
317
  public boolean add(Effect effect, int position)
318
    {
319
    if( mMax[mIndex]>mNumEffectsToBe || !mCreated )
320
      {
321
      mJobs.add(new Job(ATTACH,position,false,effect));
322
      InternalMaster.newSlave(this);
323
      mNumEffectsToBe++;
324
      return true;
325
      }
326

    
327
    return false;
328
    }
329

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

    
332
  public int getNumEffects()
333
    {
334
    return mNumEffects;
335
    }
336

    
337
///////////////////////////////////////////////////////////////////////////////////////////////////
338

    
339
  public Effect getEffect(int position)
340
    {
341
    if( position>=0 && position< mNumEffects )
342
      {
343
      return mEffects[position];
344
      }
345
    else
346
      {
347
      android.util.Log.e("queue", "getEffect: out of range "+position);
348
      return null;
349
      }
350
    }
351

    
352
///////////////////////////////////////////////////////////////////////////////////////////////////
353

    
354
  public void doWork()
355
    {
356
    boolean changed = false;
357
    int num = mJobs.size();
358
    Job job;
359

    
360
    for(int i=0; i<num; i++)
361
      {
362
      job = mJobs.remove(0);
363

    
364
      switch(job.type)
365
        {
366
        case CREATE: int max = mMax[mIndex];
367
                     if( max>0 )
368
                       {
369
                       mUniforms        = new float[max*job.num];
370
                       mCurrentDuration = new long[max];
371
                       mEffects         = new Effect[max];
372
                       mName            = new int[max];
373
                       }
374
                     mCreated = true;
375

    
376
                     break;
377
        case ATTACH: if( mMax[mIndex]>mNumEffects ) // it is possible that we have first
378
                       {                            // added effects and then lowered mMax
379
                       int position = job.num;
380

    
381
                       if( position==-1 )
382
                         {
383
                         mCurrentDuration[mNumEffects] = 0;
384
                         mEffects[mNumEffects] = job.effect;
385
                         mName[mNumEffects] = job.effect.getName().ordinal();
386

    
387
                         mNumEffects++;
388
                         changed = true;
389
                         }
390
                       else if( position>=0 && position<=mNumEffects )
391
                         {
392
                         for(int j=mNumEffects; j>position; j--)
393
                           {
394
                           mCurrentDuration[j] = mCurrentDuration[j-1];
395
                           mEffects[j]         = mEffects[j-1];
396
                           mName[j]            = mName[j-1];
397
                           }
398

    
399
                         mCurrentDuration[position] = 0;
400
                         mEffects[position] = job.effect;
401
                         mName[position] = job.effect.getName().ordinal();
402

    
403
                         mNumEffects++;
404
                         changed = true;
405
                         }
406
                       }
407
                     else
408
                       {
409
                       android.util.Log.e("queue", "failed to add effect "+job.effect.getName());
410
                       }
411
                     break;
412
        case DETACH: for(int j=0; j<mNumEffects; j++)
413
                       {
414
                       if (mEffects[j] == job.effect)
415
                         {
416
                         remove(j);
417
                         changed = true;
418
                         break;
419
                         }
420
                       }
421
                     break;
422
        case DETALL: for(int j=0; j<mNumEffects; j++ )
423
                       {
424
                       changed = true;
425
                       mEffects[j] = null;
426
                       }
427

    
428
                     mNumEffects= 0;
429
                     break;
430
        }
431
      }
432

    
433
    if( changed && mIndex==EffectType.POSTPROCESS.ordinal() ) regenerateID();
434
    }
435
  }
(1-1/5)