Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedObject.java @ 2dbed690

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.main;
21

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

    
25
///////////////////////////////////////////////////////////////////////////////////////////////////
26
/**
27
 * Any Object which gets uploaded to GPU memory and thus needs to be re-created (transparently to
28
 * applications!) whenever we lose OpenGL context.
29
 *
30
 * Keep all objects created in a static LinkedList. The point: we need to be able to mark
31
 * Objects for deletion, and delete all marked Objects later at a convenient time (that's
32
 * because we can only delete from a thread that holds the OpenGL context so here we provide a
33
 * framework where one is able to mark for deletion at any time and actual deletion takes place
34
 * on the next render).
35
*/
36
abstract class DistortedObject
37
{
38
  static final int FAILED_TO_CREATE = 1;
39
  static final int NOT_CREATED_YET  = 2;
40
  static final int DONT_CREATE      = 3;
41
  static final int CREATED          = 4;
42
  static final int COPIED           = 5;
43

    
44
  static final int TYPE_USER = 0;
45
  static final int TYPE_TREE = 1;
46
  static final int TYPE_SYST = 2;
47

    
48
  private static final int JOB_CREATE = 0;
49
  private static final int JOB_DELETE = 1;
50

    
51
  private class Job
52
    {
53
    DistortedObject object;
54
    int action;
55

    
56
    Job(DistortedObject o, int a)
57
      {
58
      object = o;
59
      action = a;
60
      }
61
    }
62

    
63
  private static boolean mToDo = false;
64
  private static LinkedList<DistortedObject> mDoneList = new LinkedList<>();
65
  private static HashMap<Long,Job> mToDoMap = new HashMap<>();
66
  private static long mNextClientID = 0;
67
  private static long mNextSystemID = 0;
68

    
69
  private long mID;
70
  protected int mType;
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

    
74
  abstract void create();
75
  abstract void delete();
76
  abstract void recreate();
77
  abstract String printDetails();
78

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80
// must be called from a thread holding OpenGL Context
81

    
82
  static synchronized boolean toDo()
83
    {
84
    if( mToDo )
85
      {
86
      Job job;
87
      DistortedObject object;
88

    
89
      for(Long key: mToDoMap.keySet())
90
        {
91
        job = mToDoMap.get(key);
92
        object = job.object;
93

    
94
        //android.util.Log.d("Object", object.getClass().getSimpleName()+"  ---> need to "
95
        //                  +(job.action==JOB_CREATE ? "create":"delete")+" objectID="+object.mDistortedEffectsID );
96

    
97
        if( job.action==JOB_CREATE )
98
          {
99
          object.create();
100
          mDoneList.add(object);
101
          }
102
        else if( job.action==JOB_DELETE )
103
          {
104
          object.delete();
105
          }
106
        }
107

    
108
      mToDoMap.clear();
109
      mToDo = false;
110
      return true;
111
      }
112

    
113
    return false;
114
    }
115

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

    
118
  static synchronized void onPause()
119
    {
120
    DistortedObject object;
121
    int num = mDoneList.size();
122

    
123
    try
124
      {
125
      for (int i = 0; i < num; i++)
126
        {
127
        object = mDoneList.removeFirst();
128
        mToDoMap.put(object.mID, object.new Job(object, JOB_CREATE));
129
        object.recreate();
130
        }
131
      }
132
    catch( Exception ex )
133
      {
134
      // something else removed an object in the meantime; ignore
135
      }
136

    
137
    mToDo = true;
138
    }
139

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141

    
142
  static synchronized void onDestroy()
143
    {
144
    mToDoMap.clear();
145
    mDoneList.clear();
146

    
147
    mToDo = true;
148
    mNextClientID = 0;
149
    }
150

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152

    
153
  @SuppressWarnings("unused")
154
  static void debugLists()
155
    {
156
    android.util.Log.e("Object", "Done list:");
157

    
158
    DistortedObject object;
159
    int num = mDoneList.size();
160

    
161
    for(int i=0; i<num; i++)
162
      {
163
      object = mDoneList.get(i);
164
      object.print("");
165
      }
166

    
167
    android.util.Log.e("Object", "ToDo list:");
168

    
169
    Job job;
170

    
171
    for(Long key: mToDoMap.keySet())
172
      {
173
      job = mToDoMap.get(key);
174
      job.object.print(job.action==JOB_CREATE ? " create":" delete");
175
      }
176
    }
177

    
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179

    
180
  private void print(String msg)
181
    {
182
    String str = "ID:"+mID;
183

    
184
    switch(mType)
185
      {
186
      case TYPE_SYST: str+=" SYSTEM "; break;
187
      case TYPE_USER: str+=" USER   "; break;
188
      case TYPE_TREE: str+=" TREE   "; break;
189
      default       : str+=" ERROR? ";
190
      }
191

    
192
    android.util.Log.e("Object", str+printDetails()+msg);
193
    }
194

    
195

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197

    
198
  DistortedObject(int create, int type)
199
    {
200
    mID  = type==TYPE_SYST ? --mNextSystemID : ++mNextClientID;
201
    mType= type;
202

    
203
    if( create!=DONT_CREATE )
204
      {
205
      mToDoMap.put(mID, new Job(this,JOB_CREATE) );
206
      mToDo = true;
207
      }
208
    }
209

    
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211

    
212
  synchronized void markForCreation()
213
    {
214
    mDoneList.remove(this);
215
    mToDoMap.put(mID, new Job(this,JOB_CREATE) );
216
    mToDo = true;
217
    }
218

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220
// PUBLIC API
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222
/**
223
 * Mark the underlying OpenGL object for deletion. Actual deletion will take place on the next render.
224
 */
225
  synchronized public void markForDeletion()
226
    {
227
    mDoneList.remove(this);
228
    mToDoMap.put(mID, new Job(this,JOB_DELETE) );
229
    mToDo = true;
230
    }
231

    
232
////////////////////////////////////////////////////////////////////////////////////////////////////
233
/**
234
 * Return unique ID of this Object.
235
 */
236
  public long getID()
237
    {
238
    return mID;
239
    }
240

    
241
}
(7-7/21)