Project

General

Profile

« Previous | Next » 

Revision 2f1f7570

Added by Leszek Koltunski about 6 years ago

Properly clean up static varaibles in the Effect classes!

View differences:

src/main/java/org/distorted/library/effect/Effect.java
59 59

  
60 60
    int n = name.ordinal();
61 61
    float[] u = name.getUnity();
62
    int l = name.getUnity().length;
62
    int l = u.length;
63 63

  
64 64
    for(int i=0; i<l; i++)
65 65
      {
......
82 82
    mNextID = 0;
83 83

  
84 84
    for(int i=0; i<NUM_EFFECTS; i++) mEnabled[i] = false;
85

  
86
    MatrixEffect.destroyStatics();
87
    VertexEffect.destroyStatics();
88
    PostprocessEffect.destroyStatics();
85 89
    }
86 90

  
87 91
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/library/effect/EffectName.java
43 43
public enum EffectName
44 44
  {
45 45
  // EFFECT NAME /////// EFFECT TYPE ////////// EFFECT UNITY //////////// DIM REGION CENTER
46
  ROTATE           ( EffectType.MATRIX  ,   new float[] {0.0f}           , 4, false, true  ),
47
  QUATERNION       ( EffectType.MATRIX  ,   new float[] {0.0f,0.0f,0.0f} , 4, false, true  ),
48
  MOVE             ( EffectType.MATRIX  ,   new float[] {0.0f,0.0f,0.0f} , 3, false, false ),
49
  SCALE            ( EffectType.MATRIX  ,   new float[] {1.0f,1.0f,1.0f} , 3, false, false ),
50
  SHEAR            ( EffectType.MATRIX  ,   new float[] {0.0f,0.0f,0.0f} , 3, false, true  ),
51

  
52
  DISTORT          ( EffectType.VERTEX  ,   new float[] {0.0f,0.0f,0.0f} , 3, true , true  ),
53
  DEFORM           ( EffectType.VERTEX  ,   new float[] {0.0f,0.0f,0.0f} , 3, true , true  ),
54
  SINK             ( EffectType.VERTEX  ,   new float[] {1.0f}           , 1, true , true  ),
55
  PINCH            ( EffectType.VERTEX  ,   new float[] {1.0f}           , 2, true , true  ),
56
  SWIRL            ( EffectType.VERTEX  ,   new float[] {0.0f}           , 1, true , true  ),
57
  WAVE             ( EffectType.VERTEX  ,   new float[] {0.0f}           , 5, true , true  ),
58

  
59
  ALPHA            ( EffectType.FRAGMENT,   new float[] {1.0f}           , 1, true , false ),
60
  SMOOTH_ALPHA     ( EffectType.FRAGMENT,   new float[] {1.0f}           , 1, true , false ),
61
  CHROMA           ( EffectType.FRAGMENT,   new float[] {0.0f}           , 4, true , false ),
62
  SMOOTH_CHROMA    ( EffectType.FRAGMENT,   new float[] {0.0f}           , 4, true , false ),
63
  BRIGHTNESS       ( EffectType.FRAGMENT,   new float[] {1.0f}           , 1, true , false ),
64
  SMOOTH_BRIGHTNESS( EffectType.FRAGMENT,   new float[] {1.0f}           , 1, true , false ),
65
  SATURATION       ( EffectType.FRAGMENT,   new float[] {1.0f}           , 1, true , false ),
66
  SMOOTH_SATURATION( EffectType.FRAGMENT,   new float[] {1.0f}           , 1, true , false ),
67
  CONTRAST         ( EffectType.FRAGMENT,   new float[] {1.0f}           , 1, true , false ),
68
  SMOOTH_CONTRAST  ( EffectType.FRAGMENT,   new float[] {1.0f}           , 1, true , false ),
69

  
70
  BLUR             ( EffectType.POSTPROCESS,new float[] {0.0f}           , 1, false, false ),
71
  GLOW             ( EffectType.POSTPROCESS,new float[] {0.0f}           , 5, false, false );
46
  ROTATE           ( EffectType.MATRIX  ,   new float[] {0.0f}           , 4, false, true  , MatrixEffectRotate.class       ),
47
  QUATERNION       ( EffectType.MATRIX  ,   new float[] {0.0f,0.0f,0.0f} , 4, false, true  , MatrixEffectQuaternion.class   ),
48
  MOVE             ( EffectType.MATRIX  ,   new float[] {0.0f,0.0f,0.0f} , 3, false, false , MatrixEffectMove.class         ),
49
  SCALE            ( EffectType.MATRIX  ,   new float[] {1.0f,1.0f,1.0f} , 3, false, false , MatrixEffectScale.class        ),
50
  SHEAR            ( EffectType.MATRIX  ,   new float[] {0.0f,0.0f,0.0f} , 3, false, true  , MatrixEffectShear.class        ),
51

  
52
  DISTORT          ( EffectType.VERTEX  ,   new float[] {0.0f,0.0f,0.0f} , 3, true , true  , VertexEffectDistort.class      ),
53
  DEFORM           ( EffectType.VERTEX  ,   new float[] {0.0f,0.0f,0.0f} , 3, true , true  , VertexEffectDeform.class       ),
54
  SINK             ( EffectType.VERTEX  ,   new float[] {1.0f}           , 1, true , true  , VertexEffectSink.class         ),
55
  PINCH            ( EffectType.VERTEX  ,   new float[] {1.0f}           , 2, true , true  , VertexEffectPinch.class        ),
56
  SWIRL            ( EffectType.VERTEX  ,   new float[] {0.0f}           , 1, true , true  , VertexEffectSwirl.class        ),
57
  WAVE             ( EffectType.VERTEX  ,   new float[] {0.0f}           , 5, true , true  , VertexEffectWave.class         ),
58

  
59
  ALPHA            ( EffectType.FRAGMENT,   new float[] {1.0f}           , 1, true , false , FragmentEffectAlpha.class      ),
60
  SMOOTH_ALPHA     ( EffectType.FRAGMENT,   new float[] {1.0f}           , 1, true , false , FragmentEffectAlpha.class      ),
61
  CHROMA           ( EffectType.FRAGMENT,   new float[] {0.0f}           , 4, true , false , FragmentEffectChroma.class     ),
62
  SMOOTH_CHROMA    ( EffectType.FRAGMENT,   new float[] {0.0f}           , 4, true , false , FragmentEffectChroma.class     ),
63
  BRIGHTNESS       ( EffectType.FRAGMENT,   new float[] {1.0f}           , 1, true , false , FragmentEffectBrightness.class ),
64
  SMOOTH_BRIGHTNESS( EffectType.FRAGMENT,   new float[] {1.0f}           , 1, true , false , FragmentEffectBrightness.class ),
65
  SATURATION       ( EffectType.FRAGMENT,   new float[] {1.0f}           , 1, true , false , FragmentEffectSaturation.class ),
66
  SMOOTH_SATURATION( EffectType.FRAGMENT,   new float[] {1.0f}           , 1, true , false , FragmentEffectSaturation.class ),
67
  CONTRAST         ( EffectType.FRAGMENT,   new float[] {1.0f}           , 1, true , false , FragmentEffectContrast.class   ),
68
  SMOOTH_CONTRAST  ( EffectType.FRAGMENT,   new float[] {1.0f}           , 1, true , false , FragmentEffectContrast.class   ),
69

  
70
  BLUR             ( EffectType.POSTPROCESS,new float[] {0.0f}           , 1, false, false , PostprocessEffectBlur.class    ),
71
  GLOW             ( EffectType.POSTPROCESS,new float[] {0.0f}           , 5, false, false , PostprocessEffectGlow.class    );
72 72

  
73 73
///////////////////////////////////////////////////////////////////////////////////////////////////
74 74

  
75
  private static final int MAXDIM = 4;  // maximum supported dimension of effect's unity
76 75
  public static final int LENGTH = values().length;
77 76

  
78 77
  private final EffectType type;
......
80 79
  private final int dimension;
81 80
  private final boolean supportsR;
82 81
  private final boolean supportsC;
82
  private final Class<? extends Effect> effectClass;
83 83

  
84
  private static final float[] unities;
85
  private static final int[] unityDimensions;
86 84
  private static final int[] dimensions;
87 85
  private static final boolean[] supportsRegion;
88 86
  private static final boolean[] supportsCenter;
......
92 90
    {
93 91
    int i=0;
94 92

  
95
    unities         = new float[MAXDIM*LENGTH];
96
    unityDimensions = new int[LENGTH];
97 93
    dimensions      = new int[LENGTH];
98 94
    supportsRegion  = new boolean[LENGTH];
99 95
    supportsCenter  = new boolean[LENGTH];
......
101 97

  
102 98
    for(EffectName name: EffectName.values())
103 99
      {
104
      unityDimensions[i] = (name.unity==null ? 0 : name.unity.length);
105 100
      dimensions[i]      = name.dimension;
106 101
      supportsRegion[i]  = name.supportsR;
107 102
      supportsCenter[i]  = name.supportsC;
108 103
      names[i]           = name;
109 104

  
110
      switch(unityDimensions[i])
111
        {
112
        case 4: unities[MAXDIM*i+3] = name.unity[3];
113
        case 3: unities[MAXDIM*i+2] = name.unity[2];
114
        case 2: unities[MAXDIM*i+1] = name.unity[1];
115
        case 1: unities[MAXDIM*i  ] = name.unity[0];
116
        case 0: break;
117
        }
118

  
119 105
      i++;
120 106
      }
121 107
    }
......
129 115

  
130 116
///////////////////////////////////////////////////////////////////////////////////////////////////
131 117

  
132
  EffectName(EffectType type, float[] unity, int dimension, boolean supportsR, boolean supportsC)
118
  EffectName(EffectType type, float[] unity, int dimension, boolean supportsR,
119
             boolean supportsC, Class<? extends Effect> effectClass )
133 120
    {
134
    this.type      = type;
135
    this.unity     = unity;
136
    this.dimension = dimension;
137
    this.supportsR = supportsR;
138
    this.supportsC = supportsC;
121
    this.type        = type;
122
    this.unity       = unity;
123
    this.dimension   = dimension;
124
    this.supportsR   = supportsR;
125
    this.supportsC   = supportsC;
126
    this.effectClass = effectClass;
139 127
    }
140 128

  
141 129
///////////////////////////////////////////////////////////////////////////////////////////////////
......
151 139
    return type;
152 140
    }
