Project

General

Profile

« Previous | Next » 

Revision efe3d8fe

Added by Leszek Koltunski about 7 years ago

Messing around with the AttachDaemon (from now on a more generic DistortedMaster).

In preparation for thr SORT job that will let us sort the children Nodes into postprocessing Buckets.

View differences:

src/main/java/org/distorted/library/Distorted.java
134 134
    DistortedNode.onDestroy();
135 135
    EffectQueue.onDestroy();
136 136
    DistortedEffects.onDestroy();
137
    DistortedAttachDaemon.onDestroy();
137
    DistortedMaster.onDestroy();
138 138
    EffectMessageSender.stopSending();
139 139

  
140 140
    mInitialized = false;
src/main/java/org/distorted/library/DistortedAttachDaemon.java
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 java.util.ArrayList;
23

  
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25
/**
26
 * This static class handles changing topology of a Tree in a safe way. I.e. if one wants to
27
 * attach() a new Node somewhere in the Tree, we cannot simply attach it mid-render (detach() is
28
 * even worse!). What we do instead is mark that this job will have to be done and actually do it
29
 * just before the next render. That's what this Daemon does.
30
 */
31
class DistortedAttachDaemon
32
  {
33
  private static final int ATTACH    = 0; //
34
  private static final int DETACH    = 1; // types of jobs that the Daemon can do
35
  private static final int DETACHALL = 2; //
36

  
37
  private static class Job
38
    {
39
    int type;
40
    DistortedAttacheable attacheable;
41
    DistortedNode object;
42

  
43
    Job(int t, DistortedAttacheable a, DistortedNode o)
44
      {
45
      type        = t;
46
      attacheable = a;
47
      object      = o;
48
      }
49
    }
50

  
51
  private static ArrayList<Job> mJobs = new ArrayList<>();
52

  
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

  
55
  private DistortedAttachDaemon()
56
    {
57

  
58
    }
59

  
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

  
62
  static boolean toDo()
63
    {
64
    int num = mJobs.size();
65
    Job job;
66

  
67
    for(int i=0; i<num; i++)
68
      {
69
      job = mJobs.remove(0);
70

  
71
      switch(job.type)
72
        {
73
        case ATTACH   : job.attacheable.attachNow(job.object);
74
                        break;
75
        case DETACH   : job.attacheable.detachNow(job.object);
76
                        break;
77
        case DETACHALL: job.attacheable.detachAllNow()       ;
78
                        break;
79
        default       : android.util.Log.e("AttachDaemon", "what?");
80
        }
81
      }
82

  
83
    return ( num>0 );
84
    }
85

  
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87

  
88
  static void cancelAttachJobs(DistortedAttacheable a, DistortedEffects e)
89
    {
90
    int num = mJobs.size();
91
    Job job;
92

  
93
    for(int i=0; i<num; i++)
94
      {
95
      job = mJobs.get(i);
96

  
97
      if( job.type == ATTACH && job.attacheable==a )
98
        {
99
        DistortedEffects effects = job.object.getEffects();
100

  
101
        if( effects.getID() == e.getID() )
102
          {
103
          mJobs.remove(i);
104
          break;
105
          }
106
        }
107
      }
108
    }
109

  
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

  
112
  static void attach(DistortedAttacheable a, DistortedNode n)
113
    {
114
    mJobs.add(new Job(ATTACH,a,n));
115
    }
116

  
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

  
119
  static void detach(DistortedAttacheable a, DistortedNode n)
120
    {
121
    mJobs.add(new Job(DETACH,a,n));
122
    }
123

  
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

  
126
  static void detachAll(DistortedAttacheable a)
127
    {
128
    mJobs.add(new Job(DETACHALL,a,null));
129
    }
130

  
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132

  
133
  static void onDestroy()
134
    {
135
    mJobs.clear();
136
    }
137
  }
src/main/java/org/distorted/library/DistortedAttacheable.java
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
///////////////////////////////////////////////////////////////////////////////////////////////////
23
/**
24
 * Package-private interface implemented by all objects to which one can attach a Tree-like structure
25
 * of DistortedNodes. (Currently: DistortedNode itself and DistortedOutputSurface).
26
 * <p>
27
 * All the methods below are really meant to be package-local; and this interface should really be a
28
 * package-local class which other classes would extend (but that's impossible because OutputSurface
29
 * already extends other class).
30
 */
