Project

General

Profile

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

library / src / main / java / org / distorted / library / main / InternalStackFrameList.java @ 3543a3cf

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

    
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27

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

    
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36

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

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

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

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

    
64
    mCurrentFrame.setInitialized(false);
65
    }
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

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

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

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

    
85
    mCurrentFrame.setInitialized(false);
86
    }
87

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89

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

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

    
105
        break;
106
        }
107
      }
108
    }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

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

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

    
126
        break;
127
        }
128
      }
129

    
130
    setInitialized(false);
131

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

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

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

    
146
    InternalStackFrame.debugCommonList();
147

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

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156

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

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

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171
// must be called from a thread holding OpenGL Context
172

    
173
  static boolean toDo()
174
    {
175
    if( mToDo )
176
      {
177
      mToDo = false;
178

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

    
187
    return false;
188
    }
189

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191

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

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

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

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212

    
213
  static InternalStackFrame getCurrentFrame()
214
    {
215
    return mCurrentFrame;
216
    }
217

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

    
220
  static long getNextEffectsID()
221
    {
222
    return mCurrentFrame.getNextEffectsID();
223
    }
224

    
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226

    
227
  static void setInitialized(boolean init)
228
    {
229
    mCurrentFrame.setInitialized(init);
230
    }
231

    
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233

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

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240

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

    
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247

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

    
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254
// PUBLIC API
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256

    
257
  public static long getNextEffectID()
258
    {
259
    return mCurrentFrame.getNextEffectID();
260
    }
261

    
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263

    
264
  public static boolean isInitialized()
265
    {
266
    return mCurrentFrame.isInitialized();
267
    }
268

    
269
///////////////////////////////////////////////////////////////////////////////////////////////////
270

    
271
  public static int getMax(int index)
272
    {
273
    return mCurrentFrame.getMax(index);
274
    }
275

    
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277

    
278
  public static boolean setMax(int index,int max)
279
    {
280
    return mCurrentFrame.setMax(index,max);
281
    }
282
}
(15-15/16)