Resource.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../Core/Profiler.h"
  24. #include "../IO/File.h"
  25. #include "../IO/Log.h"
  26. #include "../Resource/Resource.h"
  27. namespace Urho3D
  28. {
  29. Resource::Resource(Context* context) :
  30. Object(context),
  31. memoryUse_(0),
  32. asyncLoadState_(ASYNC_DONE)
  33. {
  34. }
  35. bool Resource::Load(Deserializer& source)
  36. {
  37. // Because BeginLoad() / EndLoad() can be called from worker threads, where profiling would be a no-op,
  38. // create a type name -based profile block here
  39. #ifdef URHO3D_PROFILING
  40. String profileBlockName("Load" + GetTypeName());
  41. Profiler* profiler = GetSubsystem<Profiler>();
  42. if (profiler)
  43. profiler->BeginBlock(profileBlockName.CString());
  44. #endif
  45. // If we are loading synchronously in a non-main thread, behave as if async loading (for example use
  46. // GetTempResource() instead of GetResource() to load resource dependencies)
  47. SetAsyncLoadState(Thread::IsMainThread() ? ASYNC_DONE : ASYNC_LOADING);
  48. bool success = BeginLoad(source);
  49. if (success)
  50. success &= EndLoad();
  51. SetAsyncLoadState(ASYNC_DONE);
  52. #ifdef URHO3D_PROFILING
  53. if (profiler)
  54. profiler->EndBlock();
  55. #endif
  56. return success;
  57. }
  58. bool Resource::BeginLoad(Deserializer& source)
  59. {
  60. // This always needs to be overridden by subclasses
  61. return false;
  62. }
  63. bool Resource::EndLoad()
  64. {
  65. // If no GPU upload step is necessary, no override is necessary
  66. return true;
  67. }
  68. bool Resource::Save(Serializer& dest) const
  69. {
  70. URHO3D_LOGERROR("Save not supported for " + GetTypeName());
  71. return false;
  72. }
  73. bool Resource::LoadFile(const String& fileName)
  74. {
  75. File file(context_);
  76. return file.Open(fileName, FILE_READ) && Load(file);
  77. }
  78. bool Resource::SaveFile(const String& fileName) const
  79. {
  80. File file(context_);
  81. return file.Open(fileName, FILE_WRITE) && Save(file);
  82. }
  83. void Resource::SetName(const String& name)
  84. {
  85. name_ = name;
  86. nameHash_ = name;
  87. }
  88. void Resource::SetMemoryUse(unsigned size)
  89. {
  90. memoryUse_ = size;
  91. }
  92. void Resource::ResetUseTimer()
  93. {
  94. useTimer_.Reset();
  95. }
  96. void Resource::SetAsyncLoadState(AsyncLoadState newState)
  97. {
  98. asyncLoadState_ = newState;
  99. }
  100. unsigned Resource::GetUseTimer()
  101. {
  102. // If more references than the resource cache, return always 0 & reset the timer
  103. if (Refs() > 1)
  104. {
  105. useTimer_.Reset();
  106. return 0;
  107. }
  108. else
  109. return useTimer_.GetMSec(false);
  110. }
  111. }