Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedSurface.java @ 6e7c8721

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