153 141

  
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143
  /**
144
   * Returns the Class of an individual Effect. For example, EffectName.ROTATION.getType() will
145
   * return MatrixEffectRotation.class.
146
   * @return effect class.
147
   */
148
  public Class<? extends Effect> getEffectClass()
149
    {
150
    return effectClass;
151
    }
152

  
154 153
///////////////////////////////////////////////////////////////////////////////////////////////////
155 154
/**
156 155
 * Returns the i-th EffectName.
src/main/java/org/distorted/library/effect/FragmentEffect.java
79 79
    }
80 80

  
81 81
///////////////////////////////////////////////////////////////////////////////////////////////////
82
/**
83
 * Only for use by the library itself.
84
 *
85
 * @y.exclude
86
 */
87
  public static void onDestroy()
82

  
83
  static void destroyStatics()
88 84
    {
89 85
    mNumEnabled = 0;
90 86
    mGLSL = "";
src/main/java/org/distorted/library/effect/MatrixEffect.java
38 38
 */
39 39
  public abstract void apply(float[] matrix, float[] uniforms, int index);
40 40

  
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

  
43
  static void destroyStatics()
44
    {
45

  
46
    }
47

  
41 48
///////////////////////////////////////////////////////////////////////////////////////////////////
42 49

  
43 50
  MatrixEffect(EffectName name)
src/main/java/org/distorted/library/effect/PostprocessEffect.java
23 23
import org.distorted.library.main.DistortedMaster;
24 24
import org.distorted.library.program.DistortedProgram;
25 25

  
26
import java.lang.reflect.Method;
26 27
import java.nio.ByteBuffer;
27 28
import java.nio.ByteOrder;
28 29
import java.nio.FloatBuffer;
......
188 189
      }
