Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedSurface.java @ 86782a25

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 af4cc5db Leszek Koltunski
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 c7da4e65 leszek
  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 af4cc5db Leszek Koltunski
40 09ab7524 Leszek Koltunski
  static final int TYPE_USER = 0;
41
  static final int TYPE_TREE = 1;
42
  static final int TYPE_SYST = 2;
43
44 f28fffc2 Leszek Koltunski
  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 f8377ef8 leszek
  private static boolean mToDo = false;
60
  private static LinkedList<DistortedSurface> mDoneList = new LinkedList<>();
61 cc2979e3 Leszek Koltunski
  private static HashMap<Long,Job> mToDoMap = new HashMap<>();
62 af27df87 leszek
  private static long mNextClientID = 0;
63
  private static long mNextSystemID = 0;
64 af4cc5db Leszek Koltunski
65 c204c69d leszek
  private long mID;
66 09ab7524 Leszek Koltunski
  private int mType;
67 c7da4e65 leszek
  int mColorCreated;
68 af4cc5db Leszek Koltunski
  int[] mColorH = new int[1];
69
  int mSizeX, mSizeY;  // in screen space
70
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72
73 f8377ef8 leszek
  abstract void create();
74 af4cc5db Leszek Koltunski
  abstract void delete();
75 f8377ef8 leszek
  abstract void recreate();
76 af4cc5db Leszek Koltunski
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78
// must be called form a thread holding OpenGL Context
79
80 f8377ef8 leszek
  static synchronized void toDo()
81 af4cc5db Leszek Koltunski
    {
82 f8377ef8 leszek
    if( mToDo )
83 af4cc5db Leszek Koltunski
      {
84 f28fffc2 Leszek Koltunski
      Job job;
85 f8377ef8 leszek
      DistortedSurface surface;
86 af4cc5db Leszek Koltunski
87 cc2979e3 Leszek Koltunski
      for(Long key: mToDoMap.keySet())
88 af4cc5db Leszek Koltunski
        {
89 cc2979e3 Leszek Koltunski
        job = mToDoMap.get(key);
90 f28fffc2 Leszek Koltunski
        surface = job.surface;
91 af4cc5db Leszek Koltunski
92 eadf0859 leszek
        //android.util.Log.d("SURFACE", "  ---> need to "+(job.action==JOB_CREATE ? "create":"delete")+" surfaceID="+surface.getID() );
93 f28fffc2 Leszek Koltunski
94
        if( job.action==JOB_CREATE )
95 af4cc5db Leszek Koltunski
          {
96 f8377ef8 leszek
          surface.create();
97
          mDoneList.add(surface);
98 af4cc5db Leszek Koltunski
          }
99 f28fffc2 Leszek Koltunski
        else if( job.action==JOB_DELETE )
100
          {
101
          surface.delete();
102
          }
103 af4cc5db Leszek Koltunski
        }
104
105 cc2979e3 Leszek Koltunski
      mToDoMap.clear();
106 f8377ef8 leszek
      mToDo = false;
107 af4cc5db Leszek Koltunski
      }
108
    }
109
110 c5369f1b leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
111
112 af4cc5db Leszek Koltunski
  static synchronized void onDestroy()
113
    {
114 f28fffc2 Leszek Koltunski
    Job job;
115 f8377ef8 leszek
    DistortedSurface surface;
116
117 cc2979e3 Leszek Koltunski
    for(Long key: mToDoMap.keySet())
118 af4cc5db Leszek Koltunski
      {
119 cc2979e3 Leszek Koltunski
      job = mToDoMap.get(key);
120 63b6561a leszek
121 f28fffc2 Leszek Koltunski
      if( job.surface.mType==TYPE_SYST )
122 63b6561a leszek
        {
123 f28fffc2 Leszek Koltunski
        mDoneList.add(job.surface);
124 63b6561a leszek
        }
125 af27df87 leszek
      }
126
127 cc2979e3 Leszek Koltunski
    mToDoMap.clear();
128 f28fffc2 Leszek Koltunski
129
    int num = mDoneList.size();
130 af27df87 leszek
131
    for(int i=0; i<num; i++)
132
      {
133 f28fffc2 Leszek Koltunski
      surface = mDoneList.removeFirst();
134 f8377ef8 leszek
135 f28fffc2 Leszek Koltunski
      if( surface.mType==TYPE_SYST )
136 f8377ef8 leszek
        {
137 cc2979e3 Leszek Koltunski
        mToDoMap.put(surface.getID(), surface.new Job(surface,JOB_CREATE) );
138 f28fffc2 Leszek Koltunski
        surface.recreate();
139 f8377ef8 leszek
        }
140 af4cc5db Leszek Koltunski
      }
141
142 f28fffc2 Leszek Koltunski
    mToDo = true;
143 af27df87 leszek
    mNextClientID = 0;
144 f8377ef8 leszek
    }
145
146 af4cc5db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
147
148 af27df87 leszek
  @SuppressWarnings("unused")
149
  static void debugLists()
