summaryrefslogtreecommitdiff
path: root/Runtime/Misc/PreloadManager.h
blob: e8aa38fc3129087b2a45cdea5072f65a4a79619b (plain)
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
#pragma once

#include "Configuration/UnityConfigure.h"
#include "Runtime/BaseClasses/NamedObject.h"
#include "Runtime/Threads/Thread.h"
#include "Runtime/Threads/Mutex.h"
#include "ResourceManager.h"
#include "Runtime/Threads/Semaphore.h"
#include "AsyncOperation.h"
#include "Runtime/Serialize/AwakeFromLoadQueue.h"

struct MonoDomain;
class PreloadManagerOperation;
class AssetBundle;


class PreloadData : public NamedObject
{
	public:
	
	DECLARE_OBJECT_SERIALIZE (PreloadData)
	REGISTER_DERIVED_CLASS (PreloadData, NamedObject)

	PreloadData (MemLabelId label, ObjectCreationMode mode) : Super(label, mode) {  } 
	// ~PreloadData (); declared-by-macro

	virtual bool ShouldIgnoreInGarbageDependencyTracking () { return true; }
	
	std::vector<PPtr<Object> > m_Assets;
};

class PreloadManager
{
	#if THREADED_LOADING
	Thread     m_Thread;
	Semaphore  m_IntegrationSemaphore;
	Mutex      m_QueueMutex;
	Mutex      m_LoadingMutex;
	#endif
	
	std::vector<PreloadManagerOperation*> m_PreloadQueue;
	std::vector<PreloadManagerOperation*> m_CallCoroutineCallbackQueue;
	PreloadManagerOperation* m_ProcessingOperation;
	
	PreloadManagerOperation* volatile m_IntegrationOperation;
	#if ENABLE_MONO
	MonoDomain* m_InitDomain;
	#endif
	
	static void* Run (void* managerPtr);
	void Run ();
	void ProcessPreloadOperation ();
	void InvokeCoroutineCallbacks ();
	
	static size_t FindTopPriorityOperation (std::vector<PreloadManagerOperation*>& ops);
	
	public:
	
	PreloadManager();
	~PreloadManager();
	
	bool IsLoading();
	bool IsLoadingOrQueued();
	bool IsLoadingOrQueuedLevel();
	
	void AddToQueue (PreloadManagerOperation* operation);
	
	void LockPreloading ();
	void UnlockPreloading ();
	
	void UpdatePreloading ();
	
	void SetThreadPriority(ThreadPriority p);
	ThreadPriority GetThreadPriority ();

#if UNITY_EDITOR
	void RemoveStopPlaymodeOperations ();
#endif

	void WaitForAllAsyncOperationsToComplete();

	void Stop();	

private:

	void UpdatePreloadingSingleStep (bool stopPreloadManager);
};

PreloadManager& GetPreloadManager();
void ReleasePreloadManager();
void StopPreloadManager();

class PreloadManagerOperation : public AsyncOperation
{
	protected:
	int		m_Priority;
	bool	m_Complete;
	float	m_Progress;

	PreloadManagerOperation () { m_Priority = 0; m_Complete = false; m_Progress = 0.0F; }
	
	public:

	virtual int GetPriority () { return m_Priority; }
	virtual void SetPriority (int priority) { m_Priority = priority; }
	
	virtual float GetProgress ();

	/// Returns true when the operation has completed.
	/// Subclasses must set m_Complete to true from either Perform or IntegrateMainThread.
	virtual bool IsDone ();
	
	/// Performs the actual work on the preload manager thread
	virtual void Perform () = 0;

	/// When complete gives the operation a chance to integrate some work on the main thread,
	/// without any other preload operation running at the same time
	virtual void IntegrateMainThread () {  }

	/// Override this and return true if you want IntegrateMainThread to be called.
	virtual bool HasIntegrateMainThread () { return false; }

	/// Do we have to complete the preload operation by the next frame?
	virtual bool MustCompleteNextFrame () { return false; }

#if UNITY_EDITOR
	virtual bool IsPlaymodeLoadLevel () { return false; }
#endif	
	
#if ENABLE_PROFILER
	virtual std::string GetDebugName () = 0;
#endif
};

///@TODO: We should seperate SceneLoading from assetbundle loading code here. Make it into two AsyncOperation classes.


class PreloadLevelOperation : public PreloadManagerOperation
{
	public:
	
	static void PreloadBundleSync (AssetBundle& bundle, const std::string& name);

	static PreloadLevelOperation* LoadAssetBundle (AssetBundle& bundle, const std::string& name);
	
	enum LoadingMode { kLoadLevel = 0, kLoadAdditiveLevel = 1, kLoadMainData = 2, kLoadAssetBundle = 3, kOpenSceneEditor = 3, kOpenSceneEditorPlaymode = 4, kLoadEditorAdditiveLevel = 5 };
	static PreloadLevelOperation* LoadLevel (const std::string& levelPath, const std::string& levelAssetPath, int levelIndex, LoadingMode mode, bool mustCompleteNextFrame);

	
	virtual void IntegrateMainThread ();
	virtual bool HasIntegrateMainThread ();

	virtual bool GetAllowSceneActivation ();
	virtual void SetAllowSceneActivation (bool allow);
	
	virtual void Perform ();
	
	virtual bool MustCompleteNextFrame () { return m_MustCompleteNextFrame; }
	
#if ENABLE_PROFILER
	virtual std::string GetDebugName () { return m_DebugName; }
#endif

#if UNITY_EDITOR
	virtual bool IsPlaymodeLoadLevel () { return m_LoadMode == kLoadLevel || m_LoadMode == kLoadAdditiveLevel; }
#endif	

private:
	bool                m_AllowSceneActivation;
	dynamic_array<SInt32> m_PreloadAssets;	
	int                 m_LoadLevelIndex;
	std::string			m_LevelPath;
	std::string			m_LevelAssetDataPath;
	LoadingMode         m_LoadMode;
	bool                m_MustCompleteNextFrame;

	AwakeFromLoadQueue  m_AwakeFromLoadQueue;
	
#if ENABLE_PROFILER
	std::string			m_DebugName;
#endif	
	
	void CleanupMemory();
	
	PreloadLevelOperation ();
	virtual ~PreloadLevelOperation ();

	static PreloadLevelOperation* CreateDummy ();
};

class UnloadUnusedAssetsOperation : public PreloadManagerOperation
{	
	UnloadUnusedAssetsOperation () : PreloadManagerOperation () {  }
	
	public:

	static UnloadUnusedAssetsOperation* UnloadUnusedAssets ();
	
	virtual void IntegrateMainThread ();
	virtual bool HasIntegrateMainThread () {return true;}

	virtual void Perform () {}
	
#if ENABLE_PROFILER
	virtual std::string GetDebugName () { return "Garbage Collect Assets"; }
#endif
};

void UnloadUnusedAssetsImmediate (bool managedObjects);