Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedOutputSurface.java @ f89f791a

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 f89f791a Leszek Koltunski
  private static final int NUM_MIPMAP = 4;
31
          static final int CUR_MIPMAP = 1;
32
33 efe3d8fe leszek
  private static final int ATTACH = 0;
34
  private static final int DETACH = 1;
35
  private static final int DETALL = 2;
36
  private static final int SORT   = 3;
37
38 a09ada4c Leszek Koltunski
  private ArrayList<DistortedNode> mChildren;
39
  private int mNumChildren;   // ==mChildren.length(), but we only create mChildren if the first one gets added
40
41 efe3d8fe leszek
  private class Job
42
    {
43
    int type;
44
    DistortedNode node;
45 be60d4ff leszek
    DistortedEffectsPostprocess dep;
46 efe3d8fe leszek
47 be60d4ff leszek
    Job(int t, DistortedNode n, DistortedEffectsPostprocess d)
48 efe3d8fe leszek
      {
49
      type = t;
50
      node = n;
51 be60d4ff leszek
      dep  = d;
52 efe3d8fe leszek
      }
53
    }
54
55
  private ArrayList<Job> mJobs = new ArrayList<>();
56
57 f89f791a Leszek Koltunski
  DistortedFramebuffer[] mBuffer1, mBuffer2;
58 60c1c622 leszek
59 95c441a2 leszek
  private long mTime;
60 c2c08950 leszek
  private float mFOV;
61 af4cc5db Leszek Koltunski
  int mWidth,mHeight,mDepth;
62 c2c08950 leszek
  float mDistance, mNear;
63 af4cc5db Leszek Koltunski
  float[] mProjectionMatrix;
64
65 c7da4e65 leszek
  int mDepthCreated;
66 a436ccc5 leszek
  int[] mDepthH = new int[1];
67
  int[] mFBOH   = new int[1];
68
69 a9f41fa3 leszek
  private float mClearR, mClearG, mClearB, mClearA;
70
  private float mClearDepth;
71 0afc143a leszek
72
//private String sNew="", sOld="";
73
74 af4cc5db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
75
76 09ab7524 Leszek Koltunski
  DistortedOutputSurface(int width, int height, int createColor, int createDepth, int fbo, int type)
77 af4cc5db Leszek Koltunski
    {
78 09ab7524 Leszek Koltunski
    super(width,height,createColor,type);
79 af4cc5db Leszek Koltunski
80
    mProjectionMatrix = new float[16];
81
82
    mWidth = width;
83
    mHeight= height;
84
85
    mFOV = 60.0f;
86 54fe333a leszek
    mNear=  0.5f;
87 af4cc5db Leszek Koltunski
88 c7da4e65 leszek
    mDepthCreated= createDepth;
89 a436ccc5 leszek
    mFBOH[0]     = fbo;
90 c7da4e65 leszek
    mDepthH[0]   = 0;
91 a436ccc5 leszek
92 95c441a2 leszek
    mTime = 0;
93
94 a9f41fa3 leszek
    mClearR = 0.0f;
95
    mClearG = 0.0f;
96
    mClearB = 0.0f;
97
    mClearA = 0.0f;
98
99
    mClearDepth = 1.0f;
100
101 f89f791a Leszek Koltunski
    mBuffer1 = new DistortedFramebuffer[NUM_MIPMAP];
102
    mBuffer2 = new DistortedFramebuffer[NUM_MIPMAP];
103
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 c2c08950 leszek
        float q = mWidth*mNear;
117 54fe333a leszek
        float c = mHeight*mNear;
118
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
        mDistance    = mHeight/a;
126 af4cc5db Leszek Koltunski
        float far    = 2*mDistance-near;
127
        mDepth       = (int)((far-near)/2);
128
129
        Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
130
        }
131
      else             // parallel projection
132
        {
133 54fe333a leszek
        float left   = -mWidth /2.0f;
134
        float right  = +mWidth /2.0f;
135
        float bottom = -mHeight/2.0f;
136
        float top    = +mHeight/2.0f;
137
        float near   = mWidth+mHeight-mHeight*(1.0f-mNear);
138
        mDistance    = mWidth+mHeight;
139
        float far    = mWidth+mHeight+mHeight*(1.0f-mNear);
140
        mDepth       = (int)((far-near)/2);
141 af4cc5db Leszek Koltunski
142
        Matrix.orthoM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
143
        }
144
      }
145
    }
