Project

General

Profile

« Previous | Next » 

Revision 1942537e

Added by Leszek Koltunski over 7 years ago

Major restructuring with DistortedTexture. One now is able to create Textures anywhere, even from a thread which does not hold the OpenGL context. Same for DistortedFramebuffers.

View differences:

src/main/java/org/distorted/library/DistortedTexture.java
24 24
import android.opengl.GLES20;
25 25
import android.opengl.GLUtils;
26 26

  
27
import java.util.Arrays;
28
import java.util.HashMap;
27
import java.util.Iterator;
28
import java.util.LinkedList;
29 29

  
30 30
///////////////////////////////////////////////////////////////////////////////////////////////////
31 31

  
32 32
public class DistortedTexture
33 33
  {
34
  private static long mNextID =0;
35
  private static HashMap<Long,DistortedTexture> mTextures = new HashMap<>();
34
  private static boolean mListMarked = false;
35
  private static LinkedList<DistortedTexture> mList = new LinkedList<>();
36 36

  
37 37
  private int mSizeX, mSizeY;  // in screen space
38 38
  float mHalfX, mHalfY;        // halves of the above
39

  
40 39
  private long mID;
41
  private long mBitmapID=0;
40
  private boolean mMarked;
42 41

  
43 42
  private Bitmap[] mBmp= null; //
44 43
  int[] mTextureDataH;         // have to be shared among all the cloned Objects
......
63 62
    }
64 63

  
65 64
///////////////////////////////////////////////////////////////////////////////////////////////////
66
// this will be called on startup and every time OpenGL context has been lost
67
// also call this from the constructor if the OpenGL context has been created already.
65
// must be called from a thread holding OpenGL Context
68 66

  
69
  private void resetTexture()
67
  void createTexture()
70 68
    {
71
    if( mTextureDataH!=null )
69
    if( mBmp[0]!=null && mTextureDataH!=null )
72 70
      {
71
      //android.util.Log.e("Texture", "creating "+mID);
72

  
73 73
      if( mTextureDataH[0]==0 ) GLES20.glGenTextures(1, mTextureDataH, 0);
74 74

  
75 75
      GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataH[0]);
......
78 78
      GLES20.glTexParameteri ( GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE );
79 79
      GLES20.glTexParameteri ( GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE );
80 80

  
81
      if( mBmp!=null && mBmp[0]!=null)
82
        {
83
        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, flipBitmap(mBmp[0]), 0);
84
        mBmp[0] = null;
85
        }
81
      GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, flipBitmap(mBmp[0]), 0);
82
      mBmp[0] = null;
86 83
      }
87 84
    }
88 85

  
89 86
///////////////////////////////////////////////////////////////////////////////////////////////////
87
// must be called from a thread holding OpenGL Context
90 88

  
91
  private void releasePriv()
89
  private void deleteTexture()
92 90
    {
93
    mBmp          = null;
94
    mTextureDataH = null;
95
    }
91
    if( mTextureDataH!=null && mTextureDataH[0]>0 )
92
      {
93
      //android.util.Log.e("Texture", "deleting "+mID);
96 94

  
97
///////////////////////////////////////////////////////////////////////////////////////////////////
95
      GLES20.glDeleteTextures(1, mTextureDataH, 0);
98 96

  
99
  long getBitmapID()
100
    {
101
    if( mBmp!=null && mBitmapID==0 ) mBitmapID = Arrays.hashCode(mBmp);
102
    return mBitmapID;
97
      mTextureDataH[0] = 0;
98
      mBitmapSet[0] = false;
99
      }
100

  
101
    mMarked = false;
103 102
    }
104 103

  
105 104
///////////////////////////////////////////////////////////////////////////////////////////////////
106 105

  
107 106
  long getID()
108
      {
109
      return mID;
110
      }
107
    {
108
    return mID;
109
    }
111 110

  
112 111
///////////////////////////////////////////////////////////////////////////////////////////////////
113 112

  
114
  static synchronized void reset()
113
  static synchronized void release()
115 114
    {
116
    for(long id: mTextures.keySet())
117
      {
118
      mTextures.get(id).resetTexture();
119
      }
115
    mListMarked = false;
116
    mList.clear();
120 117
    }
121 118

  
122 119
///////////////////////////////////////////////////////////////////////////////////////////////////
120
// must be called form a thread holding OpenGL Context
123 121

  
124
  static synchronized void release()
122
  static synchronized void deleteAllMarked()
125 123
    {
126
    for(long id: mTextures.keySet())
124
    if( mListMarked )
127 125
      {
128
      mTextures.get(id).releasePriv();
129
      }
126
      DistortedTexture tmp;
127
      Iterator<DistortedTexture> iterator = mList.iterator();
128

  
129
      while(iterator.hasNext())
130
        {
131
        tmp = iterator.next();
132

  
133
        if( tmp.mMarked )
134
          {
135
          tmp.deleteTexture();
136
          iterator.remove();
137
          }
138
        }
130 139

  
131
    mTextures.clear();
132
    mNextID = 0;
140
      mListMarked = false;
141
      }
