Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedSurface.java @ 42571056

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.HashMap;
23
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
  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

    
40
  static final int TYPE_USER = 0;
41
  static final int TYPE_TREE = 1;
42
  static final int TYPE_SYST = 2;
43

    
44
  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
  private static boolean mToDo = false;
60
  private static LinkedList<DistortedSurface> mDoneList = new LinkedList<>();
61
  private static HashMap<Long,Job> mToDoMap = new HashMap<>();
62
  private static long mNextClientID = 0;
63
  private static long mNextSystemID = 0;
64

    
65
  private long mID;
66
  private int mType;
67
  int mColorCreated;
68
  int[] mColorH = new int[1];
69
  int mWidth, mHeight;
70

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72

    
73
  abstract void create();
74
  abstract void delete();
75
  abstract void recreate();
76

    
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78
// must be called from a thread holding OpenGL Context
79

    
80
  static synchronized boolean toDo()
81
    {
82
    if( mToDo )
83
      {
84
      Job job;
85
      DistortedSurface surface;
86

    
87
      for(Long key: mToDoMap.keySet())
88
        {
89
        job = mToDoMap.get(key);
90
        surface = job.surface;
91

    
92
        //android.util.Log.d("SURFACE", "  ---> need to "+(job.action==JOB_CREATE ? "create":"delete")+" surfaceID="+surface.getID() );
93

    
94
        if( job.action==JOB_CREATE )
95
          {
96
          surface.create();
97
          mDoneList.add(surface);
98
          }
99
        else if( job.action==JOB_DELETE )
100
          {
101
          surface.delete();
102
          }
103
        }
104

    
105
      mToDoMap.clear();
106
      mToDo = false;
107
      return true;
108
      }
109

    
110
    return false;
111
    }
112

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114

    
115
  static synchronized void onPause()
116
    {
117
    DistortedSurface surface;
118
    int num = mDoneList.size();
119

    
120
    for(int i=0; i<num; i++)
121
      {
122
      surface = mDoneList.removeFirst();
123
      mToDoMap.put(surface.getID(), surface.new Job(surface,JOB_CREATE) );
124
      surface.recreate();
125
      }
126

    
127
    mToDo = true;
128
    }
129

    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131

    
132
  static synchronized void onDestroy()
133
    {
134
    mToDoMap.clear();
135
    mDoneList.clear();
136

    
137
    mToDo = true;
138
    mNextClientID = 0;
139
    }
140

    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142

    
143
  @SuppressWarnings("unused")
144
  static void debugLists()
145
    {
146
    android.util.Log.e("Surface", "Done list:");
147

    
148
    DistortedSurface surface;
149
    int num = mDoneList.size();
150

    
151
    for(int i=0; i<num; i++)
152
      {
153
      surface = mDoneList.get(i);
154
      surface.print(i, "");
155
      }
156

    
157
    android.util.Log.e("Surface", "ToDo list:");
158

    
159
    Job job;
160
    int i=0;
161

    
162
    for(Long key: mToDoMap.keySet())
163
      {
164
      job = mToDoMap.get(key);
165
      job.surface.print(i++, job.action==JOB_CREATE ? " create":" delete");
166
      }
167
    }
168

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170

    
171
  private void print(int i, String extra)
172
    {
173
    String str;
174

    
175
    if( this instanceof DistortedFramebuffer ) str = (i+": Framebuffer ");
176
    else if( this instanceof DistortedTexture) str = (i+": Texture     ");
177
    else if( this instanceof DistortedScreen ) str = (i+": Screen      ");
178
    else                                       str = (i+": UNKNOWN     ");
179

    
180
    str += ("("+getWidth()+","+getHeight()+") surfaceID:"+getID());
181

    
182
    switch(mType)
183
      {
184
      case TYPE_SYST: str+=" SYSTEM"; break;
185
      case TYPE_USER: str+=" USER"  ; break;
186
      case TYPE_TREE: str+=" TREE"  ; break;
187
      default       : str+=" ERROR??";
188
      }
189

    
190
    android.util.Log.e("Surface", str+extra);
191
    }
192

    
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194

    
195
  DistortedSurface(int width, int height, int create, int type)
196
    {
197
    mWidth        = width ;
198
    mHeight       = height;
199
    mColorCreated = create;
200
    mColorH[0]    = 0;
201
    mID           = type==TYPE_SYST ? --mNextSystemID : ++mNextClientID;
202
    mType         = type;
203

    
204
    if( create!=DONT_CREATE )
205
      {
206
      mToDoMap.put(mID, new Job(this,JOB_CREATE) );
207
      mToDo = true;
208
      }
209
    }
210

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212

    
213
  synchronized void markForCreation()
214
    {
215
    mDoneList.remove(this);
216
    mToDoMap.put(mID, new Job(this,JOB_CREATE) );
217
    mToDo = true;
218
    }
219

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221
// PUBLIC API
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223
/**
224
 * Mark the underlying OpenGL object for deletion. Actual deletion will take place on the next render.
225
 */
226
  synchronized public void markForDeletion()
227
    {
228
    mDoneList.remove(this);
229
    mToDoMap.put(mID, new Job(this,JOB_DELETE) );
230
    mToDo = true;
231
    }
232

    
233
////////////////////////////////////////////////////////////////////////////////////////////////////
234
/**
235
 * Return unique ID of this Surface.
236
 */
237
  public long getID()
238
    {
239
    return mID;
240
    }
241

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

    
244
/**
245
 * Return the width of this Surface.
246
 *
247
 * @return width of the Object, in pixels.
248
 */
249
  public int getWidth()
250
    {
251
    return mWidth;
252
    }
253

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255
/**
256
 * Return the height of this Surface.
257
 *
258
 * @return height of the Object, in pixels.
259
 */
260
  public int getHeight()
261
    {
262
    return mHeight;
263
    }
264

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266
/**
267
 * Return the depth of this Surface.
268
 * <p>
269
 * Admittedly quite a strange method. Why do we need to pass a Mesh to it? Because one cannot determine
270
 * 'depth' of a Surface (bitmap really!) when rendered based only on the texture itself, that depends
271
 * on the Mesh it is rendered with.
272
 *
273
 * @return depth of the Object, in pixels.
274
 */
275
  public int getDepth(MeshObject mesh)
276
    {
277
    return mesh==null ? 0 : (int)(mWidth*mesh.zFactor);
278
    }
279
  }
(12-12/25)