31
interface DistortedAttacheable
32
  {
33
  /**
34
   * Not part of public API, do not document
35
   * @y.exclude
36
   */
37
  void attachNow(DistortedNode node);
38
  /**
39
   * Not part of public API, do not document
40
   * @y.exclude
41
   */
42
  void detachNow(DistortedNode node);
43
  /**
44
   * Not part of public API, do not document
45
   * @y.exclude
46
   */
47
  void detachAllNow();
48
  }
src/main/java/org/distorted/library/DistortedMaster.java
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 java.util.ArrayList;
23

  
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25
/**
26
 * This static class handles assigning jobs to other classes. It does it once, at the beginning of
27
 * each frame.
28
 */
29
class DistortedMaster
30
  {
31
  private static ArrayList<DistortedSlave> mSlaves = new ArrayList<>();
32

  
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34

  
35
  private DistortedMaster()
36
    {
37

  
38
    }
39

  
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

  
42
  static boolean toDo()
43
    {
44
    DistortedSlave slave;
45
    int num = mSlaves.size();
46

  
47
    for(int i=0; i<num; i++)
48
      {
49
      slave = mSlaves.remove(0);
50
      slave.doWork();
51
      }
52

  
53
    return ( num>0 );
54
    }
55

  
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

  
58
  static void newSlave(DistortedSlave s)
59
    {
60
    int num = mSlaves.size();
61
    boolean found = false;
62
    DistortedSlave tmp;
63

  
64
    for(int i=0; i<num; i++)
65
      {
66
      tmp = mSlaves.get(i);
67

  
68
      if( tmp==s )
69
        {
70
        found = true;
71
        break;
72
        }
73
      }
74

  
75
    if( !found ) mSlaves.add(s);
76
    }
77

  
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79

  
80
  static void onDestroy()
81
    {
82
    mSlaves.clear();
83
    }
84
  }
src/main/java/org/distorted/library/DistortedNode.java
22 22
import java.util.ArrayList;
23 23
import java.util.HashMap;
24 24

  
25
import android.opengl.GLES30;
26

  
27 25
///////////////////////////////////////////////////////////////////////////////////////////////////
28 26
/**
29 27
 * Class which represents a Node in a Tree of (InputSurface,Mesh,Effects) triplets.
......
35 33
 * to sub-class 'NodeData'. Two identical sub-trees attached at different points of the main tree
36 34
 * will point to the same NodeData; only the first of this is rendered (mData.numRender!).
37 35
 */
