vtkPVDataDeliveryManager.cxx 25.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*=========================================================================

  Program:   ParaView
  Module:    $RCSfile$

  Copyright (c) Kitware, Inc.
  All rights reserved.
  See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
15
#include "vtkPVDataDeliveryManager.h"
16 17

#include "vtkAlgorithmOutput.h"
18
#include "vtkAMRBox.h"
19
#include "vtkAMRVolumeRepresentation.h"
20
#include "vtkBSPCutsGenerator.h"
21
#include "vtkCamera.h"
22 23
#include "vtkDataObject.h"
#include "vtkMath.h"
24
#include "vtkMPIMoveData.h"
25
#include "vtkMultiBlockDataSet.h"
26 27 28 29
#include "vtkMultiProcessController.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkOrderedCompositeDistributor.h"
30
#include "vtkOverlappingAMR.h"
31
#include "vtkPKdTree.h"
32
#include "vtkPointData.h"
33 34
#include "vtkPVDataRepresentation.h"
#include "vtkPVDataRepresentationPipeline.h"
35
#include "vtkPVRenderView.h"
36
#include "vtkPVStreamingMacros.h"
37
#include "vtkPVTrivialProducer.h"
38
#include "vtkRenderer.h"
39
#include "vtkSmartPointer.h"
40
#include "vtkStreamingDemandDrivenPipeline.h"
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
41
#include "vtkTimerLog.h"
42 43 44
#include "vtkUniformGridAMRDataIterator.h"
#include "vtkUniformGrid.h"
#include "vtkUnsignedIntArray.h"
45
#include "vtkWeakPointer.h"
46 47

#include <assert.h>
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
#include <map>
#include <queue>
#include <utility>

//*****************************************************************************
class vtkPVDataDeliveryManager::vtkInternals
{
public:

  class vtkPriorityQueueItem
    {
  public:
    unsigned int RepresentationId;
    unsigned int BlockId;
    unsigned int Level;
    unsigned int Index;
    double Priority;

    vtkPriorityQueueItem() :
      RepresentationId(0), BlockId(0),
      Level(0), Index(0), Priority(0)
    {
    }

    bool operator < (const vtkPriorityQueueItem& other) const
      {
      return this->Priority < other.Priority;
      }
    };

  typedef std::priority_queue<vtkPriorityQueueItem> PriorityQueueType;
  PriorityQueueType PriorityQueue;

  class vtkItem
    {
    vtkSmartPointer<vtkPVTrivialProducer> Producer;
84 85

    // Data object produced by the representation.
86
    vtkWeakPointer<vtkDataObject> DataObject;
87 88 89 90 91 92 93 94

    // Data object available after delivery to the "rendering" node.
    vtkSmartPointer<vtkDataObject> DeliveredDataObject;

    // Data object after re-distributing when using ordered compositing, for
    // example.
    vtkSmartPointer<vtkDataObject> RedistributedDataObject;

95 96 97
    // Data object for a streamed piece.
    vtkSmartPointer<vtkDataObject> StreamedPiece;

98 99 100
    unsigned long TimeStamp;
    unsigned long ActualMemorySize;
  public:
101 102 103
    vtkWeakPointer<vtkAlgorithmOutput> ImageDataProducer;
      // <-- HACK for image data volume rendering.

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    vtkWeakPointer<vtkPVDataRepresentation> Representation;
    bool AlwaysClone;
    bool Redistributable;
    bool Streamable;

    vtkItem() :
      Producer(vtkSmartPointer<vtkPVTrivialProducer>::New()),
      TimeStamp(0),
      ActualMemorySize(0),
      AlwaysClone(false),
      Redistributable(false),
      Streamable(false)
    { }

    void SetDataObject(vtkDataObject* data)
119
      {
120 121
      this->DataObject = data;
      this->ActualMemorySize = data? data->GetActualMemorySize() : 0;
122 123 124

      vtkTimeStamp ts; ts.Modified();
      this->TimeStamp = ts;
125 126
      }

127 128
    void SetDeliveredDataObject(vtkDataObject* data)
      {
129 130 131 132 133 134
      this->DeliveredDataObject = data;
      }

    void SetRedistributedDataObject(vtkDataObject* data)
      {
      this->RedistributedDataObject = data;
135 136 137 138
      }

