Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedSurface.java @ 0c303a2c

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 0c303a2c Leszek Koltunski
    mToDoMap.clear();
134 05ecc6fe Leszek Koltunski
    mDoneList.clear();
135
136 f28fffc2 Leszek Koltunski
    mToDo = true;
137 af27df87 leszek
    mNextClientID = 0;
138 f8377ef8 leszek
    }
139
140 af4cc5db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
141
142 af27df87 leszek
  @SuppressWarnings("unused")
143
  static void debugLists()
144
    {
145
    android.util.Log.e("Surface", "Done list:");
146 f28fffc2 Leszek Koltunski
147
    DistortedSurface surface;
148
    int num = mDoneList.size();
149
150
    for(int i=0; i<num; i++)
151
      {
152
      surface = mDoneList.get(i);
153
      surface.print(i, "");
154
      }
155
156 af27df87 leszek
    android.util.Log.e("Surface", "ToDo list:");
157 f28fffc2 Leszek Koltunski
158
    Job job;
159
    int i=0;
160
161 cc2979e3 Leszek Koltunski
    for(Long key: mToDoMap.keySet())
162 f28fffc2 Leszek Koltunski
      {
163 cc2979e3 Leszek Koltunski
      job = mToDoMap.get(key);
164 f28fffc2 Leszek Koltunski
      job.surface.print(i++, job.action==JOB_CREATE ? " create":" delete");
165
      }
166 af27df87 leszek
    }
167
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169
170 f28fffc2 Leszek Koltunski
  private void print(int i, String extra)
171 af27df87 leszek
    {
172
    String str;
173
174 f28fffc2 Leszek Koltunski
    if( this instanceof DistortedFramebuffer ) str = (i+": Framebuffer ");
175
    else if( this instanceof DistortedTexture) str = (i+": Texture     ");
176
    else if( this instanceof DistortedScreen ) str = (i+": Screen      ");
177
    else                                       str = (i+": UNKNOWN     ");
178 af27df87 leszek
179 f28fffc2 Leszek Koltunski
    str += ("("+getWidth()+","+getHeight()+") surfaceID:"+getID());
180 af27df87 leszek
181 f28fffc2 Leszek Koltunski
    switch(mType)
182
      {
183
      case TYPE_SYST: str+=" SYSTEM"; break;
184
      case TYPE_USER: str+=" USER"  ; break;
185
      case TYPE_TREE: str+=" TREE"  ; break;
186
      default       : str+=" ERROR??";
187 af27df87 leszek
      }
188 f28fffc2 Leszek Koltunski
189
    android.util.Log.e("Surface", str+extra);
190 af27df87 leszek
    }
191
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193
194 09ab7524 Leszek Koltunski
  DistortedSurface(int width, int height, int create, int type)
195 af4cc5db Leszek Koltunski
    {
196 c7da4e65 leszek
    mSizeX        = width ;
197
    mSizeY        = height;
198
    mColorCreated = create;
199
    mColorH[0]    = 0;
200 09ab7524 Leszek Koltunski
    mID           = type==TYPE_SYST ? --mNextSystemID : ++mNextClientID;
201
    mType         = type;
202 c7da4e65 leszek
203
    if( create!=DONT_CREATE )
204 f8377ef8 leszek
      {
205 cc2979e3 Leszek Koltunski
      mToDoMap.put(mID, new Job(this,JOB_CREATE) );
206 f8377ef8 leszek
      mToDo = true;
207
      }
208 af4cc5db Leszek Koltunski
    }
209
210 f28fffc2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
211
212
  synchronized void markForCreation()
213
    {
214
    mDoneList.remove(this);
215 cc2979e3 Leszek Koltunski
    mToDoMap.put(mID, new Job(this,JOB_CREATE) );
216 f28fffc2 Leszek Koltunski
    mToDo = true;
217
    }
218
219 af4cc5db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
220
// PUBLIC API
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222 c5369f1b leszek
/**
223 af4cc5db Leszek Koltunski
 * Mark the underlying OpenGL object for deletion. Actual deletion will take place on the next render.
224 c5369f1b leszek
 */
225 f28fffc2 Leszek Koltunski
  synchronized public void markForDeletion()
226 af4cc5db Leszek Koltunski
    {
227 f28fffc2 Leszek Koltunski
    mDoneList.remove(this);
228 cc2979e3 Leszek Koltunski
    mToDoMap.put(mID, new Job(this,JOB_DELETE) );
229 f28fffc2 Leszek Koltunski
    mToDo = true;
230 af4cc5db Leszek Koltunski
    }
231 c5369f1b leszek
232 af4cc5db Leszek Koltunski
////////////////////////////////////////////////////////////////////////////////////////////////////
233 c5369f1b leszek
/**
234 8d52a49c Leszek Koltunski
 * Return unique ID of this Surface.
235 c5369f1b leszek
 */
236 af4cc5db Leszek Koltunski
  public long getID()
237
    {
238 c204c69d leszek
    return mID;
239 af4cc5db Leszek Koltunski
    }
240
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242
243 c5369f1b leszek
/**
244 3a70bd6d leszek
 * Return the width of this Surface.
245 af4cc5db Leszek Koltunski
 *
246 3a70bd6d leszek
 * @return width of the Object, in pixels.
247 c5369f1b leszek
 */
248 af4cc5db Leszek Koltunski
  public int getWidth()
249
    {
250
    return mSizeX;
251
    }
252
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254 c5369f1b leszek
/**
255 3a70bd6d leszek
 * Return the height of this Surface.
256 af4cc5db Leszek Koltunski
 *
257 3a70bd6d leszek
 * @return height of the Object, in pixels.
258 c5369f1b leszek
 */
259 af4cc5db Leszek Koltunski
  public int getHeight()
260
    {
261
    return mSizeY;
262
    }
263
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265 c5369f1b leszek
/**
266 8d52a49c Leszek Koltunski
 * Return the depth of this Surface.
267 af4cc5db Leszek Koltunski
 * <p>
268
 * Admittedly quite a strange method. Why do we need to pass a Mesh to it? Because one cannot determine
269
 * 'depth' of a Surface (bitmap really!) when rendered based only on the texture itself, that depends
270
 * on the Mesh it is rendered with.
271
 *
272
 * @return depth of the Object, in pixels.
273 c5369f1b leszek
 */
274 af4cc5db Leszek Koltunski
  public int getDepth(MeshObject mesh)
275
    {
276
    return mesh==null ? 0 : (int)(mSizeX*mesh.zFactor);
277
    }
278
  }