Project

General

Profile

« Previous | Next » 

Revision 8e34674e

Added by Leszek Koltunski about 8 years ago

Move the *List classes into static members of DistortedFramebuffer and DistortedObject.

View differences:

src/main/java/org/distorted/library/Distorted.java
115 115
    Log.d(TAG, "Max vectors in vertex shader: "+maxV);
116 116
    Log.d(TAG, "Max vectors in fragment shader: "+maxF);
117 117
    
118
    if( Build.FINGERPRINT.contains("generic") == false )
118
    if( !Build.FINGERPRINT.contains("generic") )
119 119
      {
120 120
      int realMaxV = (maxV-11)/4;   // adjust this in case of changes to the shaders...
121 121
      int realMaxF = (maxF- 2)/4;   //
......
347 347
    GLES20.glEnableVertexAttribArray(mNormalH);
348 348
    GLES20.glEnableVertexAttribArray(mTextureCoordH);
349 349
   
350
    DistortedObjectList.reset();
350
    DistortedObject.reset();
351 351
    DistortedNode.reset();
352 352
    EffectMessageSender.startSending();
353 353
    }
......
373 373
  public static void onDestroy()
374 374
    {
375 375
    DistortedGridFactory.release();
376
    DistortedObjectList.release();
377
    DistortedFramebufferList.release();
376
    DistortedObject.release();
377
    DistortedFramebuffer.release();
378 378
    DistortedNode.release();
379
    EffectQueue.reset();
379
    EffectQueue.release();
380 380
    EffectMessageSender.stopSending();
381 381
   
382 382
    mInitialized = false;
src/main/java/org/distorted/library/DistortedFramebuffer.java
22 22
import android.opengl.GLES20;
23 23
import android.opengl.Matrix;
24 24

  
25
import java.util.Iterator;
26
import java.util.LinkedList;
27

  
25 28
///////////////////////////////////////////////////////////////////////////////////////////////////
26 29
/**
27 30
 * Class which represents a OpenGL Framebuffer object.
......
29 32
 * User is able to create either WRITE-only Framebuffers from objects already constructed outside
30 33
 * of the library (the first constructor; primary use case: the screen) or an offscreen READ-WRITE
31 34
 * FBOs (used by the DistortedNode, but also can be used by external users of the library)
35
 *
36
 * Also, keep all objects created in a static LinkedList. The point: we need to be able to mark
37
 * Framebuffers for deletion, and delete all marked objects later at a convenient time (that's
38
 * because we can only delete from a thread that holds the OpenGL context so here we provide a
39
 * framework where one is able to mark for deletion at any place and actual deletion takes place
40
 * on the next render).
32 41
 */
33 42
public class DistortedFramebuffer
34 43
  {
......
36 45
  private static final int TEXTURE_NOT_CREATED_YET  = -2;
37 46
  private static final int TEXTURE_DONT_CREATE      = -3;
38 47

  
48
  private static boolean mListMarked = false;
49
  private static LinkedList<DistortedFramebuffer> mList = new LinkedList<>();
50

  
39 51
  private float mX, mY;
40 52
  private float mFOV;
41 53

  
42 54
  private int[] texIds = new int[1];
43 55
  private int[] fboIds = new int[1];
44 56

  
57
  private boolean mMarked;
58

  
45 59
  int mWidth,mHeight,mDepth,mDistance;
46 60
  float[] mProjectionMatrix;
47
  boolean mMarked;
48 61

  
49 62
///////////////////////////////////////////////////////////////////////////////////////////////////
50 63

  
......
112 125
      return false;
113 126
      }
114 127

  
115
    DistortedFramebufferList.add(this);
128
    mList.add(this);
116 129
    android.util.Log.e("FBO", "created ("+mWidth+","+mHeight+") "+fboIds[0]);
117 130

  
118 131
    return true;
......
135 148
///////////////////////////////////////////////////////////////////////////////////////////////////
136 149
// must be called from a thread holding OpenGL Context
137 150

  
138
  void release()
151
  private void delete()
139 152
    {
140 153
    if( texIds[0]>=0 ) deleteFBO();
141 154

  
......
150 163
    texIds[0] = TEXTURE_NOT_CREATED_YET;
151 164
    }
152 165

  
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

  
168
  static synchronized void release()
169
    {
170
    mListMarked = false;
171
    mList.clear();
172
    }
173

  
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175
// must be called form a thread holding OpenGL Context
176

  
177
  static synchronized void deleteAllMarked()
178
    {
179
    if( mListMarked )
180
      {
181
      DistortedFramebuffer tmp;
182
      Iterator<DistortedFramebuffer> iterator = mList.iterator();
183

  
184
      while(iterator.hasNext())
185
        {
186
        tmp = iterator.next();
187

  
188
        if( tmp.mMarked )
189
          {
190
          tmp.delete();
191
          iterator.remove();
192
          }
193
        }
194

  
195
      mListMarked = false;
196
      }
197
    }
153 198
///////////////////////////////////////////////////////////////////////////////////////////////////
154 199
// PUBLIC API
155 200
///////////////////////////////////////////////////////////////////////////////////////////////////
......
203 248
    {
204 249
    android.util.Log.e("FBO", "marking for deletion ("+mWidth+","+mHeight+") "+fboIds[0]);
205 250

  
206
    DistortedFramebufferList.markForDeletion();
207
    mMarked = true;
251
    mListMarked = true;
252
    mMarked     = true;
208 253
    }
209 254

  
210 255
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/library/DistortedFramebufferList.java
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;
21

  
22
import java.util.LinkedList;
23
import java.util.Iterator;
24

  
25
/**
26
 * List of all DistortedFramebuffer objects currently created by the application.
27
 *
28
 * The point: we need to be able to mark Framebuffers for deletion, and delete all marked
29
 * objects later at a convenient time (that's because we can only delete from a thread that
30
 * holds the OpenGL context so here we provide a framework where one is able to mark for deletion
31
 * at any place and actual deletion takes place on the next render).
32
 */
33

  
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35

  
36
final class DistortedFramebufferList
37
  {
38
  private static boolean mMarked = false;
39
  private static LinkedList<DistortedFramebuffer> mList = new LinkedList<>();
40

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

  
43
  static synchronized void add(DistortedFramebuffer drt)
44
    {
45
    mList.add(drt);
46
    }
47

  
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

  
50
  static synchronized void markForDeletion()
51
    {
52
    mMarked = true;
53
    }
54

  
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

  
57
  static synchronized void release()
58
    {
59
    mMarked = false;
60
    mList.clear();
61
    }
62

  
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64
// must be called form a thread holding OpenGL Context
65

  
66
  static synchronized void deleteAllMarked()
67
    {
68
    if( mMarked )
69
      {
70
      DistortedFramebuffer tmp;
71
      Iterator<DistortedFramebuffer> iterator = mList.iterator();
72

  
73
      while(iterator.hasNext())
74
        {
75
        tmp = iterator.next();
76

  
77
        if( tmp.mMarked )
78
          {
79
          tmp.release();
80
          iterator.remove();
81
          }
82
        }
83

  
84
      mMarked = false;
85
      }
86
    }
87
  }
src/main/java/org/distorted/library/DistortedNode.java
90 90
      }
