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/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
}

Also available in: Unified diff