    vtkDataObject* GetDeliveredDataObject()
      {
139 140 141 142 143 144 145 146 147
      return this->DeliveredDataObject.GetPointer();
      }

    vtkDataObject* GetRedistributedDataObject()
      {
      return this->RedistributedDataObject.GetPointer();
      }

    vtkPVTrivialProducer* GetProducer(bool use_redistributed_data)
148
      {
149 150 151 152 153 154 155 156 157
      if (use_redistributed_data && this->Redistributable)
        {
        this->Producer->SetOutput(this->RedistributedDataObject);
        }
      else
        {
        this->Producer->SetOutput(this->DeliveredDataObject);
        }
      return this->Producer.GetPointer();
158 159
      }

160 161 162 163
    vtkDataObject* GetDataObject() const
      { return this->DataObject.GetPointer(); }
    unsigned long GetTimeStamp() const
      { return this->TimeStamp; }
164

165 166 167 168 169 170 171 172
    unsigned long GetVisibleDataSize()
      {
      if (this->Representation && this->Representation->GetVisibility())
        {
        return this->ActualMemorySize;
        }
      return 0;
      }
173 174 175 176 177 178 179 180 181

    void SetNextStreamedPiece(vtkDataObject* data)
      {
      this->StreamedPiece = data;
      }
    vtkDataObject* GetStreamedPiece()
      {
      return this->StreamedPiece;
      }
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
    };

  typedef std::map<unsigned int, std::pair<vtkItem, vtkItem> > ItemsMapType;

  vtkItem* GetItem(unsigned int index, bool use_second)
    {
    if (this->ItemsMap.find(index) != this->ItemsMap.end())
      {
      return use_second? &(this->ItemsMap[index].second) :
        &(this->ItemsMap[index].first);
      }
    return NULL;
    }

  vtkItem* GetItem(vtkPVDataRepresentation* repr, bool use_second)
    {
    RepresentationToIdMapType::iterator iter =
      this->RepresentationToIdMap.find(repr);
    if (iter != this->RepresentationToIdMap.end())
      {
      unsigned int index = iter->second;
      return use_second? &(this->ItemsMap[index].second) :
        &(this->ItemsMap[index].first);
      }

    return NULL;
    }

  unsigned long GetVisibleDataSize(bool use_second_if_available)
    {
    unsigned long size = 0;
    ItemsMapType::iterator iter;
    for (iter = this->ItemsMap.begin(); iter != this->ItemsMap.end(); ++iter)
      {
      if (use_second_if_available && iter->second.second.GetDataObject())
        {
        size += iter->second.second.GetVisibleDataSize();
        }
      else
        {
        size += iter->second.first.GetVisibleDataSize();
        }
      }
    return size;
    }
227

228 229 230 231 232 233 234
  typedef std::map<vtkPVDataRepresentation*, unsigned int>
    RepresentationToIdMapType;

