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.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
|
static final int FAILED_TO_CREATE = -1;
|
35
|
static final int NOT_CREATED_YET = -2;
|
36
|
static final int DONT_CREATE = -3;
|
37
|
|
38
|
private static boolean mToDo = false;
|
39
|
private static LinkedList<DistortedSurface> mDoneList = new LinkedList<>();
|
40
|
private static LinkedList<DistortedSurface> mToDoList = new LinkedList<>();
|
41
|
private static long mNextClientID = 0;
|
42
|
private static long mNextSystemID = 0;
|
43
|
|
44
|
private long mID;
|
45
|
private boolean mMarked;
|
46
|
private boolean mSystem;
|
47
|
int[] mColorH = new int[1];
|
48
|
int mSizeX, mSizeY; // in screen space
|
49
|
|
50
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
51
|
|
52
|
abstract void create();
|
53
|
abstract void delete();
|
54
|
abstract void recreate();
|
55
|
|
56
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
57
|
// must be called form a thread holding OpenGL Context
|
58
|
|
59
|
static synchronized void toDo()
|
60
|
{
|
61
|
if( mToDo )
|
62
|
{
|
63
|
DistortedSurface surface;
|
64
|
|
65
|
int num = mToDoList.size();
|
66
|
|
67
|
for(int i=0; i<num; i++)
|
68
|
{
|
69
|
surface = mToDoList.removeFirst();
|
70
|
|
71
|
if(surface.mMarked)
|
72
|
{
|
73
|
surface.delete();
|
74
|
surface.mMarked = false;
|
75
|
}
|
76
|
else
|
77
|
{
|
78
|
surface.create();
|
79
|
mDoneList.add(surface);
|
80
|
}
|
81
|
}
|
82
|
|
83
|
mToDo = false;
|
84
|
}
|
85
|
}
|
86
|
|
87
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
88
|
|
89
|
static synchronized void onDestroy()
|
90
|
{
|
91
|
DistortedSurface surface;
|
92
|
|
93
|
int num = mDoneList.size();
|
94
|
|
95
|
for(int i=0; i<num; i++)
|
96
|
{
|
97
|
surface = mDoneList.removeFirst();
|
98
|
if( surface.mSystem ) mToDoList.add(surface);
|
99
|
}
|
100
|
|
101
|
num = mToDoList.size();
|
102
|
|
103
|
for(int i=0; i<num; i++)
|
104
|
{
|
105
|
surface = mToDoList.get(i);
|
106
|
|
107
|
if( !surface.mSystem )
|
108
|
{
|
109
|
mDoneList.remove(i);
|
110
|
i--;
|
111
|
num--;
|
112
|
}
|
113
|
}
|
114
|
|
115
|
mToDo = (num>0);
|
116
|
mNextClientID = 0;
|
117
|
}
|
118
|
|
119
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
120
|
|
121
|
synchronized void moveToToDo()
|
122
|
{
|
123
|
if ( mDoneList.remove(this) )
|
124
|
{
|
125
|
mToDoList.add(this);
|
126
|
mToDo = true;
|
127
|
}
|
128
|
}
|
129
|
|
130
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
131
|
|
132
|
@SuppressWarnings("unused")
|
133
|
static void debugLists()
|
134
|
{
|
135
|
android.util.Log.e("Surface", "Done list:");
|
136
|
debugList(mDoneList);
|
137
|
android.util.Log.e("Surface", "ToDo list:");
|
138
|
debugList(mToDoList);
|
139
|
}
|
140
|
|
141
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
142
|
|
143
|
private static void debugList(LinkedList<DistortedSurface> list)
|
144
|
{
|
145
|
DistortedSurface surface;
|
146
|
String str;
|
147
|
|
148
|
int num = list.size();
|
149
|
|
150
|
for(int i=0; i<num; i++)
|
151
|
{
|
152
|
surface = list.get(i);
|
153
|
|
154
|
if( surface instanceof DistortedFramebuffer ) str = (i+": Framebuffer ");
|
155
|
else if( surface instanceof DistortedTexture) str = (i+": Texture ");
|
156
|
else if( surface instanceof DistortedScreen ) str = (i+": Screen ");
|
157
|
else str = (i+": UNKNOWN ");
|
158
|
|
159
|
str += ("("+surface.getWidth()+","+surface.getHeight()+") surfaceID:"+surface.getID());
|
160
|
|
161
|
android.util.Log.e("Surface", str);
|
162
|
}
|
163
|
}
|
164
|
|
165
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
166
|
|
167
|
DistortedSurface(int width, int height, int color, boolean system)
|
168
|
{
|
169
|
mSizeX = width ;
|
170
|
mSizeY = height;
|
171
|
mColorH[0]= color;
|
172
|
mMarked = false;
|
173
|
mID = system ? --mNextSystemID : ++mNextClientID;
|
174
|
mSystem = system;
|
175
|
|
176
|
if( color!=DONT_CREATE )
|
177
|
{
|
178
|
mToDoList.add(this);
|
179
|
mToDo = true;
|
180
|
}
|
181
|
}
|
182
|
|
183
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
184
|
// PUBLIC API
|
185
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
186
|
/**
|
187
|
* Mark the underlying OpenGL object for deletion. Actual deletion will take place on the next render.
|
188
|
*/
|
189
|
public void markForDeletion()
|
190
|
{
|
191
|
if( !mMarked )
|
192
|
{
|
193
|
mToDo = true;
|
194
|
mMarked = true;
|
195
|
mDoneList.remove(this);
|
196
|
mToDoList.add(this);
|
197
|
}
|
198
|
}
|
199
|
|
200
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
201
|
/**
|
202
|
* Return unique ID of the Surface.
|
203
|
*/
|
204
|
public long getID()
|
205
|
{
|
206
|
return mID;
|
207
|
}
|
208
|
|
209
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
210
|
|
211
|
/**
|
212
|
* Returns the height of the Surface.
|
213
|
*
|
214
|
* @return height of the object, in pixels.
|
215
|
*/
|
216
|
public int getWidth()
|
217
|
{
|
218
|
return mSizeX;
|
219
|
}
|
220
|
|
221
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
222
|
/**
|
223
|
* Returns the width of the Surface.
|
224
|
*
|
225
|
* @return width of the Object, in pixels.
|
226
|
*/
|
227
|
public int getHeight()
|
228
|
{
|
229
|
return mSizeY;
|
230
|
}
|
231
|
|
232
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
233
|
/**
|
234
|
* Returns the depth of the Surface.
|
235
|
* <p>
|
236
|
* Admittedly quite a strange method. Why do we need to pass a Mesh to it? Because one cannot determine
|
237
|
* 'depth' of a Surface (bitmap really!) when rendered based only on the texture itself, that depends
|
238
|
* on the Mesh it is rendered with.
|
239
|
*
|
240
|
* @return depth of the Object, in pixels.
|
241
|
*/
|
242
|
public int getDepth(MeshObject mesh)
|
243
|
{
|
244
|
return mesh==null ? 0 : (int)(mSizeX*mesh.zFactor);
|
245
|
}
|
246
|
}
|