38
public class DistortedNode implements DistortedAttacheable
36
public class DistortedNode implements DistortedSlave
39 37
  {
38
  private static final int ATTACH = 0;
39
  private static final int DETACH = 1;
40
  private static final int DETALL = 2;
41
  private static final int SORT   = 3;
42

  
43
  private ArrayList<DistortedNode> mChildren;
44
  private int[] mNumChildren;  // ==mChildren.length(), but we only create mChildren if the first one gets added
45

  
46
  private class Job
47
    {
48
    int type;
49
    DistortedNode node;
50
    DistortedEffectsPostprocess dep;
51

  
52
    Job(int t, DistortedNode n, DistortedEffectsPostprocess d)
53
      {
54
      type = t;
55
      node = n;
56
      dep  = d;
57
      }
58
    }
59

  
60
  private ArrayList<Job> mJobs = new ArrayList<>();
61

  
40 62
  private static HashMap<ArrayList<Long>,NodeData> mMapNodeID = new HashMap<>();
41 63
  private static long mNextNodeID =0;
42 64

  
......
48 70
  private DistortedRenderState mState;
49 71
  private NodeData mData;
50 72

  
51
  private ArrayList<DistortedNode> mChildren;
52
  private int[] mNumChildren;  // ==mChildren.length(), but we only create mChildren if the first one gets added
53

  
54 73
  private class NodeData
55 74
    {
56 75
    long ID;
......
355 374
 * Adds a new child to the last position in the list of our Node's children.
356 375
 * <p>
357 376
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
358
 * DistortedAttachDeamon (by calling attachNow())
377
 * DistortedMaster (by calling doWork())
359 378
 *
360 379
 * @param node The new Node to add.
361 380
 */
362 381
  public void attach(DistortedNode node)
363 382
    {
364
    DistortedAttachDaemon.attach(this,node);
383
    mJobs.add(new Job(ATTACH,node,null));
384
    DistortedMaster.newSlave(this);
365 385
    }
366 386

  
367 387
///////////////////////////////////////////////////////////////////////////////////////////////////
......
369 389
 * Adds a new child to the last position in the list of our Node's children.
370 390
 * <p>
371 391
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
372
 * DistortedAttachDeamon (by calling attachNow())
392
 * DistortedMaster (by calling doWork())
373 393
 *
374 394
 * @param surface InputSurface to initialize our child Node with.
375 395
 * @param effects DistortedEffects to initialize our child Node with.
......
379 399
  public DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
380 400
    {
381 401
    DistortedNode node = new DistortedNode(surface,effects,mesh);
382
    DistortedAttachDaemon.attach(this,node);
402
    mJobs.add(new Job(ATTACH,node,null));
403
    DistortedMaster.newSlave(this);
383 404
    return node;
384 405
    }
385 406

  
386
///////////////////////////////////////////////////////////////////////////////////////////////////
387
/**
388
 * This is not really part of the public API. Has to be public only because it is a part of the
389
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
390
 * Java has no multiple inheritance.
391
 *
392
 * @y.exclude
393
 * @param node The new Node to add.
394
 */
395
  public void attachNow(DistortedNode node)
396
    {
397
    if( mChildren==null ) mChildren = new ArrayList<>(2);
398

  
399
    node.mParent = this;
400
    mChildren.add(node);
401
    mNumChildren[0]++;
402
    adjustIsomorphism();
403
    }
404

  
405 407
///////////////////////////////////////////////////////////////////////////////////////////////////
406 408
/**
407 409
 * Removes the first occurrence of a specified child from the list of children of our Node.
408 410
 * <p>
409 411
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
410
 * DistortedAttachDeamon (by calling detachNow())
412
 * DistortedMaster (by calling doWork())
411 413
 *
412 414
 * @param node The Node to remove.
413 415
 */
414 416
  public void detach(DistortedNode node)
415 417
    {
416
    DistortedAttachDaemon.detach(this,node);
418
    mJobs.add(new Job(DETACH,node,null));
419
    DistortedMaster.newSlave(this);
417 420
    }
418 421

  
419 422
///////////////////////////////////////////////////////////////////////////////////////////////////
......
424 427
 * of them having the same Effects but - for instance - different Mesh. Use with care.
425 428
 * <p>
426 429
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
427
 * DistortedAttachDeamon (by calling detachNow())
430
 * DistortedMaster (by calling doWork())
428 431
 *
429 432
 * @param effects DistortedEffects to remove.
430 433
 */
......
432 435
    {
433 436
    long id = effects.getID();
434 437
    DistortedNode node;
435
    boolean detached= false;
438
    boolean detached = false;
436 439

  
437 440
    for(int i=0; i<mNumChildren[0]; i++)
438 441
      {
439 442
      node = mChildren.get(i);
440 443

  
441
      if( node.mEffects.getID()==id )
444
      if( node.getEffects().getID()==id )
442 445
        {
443
        detached=true;
444
        DistortedAttachDaemon.detach(this,node);
446
        detached = true;
447
        mJobs.add(new Job(DETACH,node,null));
448
        DistortedMaster.newSlave(this);
445 449
        break;
446 450
        }
447 451
      }
......
449 453
    if( !detached )
450 454
      {
451 455
      // if we failed to detach any, it still might be the case that
452
      // there's a job in Daemon's queue that we need to cancel.
453
      DistortedAttachDaemon.cancelAttachJobs(this,effects);
454
      }
455
    }
456
      // there's an ATTACH job that we need to cancel.
457
      int num = mJobs.size();
458
      Job job;
456 459

  
457
///////////////////////////////////////////////////////////////////////////////////////////////////
458
/**
459
 * This is not really part of the public API. Has to be public only because it is a part of the
460
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
461
 * Java has no multiple inheritance.
462
 *
463
 * @y.exclude
464
 * @param node The Node to remove.
465
 */
466
  public void detachNow(DistortedNode node)
467
    {
468
    if( mNumChildren[0]>0 && mChildren.remove(node) )
469
      {
470
      node.mParent = null;
471
      mNumChildren[0]--;
472
      adjustIsomorphism();
460
      for(int i=0; i<num; i++)
461
        {
462
        job = mJobs.get(i);
463

  
464
        if( job.type==ATTACH && job.node.getEffects()==effects )
465
          {
466
          mJobs.remove(i);
467
          break;
468
          }
469
        }
473 470
      }
474 471
    }
475 472

  
......
478 475
 * Removes all children Nodes.
479 476
 * <p>
480 477
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
481
 * DistortedAttachDeamon (by calling detachAllNow())
478
 * DistortedMaster (by calling doWork())
482 479
 */
483 480
  public void detachAll()
484 481
    {
485
    DistortedAttachDaemon.detachAll(this);
482
    mJobs.add(new Job(DETALL,null,null));
483
    DistortedMaster.newSlave(this);
486 484
    }
487 485

  
488 486
///////////////////////////////////////////////////////////////////////////////////////////////////
489 487
/**
490 488
 * This is not really part of the public API. Has to be public only because it is a part of the
491
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
489
 * DistortedSlave interface, which should really be a class that we extend here instead but
492 490
 * Java has no multiple inheritance.
493 491
 *
494 492
 * @y.exclude
495 493
 */
496
  public void detachAllNow()
494
  public void doWork()
497 495
    {
498
    if( mNumChildren[0]>0 )
496
    int num = mJobs.size();
497
    Job job;
498

  
499
    int numChanges=0;
500
    int numSorts  =0;
501

  
502
    for(int i=0; i<num; i++)
499 503
      {
500
      DistortedNode tmp;
504
      job = mJobs.remove(0);
501 505

  
502
      for(int i=mNumChildren[0]-1; i>=0; i--)
506
      switch(job.type)
503 507
        {
504
        tmp = mChildren.remove(i);
505
        tmp.mParent = null;
508
        case ATTACH: numChanges++;
509
                     if( mChildren==null ) mChildren = new ArrayList<>(2);
510
                     job.node.mParent = this;
511
                     mChildren.add(job.node);
512
                     mNumChildren[0]++;
513
                     break;
514
        case DETACH: numChanges++;
515
                     if( mNumChildren[0]>0 && mChildren.remove(job.node) )
516
                       {
517
                       job.node.mParent = null;
518
                       mNumChildren[0]--;
519
                       }
520
                     break;
521
        case DETALL: numChanges++;
522
                     if( mNumChildren[0]>0 )
523
                       {
524
                       DistortedNode tmp;
525

  
526
                       for(int j=mNumChildren[0]-1; j>=0; j--)
527
                         {
528
                         tmp = mChildren.remove(j);
529
                         tmp.mParent = null;
530
                         }
531

  
532
                       mNumChildren[0] = 0;
533
                       }
534
                     break;
535
        case SORT  : numSorts++;
536
                     // TODO: sort
537
                     break;
506 538
        }
539
      }
507 540

  
508
      mNumChildren[0] = 0;
541
    if( numChanges>0 )
542
      {
509 543
      adjustIsomorphism();
544

  
545
      if( numSorts==0 )
546
        {
547
        // TODO: sort
548
        }
510 549
      }
511 550
    }
512

  
513 551
///////////////////////////////////////////////////////////////////////////////////////////////////
514 552
/**
515 553
 * Sets the Postprocessing Effects we will apply to the temporary buffer this Node - and fellow siblings
src/main/java/org/distorted/library/DistortedOutputSurface.java
25 25

  
26 26
///////////////////////////////////////////////////////////////////////////////////////////////////
27 27

  
28
abstract class DistortedOutputSurface extends DistortedSurface implements DistortedAttacheable
28
abstract class DistortedOutputSurface extends DistortedSurface implements DistortedSlave
29 29
{
30
  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

  
30 35
  private ArrayList<DistortedNode> mChildren;
31 36
  private int mNumChildren;   // ==mChildren.length(), but we only create mChildren if the first one gets added
32 37

  
38
  private class Job
39
    {
40
    int type;
41
    DistortedNode node;
42

  
43
    Job(int t, DistortedNode n)
44
      {
45
      type = t;
46
      node = n;
47
      }
48
    }
49

  
50
  private ArrayList<Job> mJobs = new ArrayList<>();
51

  
33 52
  DistortedFramebuffer mBuffer1, mBuffer2;
34 53

  
35 54
  private long mTime;
......
181 200
/*
182 201
    boolean changed =
183 202
*/
184
    DistortedAttachDaemon.toDo();
203
    DistortedMaster.toDo();
185 204
/*
186 205
    // debugging only
187 206
    if( changed )
......
374 393
 * Adds a new child to the last position in the list of our Surface's children.
375 394
 * <p>
376 395
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
377
 * DistortedAttachDeamon (by calling attachNow())
396
 * DistortedMaster (by calling doWork())
378 397
 *
379 398
 * @param node The new Node to add.
380 399
 */
381 400
  public void attach(DistortedNode node)
382 401
    {
383
    DistortedAttachDaemon.attach(this,node);
402
    mJobs.add(new Job(ATTACH,node));
403
    DistortedMaster.newSlave(this);
384 404
    }
385 405

  
386 406
///////////////////////////////////////////////////////////////////////////////////////////////////
......
388 408
 * Adds a new child to the last position in the list of our Surface's children.
389 409
 * <p>
390 410
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
391
 * DistortedAttachDeamon (by calling attachNow())
411
 * DistortedMaster (by calling doWork())
392 412
 *
393 413
 * @param surface InputSurface to initialize our child Node with.
394 414
 * @param effects DistortedEffects to initialize our child Node with.
......
398 418
  public DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
399 419
    {
400 420
    DistortedNode node = new DistortedNode(surface,effects,mesh);
401
    DistortedAttachDaemon.attach(this,node);
421
    mJobs.add(new Job(ATTACH,node));
422
    DistortedMaster.newSlave(this);
402 423
    return node;
403 424
    }
404 425

  
405
///////////////////////////////////////////////////////////////////////////////////////////////////
406
/**
407
 * This is not really part of the public API. Has to be public only because it is a part of the
408
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
409
 * Java has no multiple inheritance.
410
 *
411
 * @y.exclude
412
 * @param node new Node to add.
413
 */
414
  public void attachNow(DistortedNode node)
415
    {
416
    if( mChildren==null ) mChildren = new ArrayList<>(2);
417
    mChildren.add(node);
418
    mNumChildren++;
419
    }
420

  
421 426
///////////////////////////////////////////////////////////////////////////////////////////////////
422 427
/**
423 428
 * Removes the first occurrence of a specified child from the list of children of our Surface.
......
426 431
 * of them having the same Effects but - for instance - different Mesh. Use with care.
427 432
 * <p>
428 433
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
429
 * DistortedAttachDeamon (by calling detachNow())
434
 * DistortedMaster (by calling doWork())
430 435
 *
431 436
 * @param effects DistortedEffects to remove.
432 437
 */
......
443 448
      if( node.getEffects().getID()==id )
444 449
        {
445 450
        detached = true;
446
        DistortedAttachDaemon.detach(this,node);
451
        mJobs.add(new Job(DETACH,node));
452
        DistortedMaster.newSlave(this);
447 453
        break;
448 454
        }
449 455
      }
......
451 457
    if( !detached )
452 458
      {
453 459
      // if we failed to detach any, it still might be the case that
454
      // there's a job in Daemon's queue that we need to cancel.
455
      DistortedAttachDaemon.cancelAttachJobs(this,effects);
460
      // there's an ATTACH job that we need to cancel.
461
      int num = mJobs.size();
462
      Job job;
463

  
464
      for(int i=0; i<num; i++)
465
        {
466
        job = mJobs.get(i);
467

  
468
        if( job.type==ATTACH && job.node.getEffects()==effects )
469
          {
470
          mJobs.remove(i);
471
          break;
472
          }
473
        }
456 474
      }
457 475
    }
