Project

General

Profile

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

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

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 af27df87 leszek
      if( surface.mSystem ) mToDoList.add(surface);
99
      }
100
101
    num = mToDoList.size();
102
103
    for(int i=0; i<num; i++)
104
      {
105
      surface = mToDoList.get(i);
106 f8377ef8 leszek
107 af27df87 leszek
      if( !surface.mSystem )
108 f8377ef8 leszek
        {
109 af27df87 leszek
        mDoneList.remove(i);
110
        i--;
111
        num--;
112 f8377ef8 leszek
        }
113 af4cc5db Leszek Koltunski
      }
114
115 af27df87 leszek
    mToDo = (num>0);
116
    mNextClientID = 0;
117 f8377ef8 leszek
    }
118
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120
121
  synchronized void moveToToDo()
122
    {
123
    if ( mDoneList.remove(this) )
124
      {
125
      mToDoList.add(this);
126
      mToDo = true;
127
      }
128 af4cc5db Leszek Koltunski
    }
129
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131
132 af27df87 leszek
  @SuppressWarnings("unused")
133
  static void debugLists()
134
    {
135
    android.util.Log.e("Surface", "Done list:");
136
    debugList(mDoneList);
137
    android.util.Log.e("Surface", "ToDo list:");
138
    debugList(mToDoList);
139
    }
140
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142
143
  private static void debugList(LinkedList<DistortedSurface> list)
144
    {
145
    DistortedSurface surface;
146
    String str;
147
148
    int num = list.size();
149
150
    for(int i=0; i<num; i++)
151
      {
152
      surface = list.get(i);
153
154
      if( surface instanceof DistortedFramebuffer ) str = (i+": Framebuffer ");
155
      else if( surface instanceof DistortedTexture) str = (i+": Texture     ");
156
      else if( surface instanceof DistortedScreen ) str = (i+": Screen      ");
157
      else                                          str = (i+": UNKNOWN     ");
158
159
      str += ("("+surface.getWidth()+","+surface.getHeight()+") surfaceID:"+surface.getID());
160
161
      android.util.Log.e("Surface", str);
162
      }
163
    }
164
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166
167
  DistortedSurface(int width, int height, int color, boolean system)
168 af4cc5db Leszek Koltunski
    {
169
    mSizeX    = width ;
170
    mSizeY    = height;
171
    mColorH[0]= color;
172
    mMarked   = false;
173 af27df87 leszek
    mID       = system ? --mNextSystemID : ++mNextClientID;
174
    mSystem   = system;
175 f8377ef8 leszek
176
    if( color!=DONT_CREATE )
177
      {
178
      mToDoList.add(this);
179
      mToDo = true;
180
      }
181 af4cc5db Leszek Koltunski
    }
182
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184
// PUBLIC API
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186 c5369f1b leszek
/**
187 af4cc5db Leszek Koltunski
 * Mark the underlying OpenGL object for deletion. Actual deletion will take place on the next render.
188 c5369f1b leszek
 */
189 af4cc5db Leszek Koltunski
  public void markForDeletion()
190
    {
191 f8377ef8 leszek
    if( !mMarked )
192
      {
193
      mToDo   = true;
194
      mMarked = true;
195
      mDoneList.remove(this);
196
      mToDoList.add(this);
197
      }
198 af4cc5db Leszek Koltunski
    }
199 c5369f1b leszek
200 af4cc5db Leszek Koltunski
////////////////////////////////////////////////////////////////////////////////////////////////////
201 c5369f1b leszek
/**
202 af4cc5db Leszek Koltunski
 * Return unique ID of the Surface.
203 c5369f1b leszek
 */
204 af4cc5db Leszek Koltunski
  public long getID()
205
    {
206 c204c69d leszek
    return mID;
207 af4cc5db Leszek Koltunski
    }
208
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210
211 c5369f1b leszek
/**
212 af4cc5db Leszek Koltunski
 * Returns the height of the Surface.
213
 *
214
 * @return height of the object, in pixels.
215 c5369f1b leszek
 */
216 af4cc5db Leszek Koltunski
  public int getWidth()
217
    {
218
    return mSizeX;
219
    }
220
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222 c5369f1b leszek
/**
223 af4cc5db Leszek Koltunski
 * Returns the width of the Surface.
224
 *
225
 * @return width of the Object, in pixels.
226 c5369f1b leszek
 */
227 af4cc5db Leszek Koltunski
  public int getHeight()
228
    {
229
    return mSizeY;
230
    }
231
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233 c5369f1b leszek
/**
234 af4cc5db Leszek Koltunski
 * Returns the depth of the Surface.
235
 * <p>
236
 * Admittedly quite a strange method. Why do we need to pass a Mesh to it? Because one cannot determine
237
 * 'depth' of a Surface (bitmap really!) when rendered based only on the texture itself, that depends
238
 * on the Mesh it is rendered with.
239
 *
240
 * @return depth of the Object, in pixels.
241 c5369f1b leszek
 */
242 af4cc5db Leszek Koltunski
  public int getDepth(MeshObject mesh)
243
    {
244
    return mesh==null ? 0 : (int)(mSizeX*mesh.zFactor);
245
    }
246
  }