91 91
    else
92 92
      {
93
      if( mData.mRendered==false )
93
      if( !mData.mRendered )
94 94
        {
95 95
        mData.mRendered = true;
96 96
        mData.mDF.setOutput();
src/main/java/org/distorted/library/DistortedObject.java
32 32
import org.distorted.library.type.Data5D;
33 33
import org.distorted.library.type.Static3D;
34 34

  
35
import java.util.HashMap;
36

  
35 37
///////////////////////////////////////////////////////////////////////////////////////////////////
36 38
/**
37 39
 * All Objects to which Distorted Graphics effects can be applied need to be extended from here.
40
 *
41
 * Just like in DistortedNode and DistortedFramebuffer, we need to have a static list of all
42
 * DistortedObjects currently created by the application so that we can implement the 'mark for
43
 * deletion now - actually delete on next render' thing.
44
 * We need to be able to quickly retrieve an Object by its ID, thus a HashMap.
38 45
 */
39 46
public abstract class DistortedObject 
40 47
  {
48
  private static long mNextID =0;
49
  private static HashMap<Long,DistortedObject> mObjects = new HashMap<>();
50

  
41 51
  private EffectQueueMatrix    mM;
42 52
  private EffectQueueFragment  mF;
43 53
  private EffectQueueVertex    mV;
......
82 92
    mSizeY= y;
83 93
    mSizeZ= z;
84 94

  
85
    mID             = DistortedObjectList.add(this);
95
    mID = mNextID++;
96
    mObjects.put(mID,this);
97

  
86 98
    mTextureDataH   = new int[1];
87 99
    mTextureDataH[0]= 0;
88 100
    mBmp            = new Bitmap[1];
......
137 149
// this will be called on startup and every time OpenGL context has been lost
138 150
// also call this from the constructor if the OpenGL context has been created already.
139 151
    
140
  void resetTexture()
152
  private void resetTexture()
141 153
    {
142 154
    if( mTextureDataH!=null )
143 155
      {
......
161 173
   
162 174
  void drawPriv(long currTime, DistortedFramebuffer df)
163 175
    {
164
    DistortedFramebufferList.deleteAllMarked();
176
    DistortedFramebuffer.deleteAllMarked();
165 177

  
166 178
    GLES20.glViewport(0, 0, df.mWidth, df.mHeight);
167 179
      
......
190 202
    
191 203
///////////////////////////////////////////////////////////////////////////////////////////////////
192 204
   
193
  void releasePriv()
205
  private void releasePriv()
194 206
    {
195
    if( matrixCloned  ==false) mM.abortAll(false);
196
    if( vertexCloned  ==false) mV.abortAll(false);
197
    if( fragmentCloned==false) mF.abortAll(false);
207
    if( !matrixCloned  ) mM.abortAll(false);
208
    if( !vertexCloned  ) mV.abortAll(false);
209
    if( !fragmentCloned) mF.abortAll(false);
198 210

  
199 211
    mBmp          = null;
200 212
    mGrid         = null;
......
207 219
///////////////////////////////////////////////////////////////////////////////////////////////////
208 220

  
209 221
  long getBitmapID()
222
    {
223
    return mBmp==null ? 0 : mBmp.hashCode();
224
    }
225

  
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227

  
228
  static synchronized void reset()
229
    {
230
    for(long id: mObjects.keySet())
210 231
      {
211
      return mBmp==null ? 0 : mBmp.hashCode();
232
      mObjects.get(id).resetTexture();
233
      }
234
    }
235

  
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

  
238
  static synchronized void release()
239
    {
240
    for(long id: mObjects.keySet())
241
      {
242
      mObjects.get(id).releasePriv();
212 243
      }
213 244

  
245
    mObjects.clear();
246
    mNextID = 0;
247
    }
248

  
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250
// PUBLIC API
214 251
///////////////////////////////////////////////////////////////////////////////////////////////////
215 252
/**
216 253
 * Default empty constructor so that derived classes can call it
......
236 273
    {
237 274
    initializeEffectLists(dc,flags);
238 275

  
239
    mID = DistortedObjectList.add(this);
276
    mID = mNextID++;
277
    mObjects.put(mID,this);
240 278

  
241 279
    mSizeX = dc.mSizeX;
242 280
    mSizeY = dc.mSizeY;
......
284 322
/**
285 323
 * Releases all resources.
286 324
 */
287
  public synchronized void release()
325
  public synchronized void delete()
288 326
    {
289 327
    releasePriv();
290
    DistortedObjectList.remove(this);
328
    mObjects.remove(this);
291 329
    }
292 330

  
293 331
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/library/DistortedObjectList.java
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;
21

  
22
import java.util.HashMap;
23
/**
24
 * List of all DistortedObjects currently created by the application.
25
 * We need to be able to quickly retrieve an Object by its ID, thus a HashMap.
26
 */
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28

  
29
final class DistortedObjectList 
30
  {
31
  private static long id =0;  
32
  private static HashMap<Long,DistortedObject> mObjects = new HashMap<>();
33
  
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35

  
36
  static synchronized long add(DistortedObject obj)
37
    {
38
    long ret = id++;  
39
    mObjects.put(ret,obj);
40
    return ret;
41
    }
42

  
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

  
45
  static synchronized void remove(DistortedObject obj)
46
    {
47
    mObjects.remove(obj);
48
    }
49
  
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

  
52
  static DistortedObject get(long id)
53
    {
54
    return mObjects.get(id);
55
    }
56
  
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58
  
59
  static synchronized void reset()
60
    {
61
    for(long id: mObjects.keySet())
62
      {
63
      get(id).resetTexture();  
64
      }
65
    }
66

  
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

  
69
  static synchronized void release()
70
    {
71
    for(long id: mObjects.keySet())
72
      {
73
      get(id).releasePriv();  
74
      }
75
   
76
    mObjects.clear();
77
    id = 0;
78
    }
79
  
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81
  }
src/main/java/org/distorted/library/EffectQueue.java
54 54

  
55 55
  static
56 56
    {
57
    reset();
57
    release();
58 58
    }
59 59
  
60 60
///////////////////////////////////////////////////////////////////////////////////////////////////
......
104 104

  
105 105
  static boolean setMax(int index, int m)
106 106
    {
107
    if( (mCreated==false && !Distorted.isInitialized()) || m<=mMax[index] )
107
    if( (!mCreated && !Distorted.isInitialized()) || m<=mMax[index] )
108 108
      {
109 109
      if( m<0              ) m = 0;
110 110
      else if( m>Byte.MAX_VALUE ) m = Byte.MAX_VALUE;
......
146 146

  
147 147
///////////////////////////////////////////////////////////////////////////////////////////////////
148 148

  
149
  static void reset()
149
  static void release()
150 150
    {
151 151
    EffectTypes.reset(mMax);
152 152
    mCreated = false;  
......
341 341
      android.util.Log.e("EffectQueue", "numEffects="+mNumEffects+" effect id="+id+" index="+index+
342 342
                         " duration="+mCurrentDuration[index]+" inter[0] null="+inter0+" inter[1] null="+inter1+" inter[2] null="+inter2);
343 343
      
344
      if( inter0==false )
344
      if( !inter0 )
345 345
        {
346 346
        android.util.Log.e("EffectQueue","inter[0]: "+mInter[0][index].print());
347 347
        }
348
      if( inter1==false )
348
      if( !inter1 )
349 349
        {
350 350
        android.util.Log.e("EffectQueue","inter[1]: "+mInter[1][index].print());
351 351
        }
352
      if( inter2==false )
352
      if( !inter2 )
353 353
        {
354 354
        android.util.Log.e("EffectQueue","inter[2]: "+mInter[2][index].print());
355 355
        }

Also available in: Unified diff