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

Also available in: Unified diff