146
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148 e02264ff leszek
// Render all children, one by one. If there are no postprocessing effects, just render to THIS.
149
// Otherwise, render to a buffer and on each change of Postprocessing Bucket, apply the postprocessing
150
// to a whole buffer and merge it.
151 39086ebb leszek
152 b2939df4 Leszek Koltunski
  int renderChildren(long time, int num, ArrayList<DistortedNode> children)
153
    {
154
    int numRenders = 0;
155 b9798977 leszek
    DistortedNode child;
156 27f42cd6 leszek
    DistortedEffectsPostprocess lastP=null, currP;
157 e137d4f5 Leszek Koltunski
    long lastB=0, currB;
158 0afc143a leszek
159
//sNew = "";
160
161 b2939df4 Leszek Koltunski
    for(int i=0; i<num; i++)
162 39086ebb leszek
      {
163 b9798977 leszek
      child = children.get(i);
164 27f42cd6 leszek
      currP = child.getEffectsPostprocess();
165 13687207 leszek
      currB = currP==null ? 0 : currP.getBucket();
166 0afc143a leszek
167
//sNew += currB;
168
169 13687207 leszek
      if( lastB!=currB && lastB!=0 )
170
        {
171
        numRenders += lastP.postprocess(time,this);
172
        }
173 b9798977 leszek
174 13687207 leszek
      if( currB==0 )
175
        {
176
        numRenders += child.draw(time,this);
177
        }
178 60c1c622 leszek
      else
179
        {
180 f89f791a Leszek Koltunski
        if( mBuffer1[0]==null )
181 60c1c622 leszek
          {
182 f89f791a Leszek Koltunski
          int sizeX = mWidth;
183
          int sizeY = mHeight;
184
185
          for(int j=0; j<NUM_MIPMAP; j++)
186
            {
187
            mBuffer1[j] = new DistortedFramebuffer(mDepthCreated!=DONT_CREATE, DistortedSurface.TYPE_SYST, sizeX, sizeY);
188
            mBuffer2[j] = new DistortedFramebuffer(false                     , DistortedSurface.TYPE_SYST, sizeX, sizeY);
189
190
            sizeX *= 0.9f;
191
            sizeY *= 0.9f;
192
            }
193 60c1c622 leszek
          }
194 b9798977 leszek
195 f89f791a Leszek Koltunski
        numRenders += child.draw(time,mBuffer1[CUR_MIPMAP]);
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 a9f41fa3 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
305
/**
306
 * Set the (R,G,B,A) values of GLES30.glClearColor() to set up color with which to clear
307
 * this Surface before each render.
308
 *
309
 * @param r the Red component. Default: 0.0f
310
 * @param g the Green component. Default: 0.0f
311
 * @param b the Blue component. Default: 0.0f
312
 * @param a the Alpha component. Default: 0.0f
313
 */
314
  public void glClearColor(float r, float g, float b, float a)
315
    {
316
    mClearR = r;
317
    mClearG = g;
318
    mClearB = b;
319
    mClearA = a;
320
    }
321
322
///////////////////////////////////////////////////////////////////////////////////////////////////
323
/**
324
 * Set the Depth value of GLES30.glClearDepthf() to set up depth with which to clear
325
 * the Depth buffer of Surface before each render.
326
 *
327
 * @param d the Depth. Default: 1.0f
328
 */
329
  public void glClearDepthf(float d)
330
    {
331
    mClearDepth = d;
332
    }
333
334 af4cc5db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
335
/**
336
 * Create new Projection matrix.
337
 *
338
 * @param fov Vertical 'field of view' of the Projection frustrum (in degrees).
339 54fe333a leszek
 *            Valid values: 0<=fov<180. FOV==0 means 'parallel projection'.
340
 * @param near Distance between the screen plane and the near plane.
341
 *             Valid vaules: 0<near<1. When near==0, the Near Plane is exactly at the tip of the
342
 *             pyramid. When near==1 (illegal!) the near plane is equivalent to the screen plane.
343 af4cc5db Leszek Koltunski
 */
344 54fe333a leszek
  public void setProjection(float fov, float near)
345 af4cc5db Leszek Koltunski
    {
346 8069e806 Leszek Koltunski
    if( fov < 180.0f && fov >=0.0f )
347
      {
348
      mFOV = fov;
349
      }
350
351
    if( near<   1.0f && near> 0.0f )
352
      {
353
      mNear= near;
354
      }
355
    else if( near<=0.0f )
356
      {
357
      mNear = 0.01f;
358
      }
359
    else if( near>=1.0f )
360
      {
361
      mNear=0.99f;
362
      }
363 af4cc5db Leszek Koltunski
364
    createProjection();
365
    }
366
367
///////////////////////////////////////////////////////////////////////////////////////////////////
368 c5369f1b leszek
/**
369 af4cc5db Leszek Koltunski
 * Resize the underlying Framebuffer.
370 c7da4e65 leszek
 * <p>
371
 * This method can be safely called mid-render as it doesn't interfere with rendering.
372 af4cc5db Leszek Koltunski
 *
373
 * @param width The new width.
374
 * @param height The new height.
375 c5369f1b leszek
 */
376 af4cc5db Leszek Koltunski
  public void resize(int width, int height)
377
    {
378
    if( mWidth!=width || mHeight!=height )
379
      {
380
      mWidth = width;
381
      mHeight= height;
382
      mSizeX = width;
383
      mSizeY = height;
384 f8377ef8 leszek
385
      createProjection();
386
387 c7da4e65 leszek
      if( mColorCreated==CREATED )
388 f8377ef8 leszek
        {
389 f28fffc2 Leszek Koltunski
        markForCreation();
390 f8377ef8 leszek
        recreate();
391
        }
392 af4cc5db Leszek Koltunski
      }
393
    }
394 a09ada4c Leszek Koltunski
395 a436ccc5 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
396
/**
397
 * Create a new DEPTH buffer and attach it or (param=false) detach an existing DEPTH attachment and recreate it.
398
 *
399
 * @param enable <bold>true</bold> if we want to attach a new DEPTH buffer to the FBO.<br>
400
 *               <bold>false</bold> if we want to detach the DEPTH attachment.
401
 */
402
  public void enableDepth(boolean enable)
403
    {
404 c7da4e65 leszek
    if( enable && mDepthCreated==DONT_CREATE )
405
      {
406
      mDepthCreated = NOT_CREATED_YET;
407 f28fffc2 Leszek Koltunski
      markForCreation();
408 c7da4e65 leszek
      }
409
    if( !enable && mDepthCreated!=DONT_CREATE )
410 a436ccc5 leszek
      {
411 c7da4e65 leszek
      mDepthCreated = DONT_CREATE;
412 f28fffc2 Leszek Koltunski
      markForCreation();
413 a436ccc5 leszek
      }
414
    }
415
416
///////////////////////////////////////////////////////////////////////////////////////////////////
417
/**
418
 * Return true if the Surface contains a DEPTH attachment.
419
 *
420
 * @return <bold>true</bold> if the FBO contains a DEPTH attachment.
421
 */
422
  public boolean hasDepth()
423
    {
424 c7da4e65 leszek
    return mDepthCreated==CREATED;
425 a436ccc5 leszek
    }
426
427 a09ada4c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
428
/**
429
 * Adds a new child to the last position in the list of our Surface's children.
430 c204c69d leszek
 * <p>
431
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
432 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
433 a09ada4c Leszek Koltunski
 *
434
 * @param node The new Node to add.
435
 */
436 c204c69d leszek
  public void attach(DistortedNode node)
437 a09ada4c Leszek Koltunski
    {
438 be60d4ff leszek
    mJobs.add(new Job(ATTACH,node,null));
439 efe3d8fe leszek
    DistortedMaster.newSlave(this);
440 a09ada4c Leszek Koltunski
    }
441
442
///////////////////////////////////////////////////////////////////////////////////////////////////
443
/**
444
 * Adds a new child to the last position in the list of our Surface's children.
445 c204c69d leszek
 * <p>
446
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
447 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
448 a09ada4c Leszek Koltunski
 *
449
 * @param surface InputSurface to initialize our child Node with.
450
 * @param effects DistortedEffects to initialize our child Node with.
451
 * @param mesh MeshObject to initialize our child Node with.
452
 * @return the newly constructed child Node, or null if we couldn't allocate resources.
453
 */
454 c204c69d leszek
  public DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
455 a09ada4c Leszek Koltunski
    {
456
    DistortedNode node = new DistortedNode(surface,effects,mesh);
457 be60d4ff leszek
    mJobs.add(new Job(ATTACH,node,null));
458 efe3d8fe leszek
    DistortedMaster.newSlave(this);
459 c204c69d leszek
    return node;
460
    }
461
462 af27df87 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
463
/**
464
 * Removes the first occurrence of a specified child from the list of children of our Surface.
465
 * <p>
466
 * A bit questionable method as there can be many different Nodes attached as children, some
467
 * of them having the same Effects but - for instance - different Mesh. Use with care.
468
 * <p>
469
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
470 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
471 af27df87 leszek
 *
472
 * @param effects DistortedEffects to remove.
473
 */
474
  public void detach(DistortedEffects effects)
475
    {
476
    long id = effects.getID();
477
    DistortedNode node;
478 8baa1fe6 Leszek Koltunski
    boolean detached = false;
479 af27df87 leszek
480
    for(int i=0; i<mNumChildren; i++)
481
      {
482
      node = mChildren.get(i);
483
484
      if( node.getEffects().getID()==id )
485
        {
486 8baa1fe6 Leszek Koltunski
        detached = true;
487 be60d4ff leszek
        mJobs.add(new Job(DETACH,node,null));
488 efe3d8fe leszek
        DistortedMaster.newSlave(this);
489 af27df87 leszek
        break;
490
        }
491
      }
492 8baa1fe6 Leszek Koltunski
493
    if( !detached )
494
      {
495
      // if we failed to detach any, it still might be the case that
496 efe3d8fe leszek
      // there's an ATTACH job that we need to cancel.
497
      int num = mJobs.size();
498
      Job job;
499
500
      for(int i=0; i<num; i++)
501
        {
502
        job = mJobs.get(i);
503
504
        if( job.type==ATTACH && job.node.getEffects()==effects )
505
          {
506
          mJobs.remove(i);
507
          break;
508
          }
509
        }
510 8baa1fe6 Leszek Koltunski
      }
511 af27df87 leszek
    }
512
513 a09ada4c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
514
/**
515
 * Removes the first occurrence of a specified child from the list of children of our Surface.
516 c204c69d leszek
 * <p>
517
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
518 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
519 c204c69d leszek
 *
520
 * @param node The Node to remove.
521
 */
522
  public void detach(DistortedNode node)
523
    {
524 be60d4ff leszek
    mJobs.add(new Job(DETACH,node,null));
525 efe3d8fe leszek
    DistortedMaster.newSlave(this);
526 a09ada4c Leszek Koltunski
    }
527
528
///////////////////////////////////////////////////////////////////////////////////////////////////
529
/**
530
 * Removes all children Nodes.
531 c204c69d leszek
 * <p>
532
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
533 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
534 c204c69d leszek
 */
535
  public void detachAll()
536
    {
537 be60d4ff leszek
    mJobs.add(new Job(DETALL,null,null));
538 efe3d8fe leszek
    DistortedMaster.newSlave(this);
539 c204c69d leszek
    }
540
541
///////////////////////////////////////////////////////////////////////////////////////////////////
542
/**
543
 * This is not really part of the public API. Has to be public only because it is a part of the
544 efe3d8fe leszek
 * DistortedSlave interface, which should really be a class that we extend here instead but
545 c204c69d leszek
 * Java has no multiple inheritance.
546 43fbf0dd leszek
 *
547
 * @y.exclude
548 a09ada4c Leszek Koltunski
 */
549 efe3d8fe leszek
  public void doWork()
550 a09ada4c Leszek Koltunski
    {
551 efe3d8fe leszek
    int num = mJobs.size();
552
    Job job;
553
554
    for(int i=0; i<num; i++)
555
      {
556
      job = mJobs.remove(0);
557
558
      switch(job.type)
559
        {
560 be60d4ff leszek
        case ATTACH: if( mChildren==null ) mChildren = new ArrayList<>(2);
561
                     job.node.setSurfaceParent(this);
562
                     DistortedMaster.addSorted(mChildren,job.node);
563 efe3d8fe leszek
                     mNumChildren++;
564
                     break;
565 be60d4ff leszek
        case DETACH: if( mNumChildren>0 && mChildren.remove(job.node) )
566 efe3d8fe leszek
                       {
567 be60d4ff leszek
                       job.node.setSurfaceParent(null);
568 efe3d8fe leszek
                       mNumChildren--;
569
                       }
570
                     break;
571 be60d4ff leszek
        case DETALL: if( mNumChildren>0 )
572 efe3d8fe leszek
                       {
573 be60d4ff leszek
                       DistortedNode tmp;
574
575
                       for(int j=mNumChildren-1; j>=0; j--)
576
                         {
577
                         tmp = mChildren.remove(j);
578
                         tmp.setSurfaceParent(null);
579
                         }
580
581 efe3d8fe leszek
                       mNumChildren = 0;
582
                       }
583
                     break;
584 be60d4ff leszek
        case SORT  : job.node.setPost(job.dep);
585
                     mChildren.remove(job.node);
586
                     DistortedMaster.addSorted(mChildren,job.node);
587 efe3d8fe leszek
                     break;
588
        }
589
      }
590 a09ada4c Leszek Koltunski
    }
591 af4cc5db Leszek Koltunski
}