Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedOutputSurface.java @ 885d424a

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 a436ccc5 leszek
import android.opengl.GLES30;
23 af4cc5db Leszek Koltunski
import android.opengl.Matrix;
24 a09ada4c Leszek Koltunski
import java.util.ArrayList;
25 af4cc5db Leszek Koltunski
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27
28 efe3d8fe leszek
abstract class DistortedOutputSurface extends DistortedSurface implements DistortedSlave
29 af4cc5db Leszek Koltunski
{
30 efe3d8fe leszek
  private static final int ATTACH = 0;
31
  private static final int DETACH = 1;
32
  private static final int DETALL = 2;
33
  private static final int SORT   = 3;
34
35 a09ada4c Leszek Koltunski
  private ArrayList<DistortedNode> mChildren;
36
  private int mNumChildren;   // ==mChildren.length(), but we only create mChildren if the first one gets added
37
38 efe3d8fe leszek
  private class Job
39
    {
40
    int type;
41
    DistortedNode node;
42 be60d4ff leszek
    DistortedEffectsPostprocess dep;
43 efe3d8fe leszek
44 be60d4ff leszek
    Job(int t, DistortedNode n, DistortedEffectsPostprocess d)
45 efe3d8fe leszek
      {
46
      type = t;
47
      node = n;
48 be60d4ff leszek
      dep  = d;
49 efe3d8fe leszek
      }
50
    }
51
52
  private ArrayList<Job> mJobs = new ArrayList<>();
53
54 78db8663 Leszek Koltunski
  DistortedFramebuffer[] mBuffer1, mBuffer2;
55 60c1c622 leszek
56 95c441a2 leszek
  private long mTime;
57 c2c08950 leszek
  private float mFOV;
58
  float mDistance, mNear;
59 af4cc5db Leszek Koltunski
  float[] mProjectionMatrix;
60
61 c7da4e65 leszek
  int mDepthCreated;
62 a436ccc5 leszek
  int[] mDepthH = new int[1];
63
  int[] mFBOH   = new int[1];
64
65 a9f41fa3 leszek
  private float mClearR, mClearG, mClearB, mClearA;
66
  private float mClearDepth;
67 0afc143a leszek
68
//private String sNew="", sOld="";
69
70 78db8663 Leszek Koltunski
  float mMipmap;
71
72 af4cc5db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
73
74 09ab7524 Leszek Koltunski
  DistortedOutputSurface(int width, int height, int createColor, int createDepth, int fbo, int type)
75 af4cc5db Leszek Koltunski
    {
76 09ab7524 Leszek Koltunski
    super(width,height,createColor,type);
77 af4cc5db Leszek Koltunski
78
    mProjectionMatrix = new float[16];
79
80
    mWidth = width;
81
    mHeight= height;
82
83
    mFOV = 60.0f;
84 54fe333a leszek
    mNear=  0.5f;
85 af4cc5db Leszek Koltunski
86 c7da4e65 leszek
    mDepthCreated= createDepth;
87 a436ccc5 leszek
    mFBOH[0]     = fbo;
88 c7da4e65 leszek
    mDepthH[0]   = 0;
89 a436ccc5 leszek
90 95c441a2 leszek
    mTime = 0;
91
92 a9f41fa3 leszek
    mClearR = 0.0f;
93
    mClearG = 0.0f;
94
    mClearB = 0.0f;
95
    mClearA = 0.0f;
96
97
    mClearDepth = 1.0f;
98
99 8426bd6a Leszek Koltunski
    mBuffer1 = new DistortedFramebuffer[EffectQuality.LENGTH];
100
    mBuffer2 = new DistortedFramebuffer[EffectQuality.LENGTH];
101 638b5b5c leszek
102 8426bd6a Leszek Koltunski
    mMipmap = 1.0f;
103 78db8663 Leszek Koltunski
104 af4cc5db Leszek Koltunski
    createProjection();
105
    }
106
107 c5369f1b leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
108
109 be60d4ff leszek
  private void createProjection()
110 af4cc5db Leszek Koltunski
    {
111
    if( mWidth>0 && mHeight>1 )
112
      {
113
      if( mFOV>0.0f )  // perspective projection
114
        {
115 54fe333a leszek
        float a = 2.0f*(float)Math.tan(mFOV*Math.PI/360);
116 8426bd6a Leszek Koltunski
        float q = mWidth*mNear;
117
        float c = mHeight*mNear;
118 54fe333a leszek
119 c2c08950 leszek
        float left   = -q/2;
120
        float right  = +q/2;
121
        float bottom = -c/2;
122
        float top    = +c/2;
123
        float near   =  c/a;
124 54fe333a leszek
125 8426bd6a Leszek Koltunski
        mDistance    = mHeight/a;
126 af4cc5db Leszek Koltunski
        float far    = 2*mDistance-near;
127
128
        Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
129
        }
130
      else             // parallel projection
131
        {
132 8426bd6a Leszek Koltunski
        float left   = -mWidth/2.0f;
133
        float right  = +mWidth/2.0f;
134
        float bottom = -mHeight/2.0f;
135
        float top    = +mHeight/2.0f;
136
        float near   = mWidth+mHeight-mHeight*(1.0f-mNear);
137
        mDistance    = mWidth+mHeight;
138
        float far    = mWidth+mHeight+mHeight*(1.0f-mNear);
139 af4cc5db Leszek Koltunski
140
        Matrix.orthoM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
141
        }
142
      }
143
    }
144
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146 e02264ff leszek
// Render all children, one by one. If there are no postprocessing effects, just render to THIS.
147
// Otherwise, render to a buffer and on each change of Postprocessing Bucket, apply the postprocessing
148
// to a whole buffer and merge it.
149 39086ebb leszek
150 b2939df4 Leszek Koltunski
  int renderChildren(long time, int num, ArrayList<DistortedNode> children)
151
    {
152
    int numRenders = 0;
153 b9798977 leszek
    DistortedNode child;
154 27f42cd6 leszek
    DistortedEffectsPostprocess lastP=null, currP;
155 e137d4f5 Leszek Koltunski
    long lastB=0, currB;
156 0afc143a leszek
157
//sNew = "";
158
159 b2939df4 Leszek Koltunski
    for(int i=0; i<num; i++)
160 39086ebb leszek
      {
161 b9798977 leszek
      child = children.get(i);
162 27f42cd6 leszek
      currP = child.getEffectsPostprocess();
163 13687207 leszek
      currB = currP==null ? 0 : currP.getBucket();
164 0afc143a leszek
165
//sNew += currB;
166
167 13687207 leszek
      if( lastB!=currB && lastB!=0 )
168
        {
169
        numRenders += lastP.postprocess(time,this);
170
        }
171 b9798977 leszek
172 13687207 leszek
      if( currB==0 )
173
        {
174
        numRenders += child.draw(time,this);
175
        }
176 60c1c622 leszek
      else
177
        {
178 78db8663 Leszek Koltunski
        if( mBuffer1[0]==null )
179 60c1c622 leszek
          {
180 78db8663 Leszek Koltunski
          float mipmap=1.0f;
181
182 8426bd6a Leszek Koltunski
          for(int j=0; j<EffectQuality.LENGTH; j++)
183 78db8663 Leszek Koltunski
            {
184
            mBuffer1[j] = new DistortedFramebuffer( mDepthCreated!=DONT_CREATE, DistortedSurface.TYPE_SYST,
185
                                                    (int)(mWidth*mipmap), (int)(mHeight*mipmap) );
186
            mBuffer2[j] = new DistortedFramebuffer(false                     , DistortedSurface.TYPE_SYST,
187
                                                    (int)(mWidth*mipmap), (int)(mHeight*mipmap) );
188
            mBuffer1[j].mMipmap = mipmap;
189 472e51e1 Leszek Koltunski
            mipmap *= EffectQuality.MULTIPLIER;
190 78db8663 Leszek Koltunski
            }
191 893e008d leszek
          DistortedSurface.toDo();  // create immediately
192 60c1c622 leszek
          }
193 b9798977 leszek
194 8426bd6a Leszek Koltunski
        numRenders += child.draw(time,mBuffer1[currP.getQuality()]);
195
196 13687207 leszek
        if( i==num-1 )
197
          {
198
          numRenders += currP.postprocess(time,this);
199
          }
200 60c1c622 leszek
        }
201 b9798977 leszek
202 60c1c622 leszek
      lastP = currP;
203
      lastB = currB;
204 95c441a2 leszek
      }
205 be60d4ff leszek
/*
206
if( !sNew.equals(sOld) )
207
  {
208
  sOld = sNew;
209
  android.util.Log.e("surface", "Surface"+getID()+": "+sOld);
210
  }
211
*/
212 39086ebb leszek
    return numRenders;
213
    }
214
215 be60d4ff leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
216
217
  void newJob(int t, DistortedNode n, DistortedEffectsPostprocess d)
218
    {
219
    mJobs.add(new Job(t,n,d));
220
    }
221
222 b2939df4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
223
// PUBLIC API
224 39086ebb leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
225 c5369f1b leszek
/**
226 d9706fd2 Leszek Koltunski
 * Draws all the attached children to this OutputSurface.
227 af4cc5db Leszek Koltunski
 * <p>
228
 * Must be called from a thread holding OpenGL Context.
229
 *
230 d9706fd2 Leszek Koltunski
 * @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes.
231 7691a39f leszek
 * @return Number of objects rendered.
232 c5369f1b leszek
 */
233 b2939df4 Leszek Koltunski
  public int render(long time)
234 af4cc5db Leszek Koltunski
    {
235 c204c69d leszek
    // change tree topology (attach and detach children)
236 889cce10 Leszek Koltunski
/*
237
    boolean changed =
238
*/
239 efe3d8fe leszek
    DistortedMaster.toDo();
240 eadf0859 leszek
/*
241 f28fffc2 Leszek Koltunski
    // debugging only
242 af27df87 leszek
    if( changed )
243 c204c69d leszek
      {
244
      for(int i=0; i<mNumChildren; i++)
245
        {
246 af27df87 leszek
        mChildren.get(i).debug(0);
247 c204c69d leszek
        }
248 af27df87 leszek
249 7691a39f leszek
      DistortedNode.debugMap();
250 c204c69d leszek
      }
251 eadf0859 leszek
*/
252 09ab7524 Leszek Koltunski
    // create and delete all underlying OpenGL resources
253
    // Watch out: FIRST change topology, only then deal
254
    // with OpenGL resources. That's because changing Tree
255
    // can result in additional Framebuffers that would need
256
    // to be created immediately, before the calls to drawRecursive()
257 f8377ef8 leszek
    toDo();
258 eadf0859 leszek
/*
259 f28fffc2 Leszek Koltunski
    // debugging only
260 af27df87 leszek
    if( changed )
261
      {
262
      DistortedSurface.debugLists();
263
      }
264 eadf0859 leszek
*/
265 c834348d leszek
    // mark OpenGL state as unknown
266
    DistortedRenderState.reset();
267 2ed1c692 leszek
268 b2939df4 Leszek Koltunski
    int numRenders=0;
269 7691a39f leszek
270 d9706fd2 Leszek Koltunski
    for(int i=0; i<mNumChildren; i++)
271 af4cc5db Leszek Koltunski
      {
272 b2939df4 Leszek Koltunski
      numRenders += mChildren.get(i).renderRecursive(time);
273 af4cc5db Leszek Koltunski
      }
274 7691a39f leszek
275 95c441a2 leszek
    setAsOutput(time);
276 b2939df4 Leszek Koltunski
    numRenders += renderChildren(time,mNumChildren,mChildren);
277
278 7691a39f leszek
    return numRenders;
279 af4cc5db Leszek Koltunski
    }
280
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282 c5369f1b leszek
/**
283
 * Bind this Surface as a Framebuffer we can render to.
284 02ab6f9d leszek
 *
285
 * @param time Present time, in milliseconds. The point: looking at this param the library can figure
286
 *             out if this is the first time during present frame that this FBO is being set as output.
287
 *             If so, the library, in addition to binding the Surface for output, also clears the
288
 *             Surface's color and depth attachments.
289 c5369f1b leszek
 */
290 95c441a2 leszek
  public void setAsOutput(long time)
291 a436ccc5 leszek
    {
292
    GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
293 95c441a2 leszek
294
    if( mTime!=time )
295
      {
296
      mTime = time;
297
      DistortedRenderState.colorDepthOn();
298 a9f41fa3 leszek
      GLES30.glClearColor(mClearR, mClearG, mClearB, mClearA);
299
      GLES30.glClearDepthf(mClearDepth);
300 95c441a2 leszek
      GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
301
      }
302 a436ccc5 leszek
    }
303 af4cc5db Leszek Koltunski
304 638b5b5c leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
305
/**
306
 * Set mipmap level.
307
 * <p>
308
 * Trick for speeding up your renders - one can create a pyramid of OutputSurface objects, each next
309
 * one some constant FACTOR smaller than the previous (0.5 is the common value), then set the Mipmap
310
 * Level of the i-th object to be FACTOR^i (we start counting from 0). When rendering any scene into
311
 * such prepared OutputSurface, the library will make sure to scale any Effects used so that the end
312
 * scene will end up looking identical no matter which object we render to. Identical, that is, except
313
 * for the loss of quality and gain in speed associated with rendering to a smaller Surface.
314 8426bd6a Leszek Koltunski
 * <p>
315
 * Example: if you create two FBOs, one 1000x1000 and another 500x500 in size, and set the second one
316
 * mipmap to 0.5 (the first one's is 1.0 by default), define Effects to be a single move by (100,100),
317
 * and render a skinned Mesh into both FBO, the end result will look proportionally the same, because
318
 * in the second case the move vector (100,100) will be auto-scaled to (50,50).
319 638b5b5c leszek
 *
320
 * @param mipmap The mipmap level. Acceptable range: 0&lt;mipmap&lt;infinity, although mipmap&gt;1
321
 *               does not make any sense (that would result in loss of speed and no gain in quality)
322
 */
323
  public void setMipmap(float mipmap)
324
    {
325
    mMipmap = mipmap;
326
    }
327
328 a9f41fa3 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
329
/**
330
 * Set the (R,G,B,A) values of GLES30.glClearColor() to set up color with which to clear
331
 * this Surface before each render.
332
 *
333
 * @param r the Red component. Default: 0.0f
334
 * @param g the Green component. Default: 0.0f
335
 * @param b the Blue component. Default: 0.0f
336
 * @param a the Alpha component. Default: 0.0f
337
 */
338
  public void glClearColor(float r, float g, float b, float a)
339
    {
340
    mClearR = r;
341
    mClearG = g;
342
    mClearB = b;
343
    mClearA = a;
344
    }
345
346
///////////////////////////////////////////////////////////////////////////////////////////////////
347
/**
348
 * Set the Depth value of GLES30.glClearDepthf() to set up depth with which to clear
349
 * the Depth buffer of Surface before each render.
350
 *
351
 * @param d the Depth. Default: 1.0f
352
 */
353
  public void glClearDepthf(float d)
354
    {
355
    mClearDepth = d;
356
    }
357
358 af4cc5db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
359
/**
360
 * Create new Projection matrix.
361
 *
362
 * @param fov Vertical 'field of view' of the Projection frustrum (in degrees).
363 54fe333a leszek
 *            Valid values: 0<=fov<180. FOV==0 means 'parallel projection'.
364
 * @param near Distance between the screen plane and the near plane.
365
 *             Valid vaules: 0<near<1. When near==0, the Near Plane is exactly at the tip of the
366
 *             pyramid. When near==1 (illegal!) the near plane is equivalent to the screen plane.
367 af4cc5db Leszek Koltunski
 */
368 54fe333a leszek
  public void setProjection(float fov, float near)
369 af4cc5db Leszek Koltunski
    {
370 8069e806 Leszek Koltunski
    if( fov < 180.0f && fov >=0.0f )
371
      {
372
      mFOV = fov;
373
      }
374
375
    if( near<   1.0f && near> 0.0f )
376
      {
377
      mNear= near;
378
      }
379
    else if( near<=0.0f )
380
      {
381
      mNear = 0.01f;
382
      }
383
    else if( near>=1.0f )
384
      {
385
      mNear=0.99f;
386
      }
387 af4cc5db Leszek Koltunski
388
    createProjection();
389
    }
390
391
///////////////////////////////////////////////////////////////////////////////////////////////////
392 c5369f1b leszek
/**
393 af4cc5db Leszek Koltunski
 * Resize the underlying Framebuffer.
394 c7da4e65 leszek
 * <p>
395
 * This method can be safely called mid-render as it doesn't interfere with rendering.
396 af4cc5db Leszek Koltunski
 *
397
 * @param width The new width.
398
 * @param height The new height.
399 c5369f1b leszek
 */
400 af4cc5db Leszek Koltunski
  public void resize(int width, int height)
401
    {
402
    if( mWidth!=width || mHeight!=height )
403
      {
404
      mWidth = width;
405
      mHeight= height;
406 f8377ef8 leszek
407
      createProjection();
408
409 c7da4e65 leszek
      if( mColorCreated==CREATED )
410 f8377ef8 leszek
        {
411 f28fffc2 Leszek Koltunski
        markForCreation();
412 f8377ef8 leszek
        recreate();
413
        }
414 af4cc5db Leszek Koltunski
      }
415
    }
416 a09ada4c Leszek Koltunski
417 a436ccc5 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
418
/**
419
 * Create a new DEPTH buffer and attach it or (param=false) detach an existing DEPTH attachment and recreate it.
420
 *
421
 * @param enable <bold>true</bold> if we want to attach a new DEPTH buffer to the FBO.<br>
422
 *               <bold>false</bold> if we want to detach the DEPTH attachment.
423
 */
424
  public void enableDepth(boolean enable)
425
    {
426 c7da4e65 leszek
    if( enable && mDepthCreated==DONT_CREATE )
427
      {
428
      mDepthCreated = NOT_CREATED_YET;
429 f28fffc2 Leszek Koltunski
      markForCreation();
430 c7da4e65 leszek
      }
431
    if( !enable && mDepthCreated!=DONT_CREATE )
432 a436ccc5 leszek
      {
433 c7da4e65 leszek
      mDepthCreated = DONT_CREATE;
434 f28fffc2 Leszek Koltunski
      markForCreation();
435 a436ccc5 leszek
      }
436
    }
437
438
///////////////////////////////////////////////////////////////////////////////////////////////////
439
/**
440
 * Return true if the Surface contains a DEPTH attachment.
441
 *
442
 * @return <bold>true</bold> if the FBO contains a DEPTH attachment.
443
 */
444
  public boolean hasDepth()
445
    {
446 c7da4e65 leszek
    return mDepthCreated==CREATED;
447 a436ccc5 leszek
    }
448
449 a09ada4c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
450
/**
451
 * Adds a new child to the last position in the list of our Surface's children.
452 c204c69d leszek
 * <p>
453
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
454 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
455 a09ada4c Leszek Koltunski
 *
456
 * @param node The new Node to add.
457
 */
458 c204c69d leszek
  public void attach(DistortedNode node)
459 a09ada4c Leszek Koltunski
    {
460 be60d4ff leszek
    mJobs.add(new Job(ATTACH,node,null));
461 efe3d8fe leszek
    DistortedMaster.newSlave(this);
462 a09ada4c Leszek Koltunski
    }
463
464
///////////////////////////////////////////////////////////////////////////////////////////////////
465
/**
466
 * Adds a new child to the last position in the list of our Surface's children.
467 c204c69d leszek
 * <p>
468
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
469 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
470 a09ada4c Leszek Koltunski
 *
471
 * @param surface InputSurface to initialize our child Node with.
472
 * @param effects DistortedEffects to initialize our child Node with.
473
 * @param mesh MeshObject to initialize our child Node with.
474
 * @return the newly constructed child Node, or null if we couldn't allocate resources.
475
 */
476 c204c69d leszek
  public DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
477 a09ada4c Leszek Koltunski
    {
478
    DistortedNode node = new DistortedNode(surface,effects,mesh);
479 be60d4ff leszek
    mJobs.add(new Job(ATTACH,node,null));
480 efe3d8fe leszek
    DistortedMaster.newSlave(this);
481 c204c69d leszek
    return node;
482
    }
483
484 af27df87 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
485
/**
486
 * Removes the first occurrence of a specified child from the list of children of our Surface.
487
 * <p>
488
 * A bit questionable method as there can be many different Nodes attached as children, some
489
 * of them having the same Effects but - for instance - different Mesh. Use with care.
490
 * <p>
491
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
492 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
493 af27df87 leszek
 *
494
 * @param effects DistortedEffects to remove.
495
 */
496
  public void detach(DistortedEffects effects)
497
    {
498
    long id = effects.getID();
499
    DistortedNode node;
500 8baa1fe6 Leszek Koltunski
    boolean detached = false;
501 af27df87 leszek
502
    for(int i=0; i<mNumChildren; i++)
503
      {
504
      node = mChildren.get(i);
505
506
      if( node.getEffects().getID()==id )
507
        {
508 8baa1fe6 Leszek Koltunski
        detached = true;
509 be60d4ff leszek
        mJobs.add(new Job(DETACH,node,null));
510 efe3d8fe leszek
        DistortedMaster.newSlave(this);
511 af27df87 leszek
        break;
512
        }
513
      }
514 8baa1fe6 Leszek Koltunski
515
    if( !detached )
516
      {
517
      // if we failed to detach any, it still might be the case that
518 efe3d8fe leszek
      // there's an ATTACH job that we need to cancel.
519
      int num = mJobs.size();
520
      Job job;
521
522
      for(int i=0; i<num; i++)
523
        {
524
        job = mJobs.get(i);
525
526
        if( job.type==ATTACH && job.node.getEffects()==effects )
527
          {
528
          mJobs.remove(i);
529
          break;
530
          }
531
        }
532 8baa1fe6 Leszek Koltunski
      }
533 af27df87 leszek
    }
534
535 a09ada4c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
536
/**
537
 * Removes the first occurrence of a specified child from the list of children of our Surface.
538 c204c69d leszek
 * <p>
539
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
540 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
541 c204c69d leszek
 *
542
 * @param node The Node to remove.
543
 */
544
  public void detach(DistortedNode node)
545
    {
546 be60d4ff leszek
    mJobs.add(new Job(DETACH,node,null));
547 efe3d8fe leszek
    DistortedMaster.newSlave(this);
548 a09ada4c Leszek Koltunski
    }
549
550
///////////////////////////////////////////////////////////////////////////////////////////////////
551
/**
552
 * Removes all children Nodes.
553 c204c69d leszek
 * <p>
554
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
555 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
556 c204c69d leszek
 */
557
  public void detachAll()
558
    {
559 be60d4ff leszek
    mJobs.add(new Job(DETALL,null,null));
560 efe3d8fe leszek
    DistortedMaster.newSlave(this);
561 c204c69d leszek
    }
562
563
///////////////////////////////////////////////////////////////////////////////////////////////////
564
/**
565
 * This is not really part of the public API. Has to be public only because it is a part of the
566 efe3d8fe leszek
 * DistortedSlave interface, which should really be a class that we extend here instead but
567 c204c69d leszek
 * Java has no multiple inheritance.
568 43fbf0dd leszek
 *
569
 * @y.exclude
570 a09ada4c Leszek Koltunski
 */
571 efe3d8fe leszek
  public void doWork()
572 a09ada4c Leszek Koltunski
    {
573 efe3d8fe leszek
    int num = mJobs.size();
574
    Job job;
575
576
    for(int i=0; i<num; i++)
577
      {
578
      job = mJobs.remove(0);
579
580
      switch(job.type)
581
        {
582 be60d4ff leszek
        case ATTACH: if( mChildren==null ) mChildren = new ArrayList<>(2);
583
                     job.node.setSurfaceParent(this);
584
                     DistortedMaster.addSorted(mChildren,job.node);
585 efe3d8fe leszek
                     mNumChildren++;
586
                     break;
587 be60d4ff leszek
        case DETACH: if( mNumChildren>0 && mChildren.remove(job.node) )
588 efe3d8fe leszek
                       {
589 be60d4ff leszek
                       job.node.setSurfaceParent(null);
590 efe3d8fe leszek
                       mNumChildren--;
591
                       }
592
                     break;
593 be60d4ff leszek
        case DETALL: if( mNumChildren>0 )
594 efe3d8fe leszek
                       {
595 be60d4ff leszek
                       DistortedNode tmp;
596
597
                       for(int j=mNumChildren-1; j>=0; j--)
598
                         {
599
                         tmp = mChildren.remove(j);
600
                         tmp.setSurfaceParent(null);
601
                         }
602
603 efe3d8fe leszek
                       mNumChildren = 0;
604
                       }
605
                     break;
606 be60d4ff leszek
        case SORT  : job.node.setPost(job.dep);
607
                     mChildren.remove(job.node);
608
                     DistortedMaster.addSorted(mChildren,job.node);
609 efe3d8fe leszek
                     break;
610
        }
611
      }
612 a09ada4c Leszek Koltunski
    }
613 af4cc5db Leszek Koltunski
}