Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedObject.java @ 7a5e538a

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

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

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

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

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

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

    
68
  private long mID;
69
  private int mType;
70

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72

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

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

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

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

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

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

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

    
112
    return false;
113
    }
114

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116

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

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

    
136
    mToDo = true;
137
    }
138

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

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

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

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151

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

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

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

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

    
168
    Job job;
169

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

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178

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

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

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

    
194

    
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196

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

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

    
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210

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

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

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

    
240
}
(7-7/17)