Project

General

Profile

« Previous | Next » 

Revision f8377ef8

Added by Leszek Koltunski about 7 years ago

New, cleaner way to create/destroy DistortedSurfaces.

Serious regression in StarWars (crashes!). Looks like the Node's internal FBO is being deleted and not re-created in time.

View differences:

src/main/java/org/distorted/library/DistortedFramebuffer.java
38 38
// Must be called from a thread holding OpenGL Context
39 39
// Watch out - this has the side-effect of binding a Texture and a Framebuffer!
40 40

  
41
  public void create()
41
  void create()
42 42
    {
43 43
    if( mColorH[0]==NOT_CREATED_YET )
44 44
      {
......
71 71

  
72 72
      checkStatus("depth");
73 73
      }
74
    if( !mDepthEnabled && mDepthH[0]!=NOT_CREATED_YET ) // we need to detach and destroy the DEPTH attachment.
74
    if( !mDepthEnabled && mDepthH[0]!=NOT_CREATED_YET ) // we need to detach and recreate the DEPTH attachment.
75 75
      {
76 76
      GLES30.glDeleteTextures(1, mDepthH, 0);
77 77
      mDepthH[0]=NOT_CREATED_YET;
......
125 125
///////////////////////////////////////////////////////////////////////////////////////////////////
126 126
// called from onDestroy(); mark OpenGL assets as 'not created'
127 127

  
128
  void destroy()
128
  void recreate()
129 129
    {
130 130
    if( mColorH[0]!=DONT_CREATE ) mColorH[0] = NOT_CREATED_YET;
131 131
    if( mDepthEnabled           ) mDepthH[0] = NOT_CREATED_YET;
......
231 231

  
232 232
///////////////////////////////////////////////////////////////////////////////////////////////////
233 233
/**
234
 * Create a new DEPTH buffer and attach it or (param=false) detach an existing DEPTh attachment and destroy it.
234
 * Create a new DEPTH buffer and attach it or (param=false) detach an existing DEPTh attachment and recreate it.
235 235
 *
236 236
 * @param enable <bold>true</bold> if we want to attach a new DEPTH buffer to the FBO.<br>
237 237
 *               <bold>false</bold> if we want to detach the DEPTH attachment.
238 238
 */
239 239
  public void enableDepthAttachment(boolean enable)
240 240
    {
241
    mDepthEnabled = enable;
241
    if( mDepthEnabled!=enable )
242
      {
243
      mDepthEnabled = enable;
244
      moveToToDo();
245
      }
242 246
    }
243 247

  
244 248
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/library/DistortedInputSurface.java
26 26

  
27 27
interface DistortedInputSurface
28 28
{
29
/**
30
 * Create the underlying OpenGL part of the Surface.
31
 */
32
  void create();
33 29
/**
34 30
 * Return a unique ID of this Surface.
35 31
 */
src/main/java/org/distorted/library/DistortedNode.java
58 58
      numPointingNodes= 1;
59 59
      numRendered     = 0;
60 60
      mFBO            = null;
61

  
62
      android.util.Log.e("DistortedNode", "new NodeData");
61 63
      }
62 64
    }
63 65
 
......
125 127
      if( newList.size()>1 )
126 128
        {
127 129
        if( mData.mFBO ==null )
130
          {
131
          android.util.Log.e("DistortedNode", "creating a new FBO");
132

  
128 133
          mData.mFBO = new DistortedFramebuffer(mSurface.getWidth(), mSurface.getHeight());
134
          }
129 135
        }
130 136
      else
131 137
        {
132 138
        if( mData.mFBO !=null )
133 139
          {
140
          android.util.Log.e("DistortedNode", "marking FBO for deletion and setting it to NULL");
141

  
134 142
          mData.mFBO.markForDeletion();
135 143
          mData.mFBO = null;
136 144
          }
......
200 208

  
201 209
  void drawRecursive(long currTime, DistortedOutputSurface surface)
202 210
    {
203
    mSurface.create();
204 211
    float halfX = mSurface.getWidth()/2.0f;
205 212
    float halfY = mSurface.getHeight()/2.0f;
206 213

  
......
210 217
      }
211 218
    else
212 219
      {
213
      mData.mFBO.create();
214

  
215 220
      if( mData.numRendered==0 )
216 221
        {
217 222
        mData.mFBO.setAsOutput();
......
345 350
    ArrayList<Long> prev = generateIDList(); 
346 351
   
347 352
    if( mChildren==null ) mChildren = new ArrayList<>(2);
348
     
353

  
354
    android.util.Log.e("DistortedNode", "ATTACH1");
355

  
356

  
349 357
    node.mParent = this;
350 358
    mChildren.add(node);
351 359
    mNumChildren[0]++;
......
365 373
  public synchronized DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
366 374
    {
367 375
    ArrayList<Long> prev = generateIDList(); 
368
      
376

  
377
    android.util.Log.e("DistortedNode", "ATTACH2");
378

  
369 379
    if( mChildren==null ) mChildren = new ArrayList<>(2);
370 380
    DistortedNode node = new DistortedNode(surface,effects,mesh);
371 381
    node.mParent = this;
......
392 402
         
393 403
      if( mChildren.remove(node) )
394 404
        {
395
        node.mParent = null;  
405
android.util.Log.e("DistortedNode", "DETACH1");
406

  
407
        node.mParent = null;
396 408
        mNumChildren[0]--;
397 409
     
398 410
        RecomputeNodeID(prev);
......
425 437

  
426 438
      if( node.mEffects.getID()==id )
427 439
        {
440
android.util.Log.e("DistortedNode", "DETACH2");
441

  
442

  
428 443
        ArrayList<Long> prev = generateIDList();
429 444

  
430 445
        node.mParent = null;
src/main/java/org/distorted/library/DistortedOutputSurface.java
99 99
 */
100 100
  public void render(long time)
101 101
    {
102
    DistortedSurface.deleteAllMarked();
103
    create();
102
    toDo();
104 103

  
105 104
    for(int i=0; i<mNumChildren; i++)
106 105
      {
......
144 143
      {
145 144
      mWidth = width;
146 145
      mHeight= height;
147
      createProjection();
148 146
      mSizeX = width;
149 147
      mSizeY = height;
150
      if( mColorH[0]>0 ) markForDeletion();
148

  
149
      createProjection();
150

  
151
      if( mColorH[0]>0 )
152
        {
153
        moveToToDo();
154
        recreate();
155
        }
151 156
      }
152 157
    }
153 158

  
src/main/java/org/distorted/library/DistortedScreen.java
32 32
///////////////////////////////////////////////////////////////////////////////////////////////////
33 33
// here we don't manage underlying OpenGL assets ourselves
34 34

  
35
  public void create() {}
36
  void delete() {}
37
  void destroy() {}
35
  void create()  {}
36
  void delete()  {}
37
  void recreate() {}
38 38

  
39 39
///////////////////////////////////////////////////////////////////////////////////////////////////
40 40
// PUBLIC API
src/main/java/org/distorted/library/DistortedSurface.java
19 19

  
20 20
package org.distorted.library;
21 21

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

  
25 24
///////////////////////////////////////////////////////////////////////////////////////////////////
......
36 35
  static final int NOT_CREATED_YET  = -2;
37 36
  static final int DONT_CREATE      = -3;
38 37

  
39
  private static boolean mListMarked = false;
40
  private static LinkedList<DistortedSurface> mList = new LinkedList<>();
38
  private static boolean mToDo = false;
39
  private static LinkedList<DistortedSurface> mDoneList = new LinkedList<>();
40
  private static LinkedList<DistortedSurface> mToDoList = new LinkedList<>();
41 41

  
42 42
  private boolean mMarked;
43 43
  int[] mColorH = new int[1];
......
45 45

  
46 46
///////////////////////////////////////////////////////////////////////////////////////////////////
47 47

  
48
  public abstract void create();
48
  abstract void create();
49 49
  abstract void delete();
50
  abstract void destroy();
50
  abstract void recreate();
51 51

  
52 52
///////////////////////////////////////////////////////////////////////////////////////////////////
53 53
// must be called form a thread holding OpenGL Context
54 54

  
55
  static synchronized void deleteAllMarked()
55
  static synchronized void toDo()
56 56
    {
57
    if( mListMarked )
57
    if( mToDo )
58 58
      {
59
      DistortedSurface tmp;
60
      Iterator<DistortedSurface> iterator = mList.iterator();
59
      DistortedSurface surface;
61 60

  
62
      while(iterator.hasNext())
61
      int num = mToDoList.size();
62

  
63
      for(int i=0; i<num; i++)
63 64
        {
64
        tmp = iterator.next();
65
        surface = mToDoList.removeFirst();
65 66

  
66
        if( tmp.mMarked )
67
        if(surface.mMarked)
68
          {
69
          surface.delete();
70
          surface.mMarked = false;
71
          }
72
        else
67 73
          {
68
          tmp.delete();
69
          tmp.mMarked = false;
70
          iterator.remove();
74
          surface.create();
75
          mDoneList.add(surface);
71 76
          }
72 77
        }
73 78

  
74
      mListMarked = false;
79
      mToDo = false;
75 80
      }
76 81
    }
77 82

  
......
79 84

  
80 85
  static synchronized void onDestroy()
81 86
    {
82
    for( DistortedSurface surface : mList)
87
    DistortedSurface surface;
88

  
89
    int num = mDoneList.size();
90

  
91
    for(int i=0; i<num; i++)
83 92
      {
84
      surface.destroy();
85
      surface.mMarked = false;
93
      surface = mDoneList.removeFirst();
94

  
95
      if( !surface.mMarked )
96
        {
97
        surface.recreate();
98
        mToDoList.add(surface);
99
        }
86 100
      }
87 101

  
88
    mListMarked = false;
102
    mToDo = true;
103
    }
104

  
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106

  
107
  synchronized void moveToToDo()
108
    {
109
    if ( mDoneList.remove(this) )
110
      {
111
      mToDoList.add(this);
112
      mToDo = true;
113
      }
89 114
    }
90 115

  
91 116
///////////////////////////////////////////////////////////////////////////////////////////////////
......
96 121
    mSizeY    = height;
97 122
    mColorH[0]= color;
98 123
    mMarked   = false;
99
    mList.add(this);
124

  
125
    if( color!=DONT_CREATE )
126
      {
127
      mToDoList.add(this);
128
      mToDo = true;
129
      }
100 130
    }
101 131

  
102 132
///////////////////////////////////////////////////////////////////////////////////////////////////
......
107 137
 */
108 138
  public void markForDeletion()
109 139
    {
110
    mListMarked = true;
111
    mMarked     = true;
140
    if( !mMarked )
141
      {
142
      mToDo   = true;
143
      mMarked = true;
144
      mDoneList.remove(this);
145
      mToDoList.add(this);
146
      }
112 147
    }
113 148

  
114 149
////////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/library/DistortedTexture.java
55 55
///////////////////////////////////////////////////////////////////////////////////////////////////
56 56
// must be called from a thread holding OpenGL Context
57 57

  
58
  public void create()
58
  void create()
59 59
    {
60 60
    if( mBmp!=null && mColorH !=null )
61 61
      {
......
94 94
///////////////////////////////////////////////////////////////////////////////////////////////////
95 95
// called from onDestroy(); mark OpenGL assets as 'not created'
96 96

  
97
  void destroy()
97
  void recreate()
98 98
    {
99 99
    if( mColorH[0]!=DONT_CREATE ) mColorH[0] = NOT_CREATED_YET;
100 100
    }
......
148 148
  public void setTexture(Bitmap bmp)
149 149
    {
150 150
    mBmp= bmp;
151
    moveToToDo();
151 152
    }
152 153
  }

Also available in: Unified diff