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;
|
21
|
|
22
|
import java.util.HashMap;
|
23
|
import java.util.LinkedList;
|
24
|
|
25
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
26
|
/**
|
27
|
* Keep all objects created in a static LinkedList. The point: we need to be able to mark
|
28
|
* Objects for deletion, and delete all marked Objects later at a convenient time (that's
|
29
|
* because we can only delete from a thread that holds the OpenGL context so here we provide a
|
30
|
* framework where one is able to mark for deletion at any time and actual deletion takes place
|
31
|
* on the next render).
|
32
|
*/
|
33
|
abstract class DistortedSurface
|
34
|
{
|
35
|
static final int FAILED_TO_CREATE = 1;
|
36
|
static final int NOT_CREATED_YET = 2;
|
37
|
static final int DONT_CREATE = 3;
|
38
|
static final int CREATED = 4;
|
39
|
|
40
|
static final int TYPE_USER = 0;
|
41
|
static final int TYPE_TREE = 1;
|
42
|
static final int TYPE_SYST = 2;
|
43
|
|
44
|
private static final int JOB_CREATE = 0;
|
45
|
private static final int JOB_DELETE = 1;
|
46
|
|
47
|
private class Job
|
48
|
{
|
49
|
DistortedSurface surface;
|
50
|
int action;
|
51
|
|
52
|
Job(DistortedSurface s, int a)
|
53
|
{
|
54
|
surface = s;
|
55
|
action = a;
|
56
|
}
|
57
|
}
|
58
|
|
59
|
private static boolean mToDo = false;
|
60
|
private static LinkedList<DistortedSurface> mDoneList = new LinkedList<>();
|
61
|
private static HashMap<Long,Job> mToDoMap = new HashMap<>();
|
62
|
private static long mNextClientID = 0;
|
63
|
private static long mNextSystemID = 0;
|
64
|
|
65
|
private long mID;
|
66
|
private int mType;
|
67
|
int mColorCreated;
|
68
|
int[] mColorH = new int[1];
|
69
|
int mSizeX, mSizeY; // in screen space
|
70
|
|
71
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
72
|
|
73
|
abstract void create();
|
74
|
abstract void delete();
|
75
|
abstract void recreate();
|
76
|
|
77
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
78
|
// must be called form a thread holding OpenGL Context
|
79
|
|
80
|
static synchronized void toDo()
|
81
|
{
|
82
|
if( mToDo )
|
83
|
{
|
84
|
Job job;
|
85
|
DistortedSurface surface;
|
86
|
|
87
|
for(Long key: mToDoMap.keySet())
|
88
|
{
|
89
|
job = mToDoMap.get(key);
|
90
|
surface = job.surface;
|
91
|
|
92
|
//android.util.Log.d("SURFACE", " ---> need to "+(job.action==JOB_CREATE ? "create":"delete")+" surfaceID="+surface.getID() );
|
93
|
|
94
|
if( job.action==JOB_CREATE )
|
95
|
{
|
96
|
surface.create();
|
97
|
mDoneList.add(surface);
|
98
|
}
|
99
|
else if( job.action==JOB_DELETE )
|
100
|
{
|
101
|
surface.delete();
|
102
|
}
|
103
|
}
|
104
|
|
105
|
mToDoMap.clear();
|
106
|
mToDo = false;
|
107
|
}
|
108
|
}
|
109
|
|
110
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
111
|
|
112
|
static synchronized void onDestroy()
|
113
|
{
|
114
|
Job job;
|
115
|
DistortedSurface surface;
|
116
|
|
117
|
for(Long key: mToDoMap.keySet())
|
118
|
{
|
119
|
job = mToDoMap.get(key);
|
120
|
|
121
|
if( job.surface.mType==TYPE_SYST )
|
122
|
{
|
123
|
mDoneList.add(job.surface);
|
124
|
}
|
125
|
}
|
126
|
|
127
|
mToDoMap.clear();
|
128
|
|
129
|
int num = mDoneList.size();
|
130
|
|
131
|
for(int i=0; i<num; i++)
|
132
|
{
|
133
|
surface = mDoneList.removeFirst();
|
134
|
|
135
|
if( surface.mType==TYPE_SYST )
|
136
|
{
|
137
|
mToDoMap.put(surface.getID(), surface.new Job(surface,JOB_CREATE) );
|
138
|
surface.recreate();
|
139
|
}
|
140
|
}
|
141
|
|
142
|
mToDo = true;
|
143
|
mNextClientID = 0;
|
144
|
}
|
145
|
|
146
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
147
|
|
148
|
@SuppressWarnings("unused")
|
149
|
static void debugLists()
|
150
|
{
|
151
|
android.util.Log.e("Surface", "Done list:");
|
152
|
|
153
|
DistortedSurface surface;
|
154
|
int num = mDoneList.size();
|
155
|
|
156
|
for(int i=0; i<num; i++)
|
157
|
{
|
158
|
surface = mDoneList.get(i);
|
159
|
surface.print(i, "");
|
160
|
}
|
161
|
|
162
|
android.util.Log.e("Surface", "ToDo list:");
|
163
|
|
164
|
Job job;
|
165
|
int i=0;
|
166
|
|
167
|
for(Long key: mToDoMap.keySet())
|
168
|
{
|
169
|
job = mToDoMap.get(key);
|
170
|
job.surface.print(i++, job.action==JOB_CREATE ? " create":" delete");
|
171
|
}
|
172
|
}
|
173
|
|
174
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
175
|
|
176
|
private void print(int i, String extra)
|
177
|
{
|
178
|
String str;
|
179
|
|
180
|
if( this instanceof DistortedFramebuffer ) str = (i+": Framebuffer ");
|
181
|
else if( this instanceof DistortedTexture) str = (i+": Texture ");
|
182
|
else if( this instanceof DistortedScreen ) str = (i+": Screen ");
|
183
|
else str = (i+": UNKNOWN ");
|
184
|
|
185
|
str += ("("+getWidth()+","+getHeight()+") surfaceID:"+getID());
|
186
|
|
187
|
switch(mType)
|
188
|
{
|
189
|
case TYPE_SYST: str+=" SYSTEM"; break;
|
190
|
case TYPE_USER: str+=" USER" ; break;
|
191
|
case TYPE_TREE: str+=" TREE" ; break;
|
192
|
default : str+=" ERROR??";
|
193
|
}
|
194
|
|
195
|
android.util.Log.e("Surface", str+extra);
|
196
|
}
|
197
|
|
198
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
199
|
|
200
|
DistortedSurface(int width, int height, int create, int type)
|
201
|
{
|
202
|
mSizeX = width ;
|
203
|
mSizeY = height;
|
204
|
mColorCreated = create;
|
205
|
mColorH[0] = 0;
|
206
|
mID = type==TYPE_SYST ? --mNextSystemID : ++mNextClientID;
|
207
|
mType = type;
|
208
|
|
209
|
if( create!=DONT_CREATE )
|
210
|
{
|
211
|
mToDoMap.put(mID, new Job(this,JOB_CREATE) );
|
212
|
mToDo = true;
|
213
|
}
|
214
|
}
|
215
|
|
216
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
217
|
|
218
|
synchronized void markForCreation()
|
219
|
{
|
220
|
mDoneList.remove(this);
|
221
|
mToDoMap.put(mID, new Job(this,JOB_CREATE) );
|
222
|
mToDo = true;
|
223
|
}
|
224
|
|
225
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
226
|
// PUBLIC API
|
227
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
228
|
/**
|
229
|
* Mark the underlying OpenGL object for deletion. Actual deletion will take place on the next render.
|
230
|
*/
|
231
|
synchronized public void markForDeletion()
|
232
|
{
|
233
|
mDoneList.remove(this);
|
234
|
mToDoMap.put(mID, new Job(this,JOB_DELETE) );
|
235
|
mToDo = true;
|
236
|
}
|
237
|
|
238
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
239
|
/**
|
240
|
* Return unique ID of this Surface.
|
241
|
*/
|
242
|
public long getID()
|
243
|
{
|
244
|
return mID;
|
245
|
}
|
246
|
|
247
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
248
|
|
249
|
/**
|
250
|
* Return the width of this Surface.
|
251
|
*
|
252
|
* @return width of the Object, in pixels.
|
253
|
*/
|
254
|
public int getWidth()
|
255
|
{
|
256
|
return mSizeX;
|
257
|
}
|
258
|
|
259
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
260
|
/**
|
261
|
* Return the height of this Surface.
|
262
|
*
|
263
|
* @return height of the Object, in pixels.
|
264
|
*/
|
265
|
public int getHeight()
|
266
|
{
|
267
|
return mSizeY;
|
268
|
}
|
269
|
|
270
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
271
|
/**
|
272
|
* Return the depth of this Surface.
|
273
|
* <p>
|
274
|
* Admittedly quite a strange method. Why do we need to pass a Mesh to it? Because one cannot determine
|
275
|
* 'depth' of a Surface (bitmap really!) when rendered based only on the texture itself, that depends
|
276
|
* on the Mesh it is rendered with.
|
277
|
*
|
278
|
* @return depth of the Object, in pixels.
|
279
|
*/
|
280
|
public int getDepth(MeshObject mesh)
|
281
|
{
|
282
|
return mesh==null ? 0 : (int)(mSizeX*mesh.zFactor);
|
283
|
}
|
284
|
}
|