Revision 226144d0
Added by Leszek Koltunski about 7 years ago
src/main/java/org/distorted/library/DistortedSurface.java | ||
---|---|---|
19 | 19 |
|
20 | 20 |
package org.distorted.library; |
21 | 21 |
|
22 |
import java.util.HashMap; |
|
23 |
import java.util.LinkedList; |
|
24 |
|
|
25 | 22 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
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 | 23 |
|
65 |
private long mID;
|
|
66 |
private int mType;
|
|
24 |
abstract class DistortedSurface extends DistortedObject
|
|
25 |
{
|
|
67 | 26 |
int mColorCreated; |
68 | 27 |
int[] mColorH = new int[1]; |
69 | 28 |
int mWidth, mHeight; |
70 | 29 |
|
71 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
72 |
|
|
73 |
abstract void create(); |
|
74 |
abstract void delete(); |
|
75 |
abstract void recreate(); |
|
76 |
|
|
77 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
78 |
// must be called from a thread holding OpenGL Context |
|
79 |
|
|
80 |
static synchronized boolean 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 |
return true; |
|
108 |
} |
|
109 |
|
|
110 |
return false; |
|
111 |
} |
|
112 |
|
|
113 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
114 |
|
|
115 |
static synchronized void onPause() |
|
116 |
{ |
|
117 |
DistortedSurface surface; |
|
118 |
int num = mDoneList.size(); |
|
119 |
|
|
120 |
for(int i=0; i<num; i++) |
|
121 |
{ |
|
122 |
surface = mDoneList.removeFirst(); |
|
123 |
mToDoMap.put(surface.getID(), surface.new Job(surface,JOB_CREATE) ); |
|
124 |
surface.recreate(); |
|
125 |
} |
|
126 |
|
|
127 |
mToDo = true; |
|
128 |
} |
|
129 |
|
|
130 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
131 |
|
|
132 |
static synchronized void onDestroy() |
|
133 |
{ |
|
134 |
mToDoMap.clear(); |
|
135 |
mDoneList.clear(); |
|
136 |
|
|
137 |
mToDo = true; |
|
138 |
mNextClientID = 0; |
|
139 |
} |
|
140 |
|
|
141 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
142 |
|
|
143 |
@SuppressWarnings("unused") |
|
144 |
static void debugLists() |
|
145 |
{ |
|
146 |
android.util.Log.e("Surface", "Done list:"); |
|
147 |
|
|
148 |
DistortedSurface surface; |
|
149 |
int num = mDoneList.size(); |
|
150 |
|
|
151 |
for(int i=0; i<num; i++) |
|
152 |
{ |
|
153 |
surface = mDoneList.get(i); |
|
154 |
surface.print(i, ""); |
|
155 |
} |
|
156 |
|
|
157 |
android.util.Log.e("Surface", "ToDo list:"); |
|
158 |
|
|
159 |
Job job; |
|
160 |
int i=0; |
|
161 |
|
|
162 |
for(Long key: mToDoMap.keySet()) |
|
163 |
{ |
|
164 |
job = mToDoMap.get(key); |
|
165 |
job.surface.print(i++, job.action==JOB_CREATE ? " create":" delete"); |
|
166 |
} |
|
167 |
} |
|
168 |
|
|
169 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
170 |
|
|
171 |
private void print(int i, String extra) |
|
172 |
{ |
|
173 |
String str; |
|
174 |
|
|
175 |
if( this instanceof DistortedFramebuffer ) str = (i+": Framebuffer "); |
|
176 |
else if( this instanceof DistortedTexture) str = (i+": Texture "); |
|
177 |
else if( this instanceof DistortedScreen ) str = (i+": Screen "); |
|
178 |
else str = (i+": UNKNOWN "); |
|
179 |
|
|
180 |
str += ("("+getWidth()+","+getHeight()+") surfaceID:"+getID()); |
|
181 |
|
|
182 |
switch(mType) |
|
183 |
{ |
|
184 |
case TYPE_SYST: str+=" SYSTEM"; break; |
|
185 |
case TYPE_USER: str+=" USER" ; break; |
|
186 |
case TYPE_TREE: str+=" TREE" ; break; |
|
187 |
default : str+=" ERROR??"; |
|
188 |
} |
|
189 |
|
|
190 |
android.util.Log.e("Surface", str+extra); |
|
191 |
} |
|
192 |
|
|
193 | 30 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
194 | 31 |
|
195 | 32 |
DistortedSurface(int width, int height, int create, int type) |
196 | 33 |
{ |
34 |
super(create,type); |
|
35 |
|
|
197 | 36 |
mWidth = width ; |
198 | 37 |
mHeight = height; |
199 | 38 |
mColorCreated = create; |
200 | 39 |
mColorH[0] = 0; |
201 |
mID = type==TYPE_SYST ? --mNextSystemID : ++mNextClientID; |
|
202 |
mType = type; |
|
203 |
|
|
204 |
if( create!=DONT_CREATE ) |
|
205 |
{ |
|
206 |
mToDoMap.put(mID, new Job(this,JOB_CREATE) ); |
|
207 |
mToDo = true; |
|
208 |
} |
|
209 | 40 |
} |
210 | 41 |
|
211 | 42 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
43 |
// debugging only |
|
212 | 44 |
|
213 |
synchronized void markForCreation()
|
|
45 |
String printDetails()
|
|
214 | 46 |
{ |
215 |
mDoneList.remove(this); |
|
216 |
mToDoMap.put(mID, new Job(this,JOB_CREATE) ); |
|
217 |
mToDo = true; |
|
47 |
return getClass().getSimpleName()+" "+mWidth+"x"+mHeight; |
|
218 | 48 |
} |
219 | 49 |
|
220 | 50 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
221 | 51 |
// PUBLIC API |
222 | 52 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
223 |
/** |
|
224 |
* Mark the underlying OpenGL object for deletion. Actual deletion will take place on the next render. |
|
225 |
*/ |
|
226 |
synchronized public void markForDeletion() |
|
227 |
{ |
|
228 |
mDoneList.remove(this); |
|
229 |
mToDoMap.put(mID, new Job(this,JOB_DELETE) ); |
|
230 |
mToDo = true; |
|
231 |
} |
|
232 |
|
|
233 |
//////////////////////////////////////////////////////////////////////////////////////////////////// |
|
234 |
/** |
|
235 |
* Return unique ID of this Surface. |
|
236 |
*/ |
|
237 |
public long getID() |
|
238 |
{ |
|
239 |
return mID; |
|
240 |
} |
|
241 |
|
|
242 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
243 |
|
|
244 | 53 |
/** |
245 | 54 |
* Return the width of this Surface. |
246 | 55 |
* |
... | ... | |
276 | 85 |
{ |
277 | 86 |
return mesh==null ? 0 : (int)(mWidth*mesh.zFactor); |
278 | 87 |
} |
279 |
} |
|
88 |
} |
Also available in: Unified diff
Progress with VBOs - this time abstract out a new class, DistortedObject - i.e. everything that uploads something to GPU and thus needs to be auto re-created upon loss of the context.