189 190
    }
190 191

  
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193

  
194
  static void destroyStatics()
195
    {
196
    mPrograms.clear();
197
    mSources.clear();
198
    mNumSources = 0;
199

  
200
    Method method=null;
201

  
202
    for(EffectName name: EffectName.values())
203
      {
204
      if( name.getType() == EffectType.POSTPROCESS )
205
        {
206
        Class<? extends Effect> cls = name.getEffectClass();
207

  
208
        try
209
          {
210
          method = cls.getMethod("destroyStatics");
211
          }
212
        catch(NoSuchMethodException ex)
213
          {
214
          android.util.Log.e("postprocess", "exception getting method: "+ex.getMessage());
215
          }
216

  
217
        try
218
          {
219
          method.invoke(null);
220
          }
221
        catch(Exception ex)
222
          {
223
          android.util.Log.e("postprocess", "exception invoking method: "+ex.getMessage());
224
          }
225
        }
226
      }
227
    }
228

  
191 229
///////////////////////////////////////////////////////////////////////////////////////////////////
192 230

  
193 231
  PostprocessEffect(EffectName name)
src/main/java/org/distorted/library/effect/PostprocessEffectBlur.java
66 66
  private static DistortedProgram mProgram1, mProgram2;
67 67
  private static int mIndex1, mIndex2;