150
    {
151
    android.util.Log.e("Surface", "Done list:");
152 f28fffc2 Leszek Koltunski
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 af27df87 leszek
    android.util.Log.e("Surface", "ToDo list:");
163 f28fffc2 Leszek Koltunski
164
    Job job;
165
    int i=0;
166
167 cc2979e3 Leszek Koltunski
    for(Long key: mToDoMap.keySet())
168 f28fffc2 Leszek Koltunski
      {
169 cc2979e3 Leszek Koltunski
      job = mToDoMap.get(key);
170 f28fffc2 Leszek Koltunski
      job.surface.print(i++, job.action==JOB_CREATE ? " create":" delete");
171
      }
172 af27df87 leszek
    }
173
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175
176 f28fffc2 Leszek Koltunski
  private void print(int i, String extra)
177 af27df87 leszek
    {
178
    String str;
179
180 f28fffc2 Leszek Koltunski
    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 af27df87 leszek
185 f28fffc2 Leszek Koltunski
    str += ("("+getWidth()+","+getHeight()+") surfaceID:"+getID());
186 af27df87 leszek
187 f28fffc2 Leszek Koltunski
    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 af27df87 leszek
      }
194 f28fffc2 Leszek Koltunski
195
    android.util.Log.e("Surface", str+extra);
196 af27df87 leszek
    }
197
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199
200 09ab7524 Leszek Koltunski
  DistortedSurface(int width, int height, int create, int type)
201 af4cc5db Leszek Koltunski
    {
202 c7da4e65 leszek
    mSizeX        = width ;
203
    mSizeY        = height;
204
    mColorCreated = create;
205
    mColorH[0]    = 0;
206 09ab7524 Leszek Koltunski
    mID           = type==TYPE_SYST ? --mNextSystemID : ++mNextClientID;
207
    mType         = type;
208 c7da4e65 leszek
209
    if( create!=DONT_CREATE )
210 f8377ef8 leszek
      {
211 cc2979e3 Leszek Koltunski
      mToDoMap.put(mID, new Job(this,JOB_CREATE) );
212 f8377ef8 leszek
      mToDo = true;
213
      }
214 af4cc5db Leszek Koltunski
    }
215
216 f28fffc2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
217
218
  synchronized void markForCreation()
219
    {
220
    mDoneList.remove(this);
221 cc2979e3 Leszek Koltunski
    mToDoMap.put(mID, new Job(this,JOB_CREATE) );
222 f28fffc2 Leszek Koltunski
    mToDo = true;
223
    }
224
225 af4cc5db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
226
// PUBLIC API
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228 c5369f1b leszek
/**
229 af4cc5db Leszek Koltunski
 * Mark the underlying OpenGL object for deletion. Actual deletion will take place on the next render.
230 c5369f1b leszek
 */
231 f28fffc2 Leszek Koltunski
  synchronized public void markForDeletion()
232 af4cc5db Leszek Koltunski
    {
233 f28fffc2 Leszek Koltunski
    mDoneList.remove(this);
234 cc2979e3 Leszek Koltunski
    mToDoMap.put(mID, new Job(this,JOB_DELETE) );
235 f28fffc2 Leszek Koltunski
    mToDo = true;
236 af4cc5db Leszek Koltunski
    }
237 c5369f1b leszek
238 af4cc5db Leszek Koltunski
////////////////////////////////////////////////////////////////////////////////////////////////////
239 c5369f1b leszek
/**
240 8d52a49c Leszek Koltunski
 * Return unique ID of this Surface.
241 c5369f1b leszek
 */
242 af4cc5db Leszek Koltunski
  public long getID()
243
    {
244 c204c69d leszek
    return mID;
245 af4cc5db Leszek Koltunski
    }
246
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248
249 c5369f1b leszek
/**
250 3a70bd6d leszek
 * Return the width of this Surface.
251 af4cc5db Leszek Koltunski
 *
252 3a70bd6d leszek
 * @return width of the Object, in pixels.
253 c5369f1b leszek
 */
254 af4cc5db Leszek Koltunski
  public int getWidth()
255
    {
256
    return mSizeX;
257
    }
258
259
///////////////////////////////////////////////////////////////////////////////////////////////////
260 c5369f1b leszek
/**
261 3a70bd6d leszek
 * Return the height of this Surface.
262 af4cc5db Leszek Koltunski
 *
263 3a70bd6d leszek
 * @return height of the Object, in pixels.
264 c5369f1b leszek
 */
265 af4cc5db Leszek Koltunski
  public int getHeight()
266
    {
267
    return mSizeY;
268
    }
269
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271 c5369f1b leszek
/**
272 8d52a49c Leszek Koltunski
 * Return the depth of this Surface.
273 af4cc5db Leszek Koltunski
 * <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 c5369f1b leszek
 */
280 af4cc5db Leszek Koltunski
  public int getDepth(MeshObject mesh)
281
    {
282
    return mesh==null ? 0 : (int)(mSizeX*mesh.zFactor);
283
    }
284
  }