  RepresentationToIdMapType RepresentationToIdMap;
  ItemsMapType ItemsMap;
};
//*****************************************************************************
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
namespace
{
  // this code is stolen from vtkFrustumCoverageCuller.
  double vtkComputeScreenCoverage(const double planes[24],
    const double bounds[6], double &distance)
    {
    distance = 0.0;

    // a duff dataset like a polydata with no cells will have bad bounds
    if (!vtkMath::AreBoundsInitialized(const_cast<double*>(bounds)))
      {
      return 0.0;
      }
    double screen_bounds[4];

    double center[3];
    center[0] = (bounds[0] + bounds[1]) / 2.0;
    center[1] = (bounds[2] + bounds[3]) / 2.0;
    center[2] = (bounds[4] + bounds[5]) / 2.0;
    double radius = 0.5 * sqrt(
      ( bounds[1] - bounds[0] ) * ( bounds[1] - bounds[0] ) +
      ( bounds[3] - bounds[2] ) * ( bounds[3] - bounds[2] ) +
      ( bounds[5] - bounds[4] ) * ( bounds[5] - bounds[4] ) );
    for (int i = 0; i < 6; i++ )
      {
      // Compute how far the center of the sphere is from this plane
      double d = planes[i*4 + 0] * center[0] +
        planes[i*4 + 1] * center[1] +
        planes[i*4 + 2] * center[2] +
        planes[i*4 + 3];
      // If d < -radius the prop is not within the view frustum
      if ( d < -radius )
        {
        return 0.0;
        }

      // The first four planes are the ones bounding the edges of the
      // view plane (the last two are the near and far planes) The
      // distance from the edge of the sphere to these planes is stored
      // to compute coverage.
      if ( i < 4 )
        {
        screen_bounds[i] = d - radius;
        }
      // The fifth plane is the near plane - use the distance to
      // the center (d) as the value to sort by
      if ( i == 4 )
        {
        distance = d;
        }
      }

    // If the prop wasn't culled during the plane tests...
    // Compute the width and height of this slice through the
    // view frustum that contains the center of the sphere
    double full_w = screen_bounds[0] + screen_bounds[1] + 2.0 * radius;
    double full_h = screen_bounds[2] + screen_bounds[3] + 2.0 * radius;
    // Subtract from the full width to get the width of the square
    // enclosing the circle slice from the sphere in the plane
    // through the center of the sphere. If the screen bounds for
    // the left and right planes (0,1) are greater than zero, then
    // the edge of the sphere was a positive distance away from the
    // plane, so there is a gap between the edge of the plane and
    // the edge of the box.
    double part_w = full_w;
    if ( screen_bounds[0] > 0.0 )
      {
      part_w -= screen_bounds[0];
      }
    if ( screen_bounds[1] > 0.0 )
      {
      part_w -= screen_bounds[1];
      }
    // Do the same thing for the height with the top and bottom
    // planes (2,3).
    double part_h = full_h;
    if ( screen_bounds[2] > 0.0 )
      {
      part_h -= screen_bounds[2];
      }
    if ( screen_bounds[3] > 0.0 )
      {
      part_h -= screen_bounds[3];
      }

    // Compute the fraction of coverage
    if ((full_w * full_h)!=0.0)
      {
      return (part_w * part_h) / (full_w * full_h);
      }

    return 0;
    }
};

330 331 332
//*****************************************************************************

vtkStandardNewMacro(vtkPVDataDeliveryManager);
333
//----------------------------------------------------------------------------
334
vtkPVDataDeliveryManager::vtkPVDataDeliveryManager()
335 336 337 338 339
  : Internals(new vtkInternals())
{
}

//----------------------------------------------------------------------------
340
vtkPVDataDeliveryManager::~vtkPVDataDeliveryManager()
341 342 343 344 345
{
  delete this->Internals;
  this->Internals = 0;
}

346
//----------------------------------------------------------------------------
347
void vtkPVDataDeliveryManager::SetRenderView(vtkPVRenderView* view)
348
{
349
  this->RenderView = view;
350 351
}

352
//----------------------------------------------------------------------------
353
vtkPVRenderView* vtkPVDataDeliveryManager::GetRenderView()
354
{
355
  return this->RenderView;
356 357
}

Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
358
//----------------------------------------------------------------------------
359
unsigned long vtkPVDataDeliveryManager::GetVisibleDataSize(bool low_res)
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
360 361 362
{
  return this->Internals->GetVisibleDataSize(low_res);
}
363 364

//----------------------------------------------------------------------------
365
void vtkPVDataDeliveryManager::RegisterRepresentation(
366
  unsigned int id, vtkPVDataRepresentation* repr)
367 368 369 370 371
{
  this->Internals->RepresentationToIdMap[repr] = id;

  vtkInternals::vtkItem item;
  item.Representation = repr;
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
372 373 374 375 376
  this->Internals->ItemsMap[id].first = item;

  vtkInternals::vtkItem item2;
  item2.Representation = repr;
  this->Internals->ItemsMap[id].second= item2;
377 378 379
}

//----------------------------------------------------------------------------
380
void vtkPVDataDeliveryManager::UnRegisterRepresentation(
381 382 383 384 385 386 387 388 389 390 391 392 393
  vtkPVDataRepresentation* repr)
{
  vtkInternals::RepresentationToIdMapType::iterator iter =
    this->Internals->RepresentationToIdMap.find(repr);
  if (iter == this->Internals->RepresentationToIdMap.end())
    {
    vtkErrorMacro("Invalid argument.");
    return;
    }
  this->Internals->ItemsMap.erase(iter->second);
  this->Internals->RepresentationToIdMap.erase(iter);
}

