Project

General

Profile

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

library / src / main / java / org / distorted / library / main / InternalObject.java @ f7c72bd1

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski  leszek@koltunski.pl                                          //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// This library is free software; you can redistribute it and/or                                 //
7
// modify it under the terms of the GNU Lesser General Public                                    //
8
// License as published by the Free Software Foundation; either                                  //
9
// version 2.1 of the License, or (at your option) any later version.                            //
10
//                                                                                               //
11
// This library 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 GNU                             //
14
// Lesser General Public License for more details.                                               //
15
//                                                                                               //
16
// You should have received a copy of the GNU Lesser General Public                              //
17
// License along with this library; if not, write to the Free Software                           //
18
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA                //
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20

    
21
package org.distorted.library.main;
22

    
23
///////////////////////////////////////////////////////////////////////////////////////////////////
24
/**
25
 * Any Object which gets uploaded to GPU memory and thus needs to be re-created (transparently to
26
 * applications!) whenever we lose OpenGL context.
27
 *
28
 * Keep all objects created in a static LinkedList. The point: we need to be able to mark
29
 * Objects for deletion, and delete all marked Objects later at a convenient time (that's
30
 * because we can only delete from a thread that holds the OpenGL context so here we provide a
31
 * framework where one is able to mark for deletion at any time and actual deletion takes place
32
 * on the next render).
33
*/
34
abstract class InternalObject
35
{
36
  static final int FAILED_TO_CREATE = 1;
37
  static final int NOT_CREATED_YET  = 2;
38
  static final int DONT_CREATE      = 3;
39
  static final int CREATED          = 4;
40

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

    
45
  static final int STORAGE_COMMON  = 0;
46
  static final int STORAGE_PRIVATE = 1;
47

    
48
  static final int JOB_CREATE = 0;
49
  static final int JOB_DELETE = 1;
50

    
51
  private final long mID;
52
  private final int mType;
53
  private final int mStorage;
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

    
57
  abstract void create();
58
  abstract void delete();
59
  abstract void recreate();
60
  abstract String printDetails();
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

    
64
  void print(String msg)
65
    {
66
    String str = "ID:"+mID;
67

    
68
    switch(mType)
69
      {
70
      case TYPE_SYST: str+=" SYSTEM "; break;
71
      case TYPE_USER: str+=" USER   "; break;
72
      case TYPE_TREE: str+=" TREE   "; break;
73
      default       : str+=" ERROR? ";
74
      }
75

    
76
    android.util.Log.e("Object", str+printDetails()+msg);
77
    }
78

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80

    
81
  InternalObject(int type, int storage)
82
    {
83
    mType    = type;
84
    mStorage = storage;
85
    mID      = InternalStackFrameList.getCurrentFrame().generateID(mType,mStorage);
86
    }
87

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89

    
90
  void markWasCreatedImmediately()
91
    {
92
    InternalStackFrameList.getCurrentFrame().addToDoneList(this,mStorage);
93
    }
94

    
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96

    
97
  void markForCreation()
98
    {
99
    InternalStackFrameList.markFor(this,mID,mStorage,JOB_CREATE);
100
    }
101

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

    
104
  void removeFromDone()
105
    {
106
    InternalStackFrameList.removeFromDone(this,mStorage);
107
    }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
// PUBLIC API
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112
/**
113
 * Mark the underlying OpenGL object for deletion. Actual deletion will take place on the next render.
114
 */
115
  public void markForDeletion()
116
    {
117
    InternalStackFrameList.markFor(this,mID,mStorage,JOB_DELETE);
118
    }
119

    
120
////////////////////////////////////////////////////////////////////////////////////////////////////
121
/**
122
 * Return unique ID of this Object.
123
 */
124
  public long getID()
125
    {
126
    return mID;
127
    }
128
}
(11-11/17)