68 68

  
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70
// Clean up of static variables on exit.
71
// called by reflection from super class.
72

  
73
  @SuppressWarnings("unused")
74
  static void destroyStatics()
75
    {
76
    mProgram1 = null;
77
    mProgram2 = null;
78

  
79
    mIndex1 = 0;
80
    mIndex2 = 0;
81
    }
82

  
69 83
///////////////////////////////////////////////////////////////////////////////////////////////////
70 84
// This implements the 'Better separable implementation using GPU fixed function sampling' from
71 85
// https://software.intel.com/en-us/blogs/2014/07/15/an-investigation-of-fast-real-time-gpu-based-image-blur-algorithms
src/main/java/org/distorted/library/effect/PostprocessEffectGlow.java
67 67
  private static DistortedProgram mProgram1, mProgram2;
68 68
  private static int mIndex1, mIndex2;
69 69

  
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71
// Clean up of static variables on exit.
72
// called by reflection from super class.
73

  
74
  @SuppressWarnings("unused")
75
  static void destroyStatics()
76
    {
77
    mProgram1 = null;
78
    mProgram2 = null;
79

  
80
    mIndex1 = 0;
81
    mIndex2 = 0;
82
    }
83

  
70 84
///////////////////////////////////////////////////////////////////////////////////////////////////
71 85
// This implements the 'Better separable implementation using GPU fixed function sampling' from
72 86
// https://software.intel.com/en-us/blogs/2014/07/15/an-investigation-of-fast-real-time-gpu-based-image-blur-algorithms
src/main/java/org/distorted/library/effect/VertexEffect.java
72 72
    }
73 73

  
74 74
///////////////////////////////////////////////////////////////////////////////////////////////////
75
/**
76
 * Only for use by the library itself.
77
 *
78
 * @y.exclude
79
 */
80
  public static void onDestroy()
75

  
76
  static void destroyStatics()
81 77
    {
82 78
    mNumEnabled = 0;
83 79
    mGLSL = "";
src/main/java/org/distorted/library/main/Distorted.java
239 239
    DistortedOutputSurface.onDestroy();
240 240
    EffectQueue.onDestroy();
241 241
    Effect.onDestroy();
242
    VertexEffect.onDestroy();
243
    FragmentEffect.onDestroy();
244 242
    EffectMessageSender.stopSending();
245 243

  
246 244
    mInitialized = false;
src/main/java/org/distorted/library/main/EffectQueueMatrix.java
103 103
    float xLenInPixels = projection.mWidth *(mMaxX-mMinX)/2;
104 104
    float yLenInPixels = projection.mHeight*(mMaxY-mMinY)/2;
105 105

  
106
    // already margin / min(xLen,yLen) is the size of the halo.
106
    // already margin / avg(xLen,yLen) is the size of the halo.
107 107
    // Here we need a bit more because we are marking not only the halo, but a little bit around
108 108
    // it as well so that the (blur for example) will be smooth on the edges. Thus the 2.0f.
109
    // ( 4.0 because there is an extra 2.0 from the avg(xLen,yLen) )
109 110
    //
110 111
    // mMipmap ( 1.0, 0.5, 0.25, 0.125 ) - we need to make the size of the halo independent
111 112
    // of postprocessing effect quality.
112 113

  
113
    return projection.mMipmap*2.0f*marginInPixels/( xLenInPixels>yLenInPixels ? yLenInPixels:xLenInPixels );
114
    return projection.mMipmap*4.0f*marginInPixels/( xLenInPixels+yLenInPixels );
114 115
    }
115 116

  
116 117
///////////////////////////////////////////////////////////////////////////////////////////////////

Also available in: Unified diff