Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedSurface.java @ 63b6561a

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