394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
//----------------------------------------------------------------------------
unsigned int vtkPVDataDeliveryManager::GetRepresentationId(
  vtkPVDataRepresentation* repr)
{
  vtkInternals::RepresentationToIdMapType::iterator iter =
    this->Internals->RepresentationToIdMap.find(repr);
  if (iter == this->Internals->RepresentationToIdMap.end())
    {
    vtkErrorMacro("Invalid argument.");
    return 0;
    }
  return iter->second;
}

//----------------------------------------------------------------------------
vtkPVDataRepresentation* vtkPVDataDeliveryManager::GetRepresentation(
  unsigned int index)
{
  vtkInternals::vtkItem* item = this->Internals->GetItem(index, false);
  return item? item->Representation : NULL;
}

416
//----------------------------------------------------------------------------
417
void vtkPVDataDeliveryManager::SetDeliverToAllProcesses(
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
418
  vtkPVDataRepresentation* repr, bool mode, bool low_res)
419
{
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
420
  vtkInternals::vtkItem* item = this->Internals->GetItem(repr, low_res);
421 422
  if (item)
    {
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
423
    item->AlwaysClone = mode;
424 425 426 427 428 429 430
    }
  else
    {
    vtkErrorMacro("Invalid argument.");
    }
}

431
//----------------------------------------------------------------------------
432
void vtkPVDataDeliveryManager::MarkAsRedistributable(
433 434 435 436 437 438 439 440 441 442 443 444 445 446
  vtkPVDataRepresentation* repr)
{
  vtkInternals::vtkItem* item = this->Internals->GetItem(repr, false);
  vtkInternals::vtkItem* low_item = this->Internals->GetItem(repr, true);
  if (item)
    {
    item->Redistributable = true;
    low_item->Redistributable = true;
    }
  else
    {
    vtkErrorMacro("Invalid argument.");
    }
}
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
447

448
//----------------------------------------------------------------------------
449
void vtkPVDataDeliveryManager::SetStreamable(
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
  vtkPVDataRepresentation* repr, bool val)
{
  vtkInternals::vtkItem* item = this->Internals->GetItem(repr, false);
  vtkInternals::vtkItem* low_item = this->Internals->GetItem(repr, true);
  if (item)
    {
    item->Streamable = val;
    low_item->Streamable = val;
    }
  else
    {
    vtkErrorMacro("Invalid argument.");
    }
}

465
//----------------------------------------------------------------------------
466
void vtkPVDataDeliveryManager::SetPiece(
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
467
  vtkPVDataRepresentation* repr, vtkDataObject* data, bool low_res)
468
{
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
469
  vtkInternals::vtkItem* item = this->Internals->GetItem(repr, low_res);
470 471
  if (item)
    {
472 473
    vtkPVDataRepresentationPipeline* executive =
      vtkPVDataRepresentationPipeline::SafeDownCast(repr->GetExecutive());
474 475

    // SetPiece() is called in every REQUEST_UPDATE() or REQUEST_UPDATE_LOD()
476
    // pass irrespective of whether the data has actually changed.
477 478 479 480 481 482 483 484 485 486
    // (I think that's a mistake, but the fact that representations can be
    // updated without view makes it tricky since we cannot set the data to
    // deliver in vtkPVDataRepresentation::RequestData() easily). Hence we need
    // to ensure that the data we are getting is newer than what we have.
    unsigned long data_time = executive? executive->GetDataTime() : 0;
    if (data && (data->GetMTime() > data_time))
      {
      data_time = data->GetMTime();
      }
    if (data_time > item->GetTimeStamp())
487 488 489
      {
      item->SetDataObject(data);
      }
490 491
    }
  else
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
492 493 494 495 496 497
    {
    vtkErrorMacro("Invalid argument.");
    }
}

//----------------------------------------------------------------------------
498
vtkAlgorithmOutput* vtkPVDataDeliveryManager::GetProducer(
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
499 500 501 502
  vtkPVDataRepresentation* repr, bool low_res)
{
  vtkInternals::vtkItem* item = this->Internals->GetItem(repr, low_res);
  if (!item)
503 504
    {
    vtkErrorMacro("Invalid arguments.");
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
505 506 507
    return NULL;
    }

508 509
  return item->GetProducer(
    this->RenderView->GetUseOrderedCompositing())->GetOutputPort(0);
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
510 511 512
}