133 142
    }
134 143

  
135 144
///////////////////////////////////////////////////////////////////////////////////////////////////
......
140 149
 */
141 150
  public DistortedTexture(int width, int height)
142 151
    {
143
    mID = mNextID++;
144
    mTextures.put(mID,this);
145

  
146 152
    mSizeX= width ; mHalfX = mSizeX/2.0f;
147 153
    mSizeY= height; mHalfY = mSizeY/2.0f;
148 154

  
......
152 158
    mBmp[0]         = null;
153 159
    mBitmapSet      = new boolean[1];
154 160
    mBitmapSet[0]   = false;
161
    mID             = 0;
162
    mMarked         = false;
155 163

  
156
    if( Distorted.isInitialized() ) resetTexture();
164
    mList.add(this);
157 165
    }
158 166

  
159 167
///////////////////////////////////////////////////////////////////////////////////////////////////
......
170 178

  
171 179
  public DistortedTexture(DistortedTexture dt, int flags)
172 180
    {
173
    mID = mNextID++;
174
    mTextures.put(mID,this);
175

  
176 181
    mSizeX= dt.mSizeX ; mHalfX = mSizeX/2.0f;
177 182
    mSizeY= dt.mSizeY ; mHalfY = mSizeY/2.0f;
178 183

  
......
181 186
      mTextureDataH = dt.mTextureDataH;
182 187
      mBmp          = dt.mBmp;
183 188
      mBitmapSet    = dt.mBitmapSet;
189
      mID           = dt.getID();
184 190
      }
185 191
    else
186 192
      {
......
190 196
      mBitmapSet[0]   = false;
191 197
      mBmp            = new Bitmap[1];
192 198
      mBmp[0]         = null;
193

  
194
      if( Distorted.isInitialized() ) resetTexture();
199
      mID             = 0;
195 200
      }
201

  
202
    mMarked = false;
203

  
204
    mList.add(this);
196 205
    }
197 206

  
198 207
///////////////////////////////////////////////////////////////////////////////////////////////////
199 208
/**
200
 * Releases all resources.
209
 * Mark the underlying OpenGL object for deletion. Actual deletion will take place on the next render.
201 210
 */
202
  public synchronized void delete()
211
  public void markForDeletion()
203 212
    {
204
    releasePriv();
205
    mTextures.remove(this);
213
    //android.util.Log.e("Texture", "marking for deletion "+mID);
214

  
215
    mListMarked = true;
216
    mMarked     = true;
206 217
    }
207 218

  
208 219
///////////////////////////////////////////////////////////////////////////////////////////////////
209 220
/**
210
 * Sets the underlying android.graphics.Bitmap object and uploads it to the GPU.
221
 * Sets the underlying android.graphics.Bitmap object.
211 222
 * <p>
212 223
 * You can only recycle() the passed Bitmap once the OpenGL context gets created (i.e. after call
213 224
 * to onSurfaceCreated) because only after this point can the Library upload it to the GPU!
......
218 229
  public void setTexture(Bitmap bmp)
219 230
    {
220 231
    mBitmapSet[0] = true;
232
    mBmp[0]       = bmp;
233
    mID           = bmp.hashCode();
221 234

  
222
    if( Distorted.isInitialized() )
223
      {
224
      GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
225
      GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataH[0]);
226
      GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, flipBitmap(bmp), 0);
227
      }
228
    else
229
      {
230
      mBmp[0] = bmp;
231
      }
235
    //android.util.Log.e("Texture", "setting new bitmap "+mID);
232 236
    }
233 237

  
234 238
///////////////////////////////////////////////////////////////////////////////////////////////////
......
238 242
 * @return height of the object, in pixels.
239 243
 */
240 244
  public int getWidth()
241
     {
242
     return mSizeX;
243
     }
245
    {
246
    return mSizeX;
247
    }
244 248

  
245 249
///////////////////////////////////////////////////////////////////////////////////////////////////
246 250
/**
......
249 253
 * @return width of the Object, in pixels.
250 254
 */
251 255
  public int getHeight()
252
      {
253
      return mSizeY;
254
      }
256
    {
257
    return mSizeY;
258
    }
255 259

  
256 260
///////////////////////////////////////////////////////////////////////////////////////////////////
257 261
/**
......
264 268
 * @return depth of the Object, in pixels.
265 269
 */
266 270
  public int getDepth(GridObject grid)
267
      {
268
      return grid==null ? 0 : (int)(mSizeX*grid.zFactor);
269
      }
271
    {
272
    return grid==null ? 0 : (int)(mSizeX*grid.zFactor);
273
    }
270 274
  }

Also available in: Unified diff