Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedSurface.java @ 05ecc6fe

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