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