//----------------------------------------------------------------------------
513
void vtkPVDataDeliveryManager::SetPiece(unsigned int id, vtkDataObject* data, bool
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
514 515 516 517 518 519 520 521 522 523
  low_res)
{
  vtkInternals::vtkItem* item = this->Internals->GetItem(id, low_res);
  if (item)
    {
    item->SetDataObject(data);
    }
  else
    {
    vtkErrorMacro("Invalid argument.");
524 525 526
    }
}

527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
//----------------------------------------------------------------------------
void vtkPVDataDeliveryManager::SetImageDataProducer(
  vtkPVDataRepresentation* repr, vtkAlgorithmOutput *producer)
{
  vtkInternals::vtkItem* item = this->Internals->GetItem(repr, false);
  if (item)
    {
    item->ImageDataProducer = producer;
    }
  else
    {
    vtkErrorMacro("Invalid argument.");
    }
}

542
//----------------------------------------------------------------------------
543
vtkAlgorithmOutput* vtkPVDataDeliveryManager::GetProducer(
544
  unsigned int id, bool low_res)
545
{
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
546
  vtkInternals::vtkItem* item = this->Internals->GetItem(id, low_res);
547 548 549 550 551 552
  if (!item)
    {
    vtkErrorMacro("Invalid arguments.");
    return NULL;
    }

553 554
  return item->GetProducer(
    this->RenderView->GetUseOrderedCompositing())->GetOutputPort(0);
555 556
}

557
//----------------------------------------------------------------------------
558
bool vtkPVDataDeliveryManager::NeedsDelivery(
559
  unsigned long timestamp,
560
  std::vector<unsigned int> &keys_to_deliver, bool use_low)
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
{
  vtkInternals::ItemsMapType::iterator iter;
  for (iter = this->Internals->ItemsMap.begin();
    iter != this->Internals->ItemsMap.end(); ++iter)
    {
    vtkInternals::vtkItem& item = use_low? iter->second.second : iter->second.first;
    if (item.Representation &&
      item.Representation->GetVisibility() &&
      item.GetTimeStamp() > timestamp)
      {
      keys_to_deliver.push_back(iter->first);
      }
    }
  return keys_to_deliver.size() > 0;
}

//----------------------------------------------------------------------------
578
void vtkPVDataDeliveryManager::Deliver(int use_lod, unsigned int size, unsigned int *values)
579 580

{
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
  // This method gets called on all processes with the list of representations
  // to "deliver". We check with the view what mode we're operating in and
  // decide where the data needs to be delivered.
  //
  // Representations can provide overrides, e.g. though the view says data is
  // merely "pass-through", some representation says we need to clone the data
  // everywhere. That makes it critical that this method is called on all
  // processes at the same time to avoid deadlocks and other complications.
  //
  // This method will be implemented in "view-specific" subclasses since how the
  // data is delivered is very view specific.

  vtkTimerLog::MarkStartEvent(use_lod?
    "LowRes Data Migration" : "FullRes Data Migration");

  bool using_remote_rendering =
597 598 599
    use_lod? this->RenderView->GetUseDistributedRenderingForInteractiveRender() :
    this->RenderView->GetUseDistributedRenderingForStillRender();
  int mode = this->RenderView->GetDataDistributionMode(using_remote_rendering);
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
600

601 602 603 604 605 606
  for (unsigned int cc=0; cc < size; cc++)
    {
    vtkInternals::vtkItem* item = this->Internals->GetItem(values[cc], use_lod !=0);

    vtkDataObject* data = item->GetDataObject();

607
    if (data->IsA("vtkUniformGridAMR"))
608
      {
609 610
      // we are dealing with AMR datasets.
      // We assume for now we're not running in render-server mode. We can
611
      // ensure that at some point in future.
612 613 614
      // So we are either in pass-through or collect mode.

      // FIXME: check that the mode flags are "suitable" for AMR.
615
      }
616

617 618 619 620 621
    vtkNew<vtkMPIMoveData> dataMover;
    dataMover->InitializeForCommunicationForParaView();
    dataMover->SetOutputDataType(data->GetDataObjectType());
    dataMover->SetMoveMode(mode);
    if (item->AlwaysClone)
622
      {
623
      dataMover->SetMoveModeToClone();
624
      }
625 626
    dataMover->SetInputData(data);

627 628 629 630 631 632
    if (dataMover->GetOutputGeneratedOnProcess())
      {
      // release old memory (not necessarily, but try).
      item->SetDeliveredDataObject(NULL);
      }
    dataMover->Update();
633
    if (item->GetDeliveredDataObject() == NULL)
634 635 636
      {
      item->SetDeliveredDataObject(dataMover->GetOutputDataObject(0));
      }
637
    }
638

639 640 641
  vtkTimerLog::MarkEndEvent(use_lod?
    "LowRes Data Migration" : "FullRes Data Migration");
}
642

