Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedObject.java @ c41d046c

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
  protected 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
    for(int i=0; i<num; i++)
123
      {
124
      object = mDoneList.removeFirst();
125
      mToDoMap.put(object.mID, object.new Job(object,JOB_CREATE) );
126
      object.recreate();
127
      }
128

    
129
    mToDo = true;
130
    }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133

    
134
  static synchronized void onDestroy()
135
    {
136
    mToDoMap.clear();
137
    mDoneList.clear();
138

    
139
    mToDo = true;
140
    mNextClientID = 0;
141
    }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

    
145
  @SuppressWarnings("unused")
146
  static void debugLists()
147
    {
148
    android.util.Log.e("Object", "Done list:");
149

    
150
    DistortedObject object;
151
    int num = mDoneList.size();
152

    
153
    for(int i=0; i<num; i++)
154
      {
155
      object = mDoneList.get(i);
156
      object.print("");
157
      }
158

    
159
    android.util.Log.e("Object", "ToDo list:");
160

    
161
    Job job;
162

    
163
    for(Long key: mToDoMap.keySet())
164
      {
165
      job = mToDoMap.get(key);
166
      job.object.print(job.action==JOB_CREATE ? " create":" delete");
167
      }
168
    }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

    
172
  private void print(String msg)
173
    {
174
    String str = "ID:"+mID;
175

    
176
    switch(mType)
177
      {
178
      case TYPE_SYST: str+=" SYSTEM "; break;
179
      case TYPE_USER: str+=" USER   "; break;
180
      case TYPE_TREE: str+=" TREE   "; break;
181
      default       : str+=" ERROR? ";
182
      }
183

    
184
    android.util.Log.e("Object", str+printDetails()+msg);
185
    }
186

    
187

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189

    
190
  DistortedObject(int create, int type)
191
    {
192
    mID  = type==TYPE_SYST ? --mNextSystemID : ++mNextClientID;
193
    mType= type;
194

    
195
    if( create!=DONT_CREATE )
196
      {
197
      mToDoMap.put(mID, new Job(this,JOB_CREATE) );
198
      mToDo = true;
199
      }
200
    }
201

    
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203

    
204
  synchronized void markForCreation()
205
    {
206
    mDoneList.remove(this);
207
    mToDoMap.put(mID, new Job(this,JOB_CREATE) );
208
    mToDo = true;
209
    }
210

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212
// PUBLIC API
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214
/**
215
 * Mark the underlying OpenGL object for deletion. Actual deletion will take place on the next render.
216
 */
217
  synchronized public void markForDeletion()
218
    {
219
    mDoneList.remove(this);
220
    mToDoMap.put(mID, new Job(this,JOB_DELETE) );
221
    mToDo = true;
222
    }
223

    
224
////////////////////////////////////////////////////////////////////////////////////////////////////
225
/**
226
 * Return unique ID of this Object.
227
 */
228
  public long getID()
229
    {
230
    return mID;
231
    }
232

    
233
}
(7-7/21)