Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedSurface.java @ 8d52a49c

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