643 644 645 646 647
//----------------------------------------------------------------------------
void vtkPVDataDeliveryManager::RedistributeDataForOrderedCompositing(
  bool use_lod)
{
  if (this->RenderView->GetUpdateTimeStamp() > this->RedistributionTimeStamp)
648
    {
649 650 651 652
    vtkTimerLog::MarkStartEvent("Regenerate Kd-Tree");
    // need to re-generate the kd-tree.
    this->RedistributionTimeStamp.Modified();

653 654 655 656 657 658 659
    vtkNew<vtkBSPCutsGenerator> cutsGenerator;
    vtkInternals::ItemsMapType::iterator iter;
    for (iter = this->Internals->ItemsMap.begin();
      iter != this->Internals->ItemsMap.end(); ++iter)
      {
      vtkInternals::vtkItem& item =  iter->second.first;
      if (item.Representation &&
660
        item.Representation->GetVisibility())
661
        {
662 663 664 665 666 667 668 669
        if (item.Redistributable)
          {
          cutsGenerator->AddInputData(item.GetDeliveredDataObject());
          }
        else if (item.ImageDataProducer)
          {
          cutsGenerator->AddInputConnection(item.ImageDataProducer);
          }
670 671 672 673 674 675 676 677 678 679 680 681
        }
      }

    vtkMultiProcessController* controller =
      vtkMultiProcessController::GetGlobalController();
    vtkStreamingDemandDrivenPipeline *sddp = vtkStreamingDemandDrivenPipeline::
      SafeDownCast(cutsGenerator->GetExecutive());
    sddp->SetUpdateExtent
      (0,controller->GetLocalProcessId(),controller->GetNumberOfProcesses(),0);
    sddp->Update(0);

    this->KdTree = cutsGenerator->GetPKdTree();
682
    vtkTimerLog::MarkEndEvent("Regenerate Kd-Tree");
683
    }
684 685

  if (this->KdTree == NULL)
686
    {
687
    return;
688 689
    }

690 691 692 693
  vtkTimerLog::MarkStartEvent("Redistributing Data for Ordered Compositing");
  vtkInternals::ItemsMapType::iterator iter;
  for (iter = this->Internals->ItemsMap.begin();
    iter != this->Internals->ItemsMap.end(); ++iter)
694
    {
695 696 697 698 699 700 701 702
    vtkInternals::vtkItem& item = use_lod? iter->second.second : iter->second.first;

    if (!item.Redistributable ||
      item.Representation == NULL ||
      item.Representation->GetVisibility() == false ||

      // delivered object can be null in case we're updating lod and the
      // representation doeesn't have any LOD data.
703
      item.GetDeliveredDataObject() == NULL)
704
      {
705 706
      continue;
      }
707

708 709 710 711 712 713 714 715 716 717 718 719
    if (item.GetRedistributedDataObject() &&

      // input-data didn't change
      (item.GetDeliveredDataObject()->GetMTime() <
       item.GetRedistributedDataObject()->GetMTime()) &&

      // kd-tree didn't change
      (item.GetRedistributedDataObject()->GetMTime() >
       this->KdTree->GetMTime()))
      {
      // skip redistribution.
      continue;
720 721
      }

722 723
    // release old memory (not necessarily, but try).
    item.SetRedistributedDataObject(NULL);
724

725 726 727 728 729 730 731 732 733
    vtkNew<vtkOrderedCompositeDistributor> redistributor;
    redistributor->SetController(vtkMultiProcessController::GetGlobalController());
    redistributor->SetInputData(item.GetDeliveredDataObject());
    redistributor->SetPKdTree(this->KdTree);
    redistributor->SetPassThrough(0);
    redistributor->Update();
    item.SetRedistributedDataObject(redistributor->GetOutputDataObject(0));
    }
  vtkTimerLog::MarkEndEvent("Redistributing Data for Ordered Compositing");
734 735
}