458 476

  
......
461 479
 * Removes the first occurrence of a specified child from the list of children of our Surface.
462 480
 * <p>
463 481
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
464
 * DistortedAttachDeamon (by calling detachNow())
482
 * DistortedMaster (by calling doWork())
465 483
 *
466 484
 * @param node The Node to remove.
467 485
 */
468 486
  public void detach(DistortedNode node)
469 487
    {
470
    DistortedAttachDaemon.detach(this,node);
471
    }
472

  
473
///////////////////////////////////////////////////////////////////////////////////////////////////
474
/**
475
 * This is not really part of the public API. Has to be public only because it is a part of the
476
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
477
 * Java has no multiple inheritance.
478
 *
479
 * @y.exclude
480
 * @param node The Node to remove.
481
 */
482
  public void detachNow(DistortedNode node)
483
    {
484
    if( mNumChildren>0 && mChildren.remove(node) )
485
      {
486
      mNumChildren--;
487
      }
488
    mJobs.add(new Job(DETACH,node));
489
    DistortedMaster.newSlave(this);
488 490
    }
489 491

  
490 492
///////////////////////////////////////////////////////////////////////////////////////////////////
......
492 494
 * Removes all children Nodes.
493 495
 * <p>
494 496
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
495
 * DistortedAttachDeamon (by calling detachAllNow())
