Project

General

Profile

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

library / src / main / java / org / distorted / library / main / InternalStackFrameList.java @ fb001aff

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 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 org.distorted.library.message.EffectMessageSender;
23

    
24
import java.util.ArrayList;
25
import java.util.HashMap;
26
import java.util.Set;
27

    
28
///////////////////////////////////////////////////////////////////////////////////////////////////
29

    
30
public class InternalStackFrameList
31
{
32
  private final static Object mLock = new Object();
33
  private static boolean mToDo = false;
34
  private static InternalStackFrame mCurrentFrame = null;
35
  private static ArrayList<InternalStackFrame> mFrameList = new ArrayList<>();
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

    
39
  static void onCreate(long taskId)
40
    {
41
    int num = mFrameList.size();
42
    InternalStackFrame frame;
43
    boolean found = false;
44

    
45
    for(int i=0; i<num; i++)
46
      {
47
      frame = mFrameList.get(i);
48

    
49
      if( frame.getTaskId() == taskId )
50
        {
51
        mCurrentFrame = frame;
52
        found = true;
53
        break;
54
        }
55
      }
56

    
57
    if( !found )
58
      {
59
      synchronized(mLock)
60
        {
61
        mCurrentFrame = new InternalStackFrame(taskId);
62
        mFrameList.add(mCurrentFrame);
63
        }
64
      }
65

    
66
    mCurrentFrame.setInitialized(false);
67
    }
68

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

    
71
  static void onResume(long taskId)
72
    {
73
    int num = mFrameList.size();
74
    InternalStackFrame frame;
75

    
76
    for(int i=0; i<num; i++)
77
      {
78
      frame = mFrameList.get(i);
79

    
80
      if( frame.getTaskId() == taskId )
81
        {
82
        mCurrentFrame = frame;
83
        break;
84
        }
85
      }
86

    
87
    mCurrentFrame.setInitialized(false);
88
    }
89

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91

    
92
  static void onPause(long taskId)
93
    {
94
    int num = mFrameList.size();
95

    
96
    for(int i=0; i<num; i++)
97
      {
98
      if( mFrameList.get(i).getTaskId() == taskId )
99
        {
100
        synchronized(mLock)
101
          {
102
          mCurrentFrame.onPause();
103
          InternalStackFrame.onPauseCommon();
104
          mToDo = true;
105
          }
106

    
107
        break;
108
        }
109
      }
110
    }
111

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

    
114
  static void onDestroy(long taskId)
115
    {
116
    int num = mFrameList.size();
117

    
118
    for(int i=0; i<num; i++)
119
      {
120
      if( mFrameList.get(i).getTaskId() == taskId )
121
        {
122
        synchronized(mLock)
123
          {
124
          mFrameList.remove(i);
125
          if( num==1 ) InternalStackFrame.cleanCommon();
126
          }
127

    
128
        break;
129
        }
130
      }
131

    
132
    setInitialized(false);
133

    
134
    if( num<2 )
135
      {
136
      EffectMessageSender.stopSending();
137
      }
138
    }
139

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

    
142
  @SuppressWarnings("unused")
143
  static void debugLists()
144
    {
145
    int num = mFrameList.size();
146
    InternalStackFrame frame;
147

    
148
    InternalStackFrame.debugCommonList();
149

    
150
    for(int i=0; i<num; i++)
151
      {
152
      frame = mFrameList.get(i);
153
      frame.debugLists("frame "+i);
154
      }
155
    }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

    
159
  @SuppressWarnings("unused")
160
  static void debugMap()
161
    {
162
    int num = mFrameList.size();
163
    InternalStackFrame frame;
164

    
165
    for(int i=0; i<num; i++)
166
      {
167
      frame = mFrameList.get(i);
168
      frame.debugMap("frame "+i);
169
      }
170
    }
171

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173
// must be called from a thread holding OpenGL Context
174

    
175
  static boolean toDo()
176
    {
177
    if( mToDo )
178
      {
179
      mToDo = false;
180

    
181
      synchronized(mLock)
182
        {
183
        mCurrentFrame.toDo();
184
        InternalStackFrame.toDoCommon();
185
        }
186
      return true;
187
      }
188

    
189
    return false;
190
    }
191

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193

    
194
  static void markFor(InternalObject obj, long id, int storage, int job)
195
    {
196
    synchronized(mLock)
197
      {
198
      mCurrentFrame.markFor(obj,id,storage,job);
199
      mToDo = true;
200
      }
201
    }
202

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204

    
205
  static void removeFromDone(InternalObject obj, int storage)
206
    {
207
    synchronized(mLock)
208
      {
209
      mCurrentFrame.removeFromDoneList(obj,storage);
210
      }
211
    }
212

    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214

    
215
  static InternalStackFrame getCurrentFrame()
216
    {
217
    return mCurrentFrame;
218
    }
219

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221

    
222
  static long getNextEffectsID()
223
    {
224
    return mCurrentFrame.getNextEffectsID();
225
    }
226

    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228

    
229
  static void setInitialized(boolean init)
230
    {
231
    mCurrentFrame.setInitialized(init);
232
    }
233

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235

    
236
  static InternalNodeData getMapID(ArrayList<Long> key)
237
    {
238
    return mCurrentFrame.getMapID(key);
239
    }
240

    
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242

    
243
  static InternalNodeData putNewDataToMap(ArrayList<Long> key)
244
    {
245
    return mCurrentFrame.putNewDataToMap(key);
246
    }
247

    
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249

    
250
  static void removeKeyFromMap(ArrayList<Long> key)
251
    {
252
    mCurrentFrame.removeKeyFromMap(key);
253
    }
254

    
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256

    
257
  static ArrayList<InternalMaster.Slave> getSet()
258
    {
259
    return mCurrentFrame.getSet();
260
    }
261

    
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263
// PUBLIC API
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265

    
266
  public static long getNextEffectID()
267
    {
268
    return mCurrentFrame.getNextEffectID();
269
    }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

    
273
  public static boolean isInitialized()
274
    {
275
    return mCurrentFrame.isInitialized();
276
    }
277

    
278
///////////////////////////////////////////////////////////////////////////////////////////////////
279

    
280
  public static int getMax(int index)
281
    {
282
    return mCurrentFrame.getMax(index);
283
    }
284

    
285
///////////////////////////////////////////////////////////////////////////////////////////////////
286

    
287
  public static boolean setMax(int index,int max)
288
    {
289
    return mCurrentFrame.setMax(index,max);
290
    }
291

    
292
///////////////////////////////////////////////////////////////////////////////////////////////////
293

    
294
  public static HashMap<ArrayList<Long>,Long> getMap()
295
    {
296
    return mCurrentFrame.getMap();
297
    }
298

    
299
///////////////////////////////////////////////////////////////////////////////////////////////////
300

    
301
  public static long getNextQueueID()
302
    {
303
    return mCurrentFrame.getNextQueueID();
304
    }
305

    
306
///////////////////////////////////////////////////////////////////////////////////////////////////
307

    
308
}
(15-15/16)