736
//----------------------------------------------------------------------------
737
vtkPKdTree* vtkPVDataDeliveryManager::GetKdTree()
738 739 740
{
  return this->KdTree;
}
741 742

//----------------------------------------------------------------------------
743 744
void vtkPVDataDeliveryManager::SetNextStreamedPiece(
  vtkPVDataRepresentation* repr, vtkDataObject* data)
745
{
746 747 748 749 750 751 752 753 754 755 756
  vtkInternals::vtkItem* item = this->Internals->GetItem(repr, /*low_res=*/false);
  if (item == NULL)
    {
    vtkErrorMacro("Invalid argument.");
    return;
    }
  
  // For now, I am going to keep things simple. Piece is delivered to the
  // representation separately. That's it.
  item->SetNextStreamedPiece(data);
}
757

758 759 760 761 762 763 764 765 766 767 768 769
//----------------------------------------------------------------------------
vtkDataObject* vtkPVDataDeliveryManager::GetCurrentStreamedPiece(
  vtkPVDataRepresentation* repr)
{
  vtkInternals::vtkItem* item = this->Internals->GetItem(repr, /*low_res=*/false);
  if (item == NULL)
    {
    vtkErrorMacro("Invalid argument.");
    return NULL;
    }
  return item->GetStreamedPiece();
}
770

771 772 773 774 775
//----------------------------------------------------------------------------
void vtkPVDataDeliveryManager::ClearStreamedPieces()
{
  // I am not too sure if I want to do this. Right now I am thinking once a
  // piece is delivered, the delivery manager should no longer bother about it.
776 777 778 779 780
  vtkInternals::ItemsMapType::iterator iter;
  for (iter = this->Internals->ItemsMap.begin();
    iter != this->Internals->ItemsMap.end(); ++iter)
    {
    vtkInternals::vtkItem& item = iter->second.first;
781
    item.SetNextStreamedPiece(NULL);
782 783 784 785
    }
}

//----------------------------------------------------------------------------
786
bool vtkPVDataDeliveryManager::DeliverStreamedPieces()
787
{
788 789
  // This method gets called on all processes to deliver any streamed pieces
  // currently available.
790

791 792 793 794 795 796 797
  // Representations can provide overrides, e.g. though the view says data is
  // merely "pass-through", some representation says we need to clone the data
  // everywhere. That makes it critical that this method is called on all
  // processes at the same time to avoid deadlocks and other complications.
  //
  // This method will be implemented in "view-specific" subclasses since how the
  // data is delivered is very view specific.
798

799 800 801
  //bool using_remote_rendering =
  //  this->RenderView->GetUseDistributedRenderingForStillRender();
  //int mode = this->RenderView->GetDataDistributionMode(using_remote_rendering);
802

803 804 805
  // FIXME: This only support built-in operation for now. We will have to add
  // support for client-server modes. The challenge is how do we deliver data
  // "gracefully".
806

807 808 809 810
  bool something_delivered = false;
  vtkInternals::ItemsMapType::iterator iter;
  for (iter = this->Internals->ItemsMap.begin();
    iter != this->Internals->ItemsMap.end(); ++iter)
811
    {
812 813
    vtkInternals::vtkItem& item = iter->second.first;
    if (item.Representation && item.Streamable && item.GetStreamedPiece())
814
      {
815 816
      // FIXME: do data delivery.
      something_delivered = true;
817
      }
818
    }
819
  return something_delivered;
820 821
}

822
//----------------------------------------------------------------------------
823
void vtkPVDataDeliveryManager::PrintSelf(ostream& os, vtkIndent indent)
824 825 826
{
  this->Superclass::PrintSelf(os, indent);
}