497
 * DistortedMaster (by calling doWork())
496 498
 */
497 499
  public void detachAll()
498 500
    {
499
    DistortedAttachDaemon.detachAll(this);
501
    mJobs.add(new Job(DETALL,null));
502
    DistortedMaster.newSlave(this);
500 503
    }
501 504

  
502 505
///////////////////////////////////////////////////////////////////////////////////////////////////
503 506
/**
504 507
 * This is not really part of the public API. Has to be public only because it is a part of the
505
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
508
 * DistortedSlave interface, which should really be a class that we extend here instead but
506 509
 * Java has no multiple inheritance.
507 510
 *
508 511
 * @y.exclude
509 512
 */
510
  public void detachAllNow()
513
  public void doWork()
511 514
    {
512
    if( mNumChildren>0 )
515
    int num = mJobs.size();
516
    Job job;
517

  
518
    int numChanges=0;
519
    int numSorts  =0;
520

  
521
    for(int i=0; i<num; i++)
522
      {
523
      job = mJobs.remove(0);
524

  
525
      switch(job.type)
526
        {
527
        case ATTACH: numChanges++;
528
                     if( mChildren==null ) mChildren = new ArrayList<>(2);
529
                     mChildren.add(job.node);
530
                     mNumChildren++;
531
                     break;
532
        case DETACH: numChanges++;
533
                     if( mNumChildren>0 && mChildren.remove(job.node) )
534
                       {
535
                       mNumChildren--;
536
                       }
537
                     break;
538
        case DETALL: numChanges++;
539
                     if( mNumChildren>0 )
540
                       {
541
                       mNumChildren = 0;
542
                       mChildren.clear();
543
                       }
544
                     break;
545
        case SORT  : numSorts++;
546
                     // TODO: sort
547
                     break;
548
        }
549
      }
550

  
551
    if( numChanges>0 && numSorts==0 )
513 552
      {
514
      mNumChildren = 0;
515
      mChildren.clear();
553
      // TODO: sort
516 554
      }
517 555
    }
518 556
}
src/main/java/org/distorted/library/DistortedSlave.java
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
///////////////////////////////////////////////////////////////////////////////////////////////////
23
/**
24
 * Package-private interface implemented by all objects which can be assigned work by the DistortedMaster.
25
 * <p>
26
 * All the methods below are really meant to be package-local; and this interface should really be a
27
 * package-local class which other classes would extend (but that's impossible because OutputSurface
28
 * already extends other class).
29
 */
30
interface DistortedSlave
31
  {
32
  /**
33
   * Not part of public API, do not document
34
   * @y.exclude
35
   */
36
  void doWork();
37
  }

Also available in: Unified diff