ezEngine  Milestone 7
ResourceBase.h
1 #pragma once
2 
3 #include <Core/Basics.h>
4 #include <Foundation/Strings/HashedString.h>
5 #include <Foundation/IO/Stream.h>
7 #include <Foundation/Time/Time.h>
8 #include <Foundation/Time/Timestamp.h>
9 #include <Core/ResourceManager/Implementation/Declarations.h>
10 #include <Core/ResourceManager/ResourceHandle.h>
11 
13 
17 class EZ_CORE_DLL ezResourceBase : public ezReflectedClass
18 {
20 
21 protected:
22  enum class DoUpdate
23  {
24  OnMainThread,
25  OnAnyThread
26  };
27 
28  enum class Unload
29  {
30  AllQualityLevels,
31  OneQualityLevel
32  };
33 
35  ezResourceBase(DoUpdate ResourceUpdateThread, ezUInt8 uiQualityLevelsLoadable);
36 
38  virtual ~ezResourceBase() { }
39 
40 public:
41 
42  struct MemoryUsage
43  {
44  MemoryUsage()
45  {
46  m_uiMemoryCPU = 0;
47  m_uiMemoryGPU = 0;
48  }
49 
50  ezUInt32 m_uiMemoryCPU;
51  ezUInt32 m_uiMemoryGPU;
52  };
53 
55  const ezString& GetResourceID() const { return m_UniqueID; }
56 
58  void SetResourceDescription(const char* szDescription);
59 
61  const ezString& GetResourceDescription() const { return m_sResourceDescription; }
62 
64  ezResourceState GetLoadingState() const { return m_LoadingState; }
65 
81  ezUInt8 GetNumQualityLevelsDiscardable() const { return m_uiQualityLevelsDiscardable; }
82 
84  ezUInt8 GetNumQualityLevelsLoadable() const { return m_uiQualityLevelsLoadable; }
85 
102  void SetPriority(ezResourcePriority priority);
103 
105  ezResourcePriority GetPriority() const { return m_Priority; }
106 
119  void SetDueDate(ezTime date = ezTime::Seconds(60.0 * 60.0 * 24.0 * 365.0 * 1000.0));
120 
128  virtual ezTime GetLoadingDeadline(ezTime tNow) const;
129 
131  const ezBitflags<ezResourceFlags>& GetBaseResourceFlags() const { return m_Flags; }
132 
134  const MemoryUsage& GetMemoryUsage() const { return m_MemoryUsage; }
135 
138  ezTime GetLastAcquireTime() const { return m_LastAcquire; }
139 
141  ezInt32 GetReferenceCount() const { return m_iReferenceCount; }
142 
146  const ezTimestamp& GetLoadedFileModificationTime() const { return m_LoadedFileModificationTime; }
147 
148 private:
149 
150  friend class ezResourceManager;
151  friend class ezResourceManagerWorker;
152  friend class ezResourceManagerWorkerGPU;
153 
155  void SetUniqueID(const char* szUniqueID, bool bIsReloadable);
156 
157  void CallUnloadData(Unload WhatToUnload);
158 
160  virtual ezResourceLoadDesc UnloadData(Unload WhatToUnload) = 0;
161 
162  void CallUpdateContent(ezStreamReaderBase* Stream);
163 
165  virtual ezResourceLoadDesc UpdateContent(ezStreamReaderBase* Stream) = 0;
166 
174  virtual ezResourceTypeLoader* GetDefaultResourceTypeLoader() const;
175 
176  volatile ezResourceState m_LoadingState;
177 
178  ezUInt8 m_uiQualityLevelsDiscardable;
179  ezUInt8 m_uiQualityLevelsLoadable;
180 
181 protected:
182 
184  MemoryUsage& ModifyMemoryUsage() { return m_MemoryUsage; }
185 
190  void SetIsReloadable(bool bIsReloadable) { m_Flags.AddOrRemove(ezResourceFlags::IsReloadable, bIsReloadable); }
191 
192 private:
193  template<typename ResourceType>
194  friend class ezResourceHandle;
195 
196  template<typename SELF, typename SELF_DESCRIPTOR>
197  friend class ezResource;
198 
199  friend EZ_CORE_DLL void IncreaseResourceRefCount(ezResourceBase* pResource);
200  friend EZ_CORE_DLL void DecreaseResourceRefCount(ezResourceBase* pResource);
201 
206  virtual void UpdateMemoryUsage(MemoryUsage& out_NewMemoryUsage) = 0;
207 
209 
210  ezResourcePriority m_Priority;
211  ezAtomicInteger32 m_iReferenceCount;
212  ezAtomicInteger32 m_iLockCount;
213  ezString m_UniqueID;
214  ezString m_sResourceDescription;
215 
216  MemoryUsage m_MemoryUsage;
217 
218  ezTime m_LastAcquire;
219  ezTime m_DueDate;
220  ezTimestamp m_LoadedFileModificationTime;
221 };