Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedEffectsPostprocess.java @ 13687207

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 org.distorted.library.message.EffectListener;
23
import org.distorted.library.type.Data1D;
24

    
25
///////////////////////////////////////////////////////////////////////////////////////////////////
26

    
27
/**
28
 * Class containing the queue of postprocessing effects.
29
 * <p>
30
 * This better be separate from the main DistortedEffects class, because we want to be able to apply
31
 * the same postprocessing effects (i.e. not only the same effects, but the very same instance of this
32
 * object) to several Children of a Node. Reason for that: if several children have the same Object,
33
 * it is easy to group them into 'Buckets' where each bucket has the same postprocessing effects and
34
 * is therefore rendered to the same buffer, which is then postprocessed in one go.
35
 * <p>
36
 * The queue holds actual effects to be applied to a given bucket of several (DistortedTexture,MeshObject) combos.
37
 */
38
public class DistortedEffectsPostprocess
39
  {
40
  private static long mNextID =0;
41
  private long mID;
42

    
43
  private EffectQueuePostprocess mP;
44

    
45
  private boolean postprocessCloned;
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

    
49
  private void initializeEffectLists(DistortedEffectsPostprocess d, int flags)
50
    {
51
    if( (flags & Distorted.CLONE_POSTPROCESS) != 0 )
52
      {
53
      mP = d.mP;
54
      postprocessCloned = true;
55
      }
56
    else
57
      {
58
      mP = new EffectQueuePostprocess(mID);
59
      postprocessCloned = false;
60
      }
61
    }
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

    
65
  EffectQueuePostprocess getPostprocess()
66
    {
67
    return mP;
68
    }
69

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

    
72
  int postprocessPriv(long currTime, DistortedOutputSurface surface)
73
    {
74
    return mP.postprocess(currTime,surface);
75
    }
76

    
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78

    
79
  private void releasePriv()
80
    {
81
    if( !postprocessCloned) mP.abortAll(false);
82

    
83
    mP = null;
84
    }
85

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87
// PUBLIC API
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89
/**
90
 * Create empty effect queue.
91
 */
92
  public DistortedEffectsPostprocess()
93
    {
94
    mID = ++mNextID;
95
    initializeEffectLists(this,0);
96
    }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99
/**
100
 * Copy constructor.
101
 * <p>
102
 * Whatever we do not clone gets created just like in the default constructor.
103
 *
104
 * @param dc    Source object to create our object from
105
 * @param flags A bitmask of values specifying what to copy.
106
 *              Currently the only values possible are CLONE_NOTHING or CLONE_POSTPROCESS.
107
 */
108
  public DistortedEffectsPostprocess(DistortedEffectsPostprocess dc, int flags)
109
    {
110
    mID = ++mNextID;
111
    initializeEffectLists(dc,flags);
112
    }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115
/**
116
 * Releases all resources. After this call, the queue should not be used anymore.
117
 */
118
  @SuppressWarnings("unused")
119
  public synchronized void delete()
120
    {
121
    releasePriv();
122
    }
123

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125
/**
126
 * Returns unique ID of this instance.
127
 *
128
 * @return ID of the object.
129
 */
130
  @SuppressWarnings("unused")
131
  public long getID()
132
      {
133
      return mID;
134
      }
135

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
/**
138
 * Adds the calling class to the list of Listeners that get notified each time some event happens 
139
 * to one of the Effects in the queues. Nothing will happen if 'el' is already in the list.
140
 * 
141
 * @param el A class implementing the EffectListener interface that wants to get notifications.
142
 */
143
  @SuppressWarnings("unused")
144
  public void registerForMessages(EffectListener el)
145
    {
146
    mP.registerForMessages(el);
147
    }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150
/**
151
 * Removes the calling class from the list of Listeners.
152
 * 
153
 * @param el A class implementing the EffectListener interface that no longer wants to get notifications.
154
 */
155
  @SuppressWarnings("unused")
156
  public void deregisterForMessages(EffectListener el)
157
    {
158
    mP.deregisterForMessages(el);
159
    }
160

    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162
/**
163
 * Aborts all Effects.
164
 * @return Number of effects aborted.
165
 */
166
  @SuppressWarnings("unused")
167
  public int abortAllEffects()
168
      {
169
      return mP.abortAll(true);
170
      }
171

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173
/**
174
 * Aborts all Effects of a given type (currently only POSTPROCESSING Effects).
175
 * 
176
 * @param type one of the constants defined in {@link EffectTypes}
177
 * @return Number of effects aborted.
178
 */
179
  @SuppressWarnings("unused")
180
  public int abortEffects(EffectTypes type)
181
    {
182
    switch(type)
183
      {
184
      case POSTPROCESS: return mP.abortAll(true);
185
      default         : return 0;
186
      }
187
    }
188
    
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190
/**
191
 * Aborts a single Effect.
192
 * 
193
 * @param id ID of the Effect we want to abort.
194
 * @return number of Effects aborted. Always either 0 or 1.
195
 */
196
  @SuppressWarnings("unused")
197
  public int abortEffect(long id)
198
    {
199
    int type = (int)(id&EffectTypes.MASK);
200

    
201
    if( type==EffectTypes.POSTPROCESS.type ) return mP.removeByID(id>>EffectTypes.LENGTH);
202

    
203
    return 0;
204
    }
205

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207
/**
208
 * Abort all Effects of a given name, for example all blurs.
209
 * 
210
 * @param name one of the constants defined in {@link EffectNames}
211
 * @return number of Effects aborted.
212
 */
213
  @SuppressWarnings("unused")
214
  public int abortEffects(EffectNames name)
215
    {
216
    switch(name.getType())
217
      {
218
      case POSTPROCESS: return mP.removeByType(name);
219
      default         : return 0;
220
      }
221
    }
222
    
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224
/**
225
 * Print some info about a given Effect to Android's standard out. Used for debugging only.
226
 * 
227
 * @param id Effect ID we want to print info about
228
 * @return <code>true</code> if a single Effect of type effectType has been found.
229
 */
230
  @SuppressWarnings("unused")
231
  public boolean printEffect(long id)
232
    {
233
    int type = (int)(id&EffectTypes.MASK);
234

    
235
    if( type==EffectTypes.POSTPROCESS.type )  return mP.printByID(id>>EffectTypes.LENGTH);
236

    
237
    return false;
238
    }
239

    
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241
/**
242
 * Returns the maximum number of Postprocess effects.
243
 *
244
 * @return The maximum number of Postprocess effects
245
 */
246
  @SuppressWarnings("unused")
247
  public static int getMaxPostprocess()
248
    {
249
    return EffectQueue.getMax(EffectTypes.POSTPROCESS.ordinal());
250
    }
251

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253
/**
254
 * Sets the maximum number of Postprocess effects that can be stored in a single EffectQueue at one time.
255
 * This can fail if:
256
 * <ul>
257
 * <li>the value of 'max' is outside permitted range (0 &le; max &le; Byte.MAX_VALUE)
258
 * <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called
259
 *     before the Fragment Shader gets compiled, i.e. before the call to {@link Distorted#onCreate}. After this
260
 *     time only decreasing the value of 'max' is permitted.
261
 * <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created.
262
 * </ul>
263
 *
264
 * @param max new maximum number of simultaneous Postprocess Effects. Has to be a non-negative number not greater
265
 *            than Byte.MAX_VALUE
266
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
267
 */
268
  @SuppressWarnings("unused")
269
  public static boolean setMaxPostprocess(int max)
270
    {
271
    return EffectQueue.setMax(EffectTypes.POSTPROCESS.ordinal(),max);
272
    }
273

    
274
///////////////////////////////////////////////////////////////////////////////////////////////////   
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276
// Individual effect functions.
277
///////////////////////////////////////////////////////////////////////////////////////////////////
278
// Postprocess-based effects
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280
/**
281
 * Blur the object.
282
 *
283
 * @param radius The 'strength' if the effect, in pixels. 0 = no blur, 10 = when blurring a given pixel,
284
 *               take into account 10 pixels in each direction.
285
 * @return ID of the effect added, or -1 if we failed to add one.
286
 */
287
  public long blur(Data1D radius)
288
    {
289
    return mP.add(EffectNames.BLUR, radius);
290
    }
291
  }
(5-5/24)