File: vtkPVServerSideAnimationPlayer.cxx

package info (click to toggle)
paraview 5.4.1%2Bdfsg4-3.1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 218,616 kB
  • sloc: cpp: 2,331,508; ansic: 322,365; python: 111,051; xml: 79,203; tcl: 47,013; yacc: 4,877; java: 4,438; perl: 3,238; sh: 2,920; lex: 1,908; f90: 748; makefile: 273; pascal: 228; objc: 83; fortran: 31
file content (209 lines) | stat: -rw-r--r-- 6,740 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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
/*=========================================================================

Program:   ParaView
Module:    vtkPVServerSideAnimationPlayer.cxx

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.

=========================================================================*/
#include "vtkPVServerSideAnimationPlayer.h"

#include "vtkCommand.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkPVXMLElement.h"
#include "vtkPVXMLParser.h"
#include "vtkProcessModule.h"
#include "vtkSMPropertyHelper.h"
#include "vtkSMProxyDefinitionManager.h"
#include "vtkSMRenderViewProxy.h"
#include "vtkSMSaveAnimationProxy.h"
#include "vtkSMSession.h"
#include "vtkSMSessionProxyManager.h"
#include "vtkSmartPointer.h"
#include "vtkWeakPointer.h"

#include <cassert>
#include <sstream>

class vtkPVServerSideAnimationPlayerObserver : public vtkCommand
{
public:
  static vtkPVServerSideAnimationPlayerObserver* New()
  {
    return new vtkPVServerSideAnimationPlayerObserver;
  }
  vtkTypeMacro(vtkPVServerSideAnimationPlayerObserver, vtkCommand);

  void SetStateXML(vtkPVXMLElement* xml) { this->StateXML = xml; }
  void SetFileName(const std::string& str) { this->FileName = str; }
  void SetSession(vtkSMSession* session) { this->Session = session; }
  void SetTargetSessionID(vtkIdType id) { this->TargetSessionID = id; }

  void Execute(vtkObject* caller, unsigned long eventid, void* calldata) VTK_OVERRIDE
  {
    assert(vtkProcessModule::SafeDownCast(caller) != NULL);
    if (eventid != vtkCommand::ConnectionClosedEvent)
    {
      return;
    }

    // Check if the session being cleaned up is indeed the one we were
    // observing.
    vtkIdType sessionId = *(reinterpret_cast<vtkIdType*>(calldata));
    if (sessionId <= 0 || sessionId != this->TargetSessionID)
    {
      return;
    }

    this->TargetSessionID = -1;
    this->SaveAnimation();

    // We no longer need to handle any events.
    caller->RemoveObserver(this);
  }

protected:
  vtkPVServerSideAnimationPlayerObserver()
    : TargetSessionID(-1)
  {
  }
  ~vtkPVServerSideAnimationPlayerObserver() {}

  void SaveAnimation()
  {
    assert(!this->FileName.empty() && this->StateXML != NULL && this->Session != NULL);

    // Switch process type to Batch mode
    vtkProcessModule* pm = vtkProcessModule::GetProcessModule();
    assert(pm->GetPartitionId() == 0);

    pm->UpdateProcessType(vtkProcessModule::PROCESS_BATCH, false);

    // Make sure our internal session will be used across rendering calls and
    // proxy management
    vtkIdType tmpSessionID = pm->RegisterSession(this->Session);
    this->Session->Activate();

    // Update our local SessionProxyManager state
    vtkSMSessionProxyManager* spxm = this->Session->GetSessionProxyManager();
    spxm->LoadXMLState(this->StateXML);

    // FIXME:
    // Mark all views so that they render offscreen.
    // BUG #10159.

    // Write any animations.
    if (vtkSMSaveAnimationProxy* options =
          vtkSMSaveAnimationProxy::SafeDownCast(spxm->FindProxy("misc", "misc", "SaveAnimation")))
    {
      vtkSMPropertyHelper(options, "DisconnectAndSave").Set(0);
      options->WriteAnimation(this->FileName.c_str());
    }

    // Disconnect our session from process module
    this->Session->DeActivate();
    pm->UnRegisterSession(tmpSessionID);
    spxm->UnRegisterProxies();
  }

private:
  std::string FileName;
  vtkSmartPointer<vtkPVXMLElement> StateXML;
  vtkSmartPointer<vtkSMSession> Session;
  vtkIdType TargetSessionID;
};

//****************************************************************************
vtkStandardNewMacro(vtkPVServerSideAnimationPlayer);
//----------------------------------------------------------------------------
vtkPVServerSideAnimationPlayer::vtkPVServerSideAnimationPlayer()
  : SessionProxyManagerState(NULL)
  , FileName(NULL)
{
}

//----------------------------------------------------------------------------
vtkPVServerSideAnimationPlayer::~vtkPVServerSideAnimationPlayer()
{
  this->SetSessionProxyManagerState(NULL);
  this->SetFileName(NULL);
}

//----------------------------------------------------------------------------
void vtkPVServerSideAnimationPlayer::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
}

//----------------------------------------------------------------------------
void vtkPVServerSideAnimationPlayer::Activate()
{
  if (!this->SessionProxyManagerState)
  {
    vtkErrorMacro("No `SessionProxyManagerState` specified.");
    return;
  }

  if (!this->FileName)
  {
    vtkErrorMacro("No `FileName` specified.");
    return;
  }

  vtkSmartPointer<vtkPVXMLElement> pxmState =
    vtkPVXMLParser::ParseXML(this->SessionProxyManagerState);
  if (!pxmState)
  {
    vtkErrorMacro("Failed to parse `SessionProxyManagerState`.");
    return;
  }

  vtkProcessModule* pm = vtkProcessModule::GetProcessModule();
  assert(pm != NULL);
  if (pm->GetPartitionId() == 0)
  {
    vtkNew<vtkPVServerSideAnimationPlayerObserver> observer;
    observer->SetStateXML(pxmState);
    observer->SetFileName(this->FileName);

    // Root node: monitor the process module till when the active session gets
    // disconnected.
    vtkPVSessionBase* serverSession = vtkPVSessionBase::SafeDownCast(pm->GetActiveSession());
    observer->SetTargetSessionID(pm->GetSessionID(serverSession));
    assert("Server session were found" && serverSession);

    // Create a new session that re-uses the existing session's parallel
    // communication infrastructure.
    vtkSmartPointer<vtkSMSession> session;
    session.TakeReference(vtkSMSession::New(serverSession));
    observer->SetSession(session);

    // Make sure the proxy definition manager will use the proper session core
    // Only for Root node, satellites don't have SessionProxyManager
    if (session->GetSessionProxyManager())
    {
      vtkSmartPointer<vtkSMProxyDefinitionManager> definitionManager;
      definitionManager = session->GetSessionProxyManager()->GetProxyDefinitionManager();

      definitionManager->SetSession(
        NULL); // This will force the session to be properly set later on
      definitionManager->SetSession(session.GetPointer());

      // Attach to unregister event, so that when the "active" session is unregistered, we can
      // do the cleanup.
      pm->AddObserver(vtkCommand::ConnectionClosedEvent, observer.Get());
    }
  }
  else
  {
    // Satellite
    pm->UpdateProcessType(vtkProcessModule::PROCESS_BATCH, false);
  }
}