Project

General

Profile

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

library / src / main / java / org / distorted / library / main / InternalStackFrameList.java @ 11c187c0

1 9ec374e8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 30094332 Leszek Koltunski
import org.distorted.library.message.EffectMessageSender;
23
24 9ec374e8 Leszek Koltunski
import java.util.ArrayList;
25 3bbe4d67 Leszek Koltunski
import java.util.HashMap;
26
import java.util.Set;
27 9ec374e8 Leszek Koltunski
28
///////////////////////////////////////////////////////////////////////////////////////////////////
29 11c187c0 Leszek Koltunski
/**
30
 * Implements a List of Frames (see InternalStackFrame)
31
 * <p>
32
 * Not part of public API, do not document
33
 *
34
 * @y.exclude
35
 */
36 9ec374e8 Leszek Koltunski
public class InternalStackFrameList
37
{
38
  private final static Object mLock = new Object();
39
  private static boolean mToDo = false;
40
  private static InternalStackFrame mCurrentFrame = null;
41
  private static ArrayList<InternalStackFrame> mFrameList = new ArrayList<>();
42
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44
45
  static void onCreate(long taskId)
46
    {
47
    int num = mFrameList.size();
48
    InternalStackFrame frame;
49
    boolean found = false;
50
51
    for(int i=0; i<num; i++)
52
      {
53
      frame = mFrameList.get(i);
54
55
      if( frame.getTaskId() == taskId )
56
        {
57
        mCurrentFrame = frame;
58
        found = true;
59
        break;
60
        }
61
      }
62
63
    if( !found )
64
      {
65
      synchronized(mLock)
66
        {
67
        mCurrentFrame = new InternalStackFrame(taskId);
68
        mFrameList.add(mCurrentFrame);
69
        }
70
      }
71
72
    mCurrentFrame.setInitialized(false);
73
    }
74
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76
77
  static void onResume(long taskId)
78
    {
79
    int num = mFrameList.size();
80
    InternalStackFrame frame;
81
82
    for(int i=0; i<num; i++)
83
      {
84
      frame = mFrameList.get(i);
85
86
      if( frame.getTaskId() == taskId )
87
        {
88
        mCurrentFrame = frame;
89
        break;
90
        }
91
      }
92
93
    mCurrentFrame.setInitialized(false);
94
    }
95
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97
98
  static void onPause(long taskId)
99
    {
100
    int num = mFrameList.size();
101
102
    for(int i=0; i<num; i++)
103
      {
104
      if( mFrameList.get(i).getTaskId() == taskId )
105
        {
106
        synchronized(mLock)
107
          {
108
          mCurrentFrame.onPause();
109
          InternalStackFrame.onPauseCommon();
110
          mToDo = true;
111
          }
112
113
        break;
114
        }
115
      }
116
    }
117
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119
120
  static void onDestroy(long taskId)
121
    {
122
    int num = mFrameList.size();
123
124
    for(int i=0; i<num; i++)
125
      {
126
      if( mFrameList.get(i).getTaskId() == taskId )
127
        {
128
        synchronized(mLock)
129
          {
130
          mFrameList.remove(i);
131
          if( num==1 ) InternalStackFrame.cleanCommon();
132
          }
133
134
        break;
135
        }
136
      }
137 30094332 Leszek Koltunski
138
    setInitialized(false);
139
140
    if( num<2 )
141
      {
142
      EffectMessageSender.stopSending();
143
      }
144 9ec374e8 Leszek Koltunski
    }
145
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147
148
  @SuppressWarnings("unused")
149
  static void debugLists()
150
    {
151
    int num = mFrameList.size();
152
    InternalStackFrame frame;
153
154
    InternalStackFrame.debugCommonList();
155
156
    for(int i=0; i<num; i++)
157
      {
158
      frame = mFrameList.get(i);
159
      frame.debugLists("frame "+i);
160
      }
161
    }
162
163 3543a3cf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
164
165
  @SuppressWarnings("unused")
166
  static void debugMap()
167
    {
168
    int num = mFrameList.size();
169
    InternalStackFrame frame;
170
171
    for(int i=0; i<num; i++)
172
      {
173
      frame = mFrameList.get(i);
174
      frame.debugMap("frame "+i);
175
      }
176
    }
177
178 9ec374e8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
179
// must be called from a thread holding OpenGL Context
180
181
  static boolean toDo()
182
    {
183
    if( mToDo )
184
      {
185
      mToDo = false;
186
187
      synchronized(mLock)
188
        {
189
        mCurrentFrame.toDo();
190
        InternalStackFrame.toDoCommon();
191
        }
192
      return true;
193
      }
194
195
    return false;
196
    }
197
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199
200
  static void markFor(InternalObject obj, long id, int storage, int job)
201
    {
202
    synchronized(mLock)
203
      {
204
      mCurrentFrame.markFor(obj,id,storage,job);
205
      mToDo = true;
206
      }
207
    }
208
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210
211
  static void removeFromDone(InternalObject obj, int storage)
212
    {
213
    synchronized(mLock)
214
      {
215
      mCurrentFrame.removeFromDoneList(obj,storage);
216
      }
217
    }
218
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220
221
  static InternalStackFrame getCurrentFrame()
222
    {
223
    return mCurrentFrame;
224
    }
225
226 3543a3cf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
227
228
  static long getNextEffectsID()
229
    {
230
    return mCurrentFrame.getNextEffectsID();
231
    }
232
233 9ec374e8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
234
235
  static void setInitialized(boolean init)
236
    {
237
    mCurrentFrame.setInitialized(init);
238
    }
239
240 3543a3cf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
241
242
  static InternalNodeData getMapID(ArrayList<Long> key)
243
    {
244
    return mCurrentFrame.getMapID(key);
245
    }
246
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248
249
  static InternalNodeData putNewDataToMap(ArrayList<Long> key)
250
    {
251
    return mCurrentFrame.putNewDataToMap(key);
252
    }
253
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255
256
  static void removeKeyFromMap(ArrayList<Long> key)
257
    {
258
    mCurrentFrame.removeKeyFromMap(key);
259
    }
260
261 3bbe4d67 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
262
263 fb001aff Leszek Koltunski
  static ArrayList<InternalMaster.Slave> getSet()
264 3bbe4d67 Leszek Koltunski
    {
265
    return mCurrentFrame.getSet();
266
    }
267
268 9ec374e8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
269
// PUBLIC API
270 3543a3cf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
271
272
  public static long getNextEffectID()
273
    {
274
    return mCurrentFrame.getNextEffectID();
275
    }
276
277 9ec374e8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
278
279
  public static boolean isInitialized()
280
    {
281
    return mCurrentFrame.isInitialized();
282
    }
283
284 30094332 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
285
286
  public static int getMax(int index)
287
    {
288
    return mCurrentFrame.getMax(index);
289
    }
290
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292
293
  public static boolean setMax(int index,int max)
294
    {
295
    return mCurrentFrame.setMax(index,max);
296
    }
297 3bbe4d67 Leszek Koltunski
298
///////////////////////////////////////////////////////////////////////////////////////////////////
299
300
  public static HashMap<ArrayList<Long>,Long> getMap()
301
    {
302
    return mCurrentFrame.getMap();
303
    }
304
305
///////////////////////////////////////////////////////////////////////////////////////////////////
306
307
  public static long getNextQueueID()
308
    {
309
    return mCurrentFrame.getNextQueueID();
310
    }
311
312
///////////////////////////////////////////////////////////////////////////////////////////////////
313
314 9ec374e8 Leszek Koltunski
}