BuilderDataItem.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <native/ui/BuilderDataItem.h>
  9. namespace AssetProcessor
  10. {
  11. static constexpr AZStd::array jobTypeDisplayNames{ "Analysis Jobs", "Processing Jobs" };
  12. static constexpr char invalidJobTypeDisplayName[] = "Invalid Job Type";
  13. BuilderDataItem::BuilderDataItem(
  14. ItemType itemType, AZStd::string name,
  15. AZ::s64 jobCount,
  16. AZ::s64 totalDuration,
  17. AZStd::weak_ptr<BuilderDataItem> parent)
  18. : m_itemType(itemType)
  19. , m_name(AZStd::move(name))
  20. , m_jobCount(jobCount)
  21. , m_totalDuration(totalDuration)
  22. , m_parent(parent)
  23. {
  24. }
  25. int BuilderDataItem::ChildCount() const
  26. {
  27. return aznumeric_cast<int>(m_children.size());
  28. }
  29. const AZStd::string& BuilderDataItem::GetName() const
  30. {
  31. return m_name;
  32. }
  33. AZ::s64 BuilderDataItem::GetJobCount() const
  34. {
  35. return m_jobCount;
  36. }
  37. AZ::s64 BuilderDataItem::GetTotalDuration() const
  38. {
  39. return m_totalDuration;
  40. }
  41. BuilderDataItem::ItemType BuilderDataItem::GetItemType() const
  42. {
  43. return m_itemType;
  44. }
  45. AZStd::shared_ptr<BuilderDataItem> BuilderDataItem::GetChild(int row) const
  46. {
  47. if (row >= m_children.size())
  48. {
  49. return nullptr;
  50. }
  51. return m_children[row];
  52. }
  53. AZStd::weak_ptr<BuilderDataItem> BuilderDataItem::GetParent() const
  54. {
  55. return m_parent;
  56. }
  57. AZStd::shared_ptr<BuilderDataItem> BuilderDataItem::InsertChild(AZStd::shared_ptr<BuilderDataItem>&& itemToBeInserted)
  58. {
  59. //! It's the caller's responsibility to ensure the builder has a unique name
  60. if (m_itemType != ItemType::InvisibleRoot ||
  61. (itemToBeInserted->GetItemType() != ItemType::InvisibleRoot && itemToBeInserted->GetItemType() != ItemType::Builder) ||
  62. itemToBeInserted->GetParent().lock().get() != this || m_childNameToIndex.contains(itemToBeInserted->GetName()))
  63. {
  64. return nullptr;
  65. }
  66. m_childNameToIndex[itemToBeInserted->GetName()] = aznumeric_cast<int>(m_children.size());
  67. m_children.push_back(AZStd::move(itemToBeInserted));
  68. return m_children.back();
  69. }
  70. AZStd::shared_ptr<BuilderDataItem> BuilderDataItem::UpdateOrInsertEntry(
  71. TaskType entryTaskType, const AZStd::string& entryName, AZ::s64 entryJobCount, AZ::s64 entryTotalDuration)
  72. {
  73. //! only allowed to insert from builder, with a valid TaskType
  74. if (m_itemType != ItemType::Builder || entryTaskType >= TaskType::Max)
  75. {
  76. return nullptr;
  77. }
  78. // jobType is either CreateJobs or ProcessJob
  79. const auto& jobType = m_children[aznumeric_cast<int>(entryTaskType)];
  80. AZStd::shared_ptr<BuilderDataItem> entry = nullptr;
  81. if (jobType->m_childNameToIndex.contains(entryName))
  82. {
  83. entry = jobType->m_children[jobType->m_childNameToIndex[entryName]];
  84. AZ::s64 jobCountDiff = entryJobCount - entry->m_jobCount;
  85. AZ::s64 totalDurationDiff = entryTotalDuration - entry->m_totalDuration;
  86. entry->m_jobCount = entryJobCount;
  87. entry->m_totalDuration = entryTotalDuration;
  88. jobType->UpdateMetrics(jobCountDiff, totalDurationDiff);
  89. }
  90. else
  91. {
  92. entry = jobType->m_children.emplace_back(
  93. new BuilderDataItem(ItemType::Entry, entryName, entryJobCount, entryTotalDuration, jobType));
  94. jobType->m_childNameToIndex[entryName] = aznumeric_cast<int>(jobType->m_children.size() - 1);
  95. jobType->UpdateMetrics(entryJobCount, entryTotalDuration);
  96. }
  97. return entry;
  98. }
  99. bool BuilderDataItem::InsertTaskTypesAsChildren(AZStd::weak_ptr<BuilderDataItem> builderWeakPointer)
  100. {
  101. if (m_itemType != ItemType::Builder)
  102. {
  103. return false;
  104. }
  105. for (int jobTypeIndex = 0; jobTypeIndex < aznumeric_cast<int>(TaskType::Max); ++jobTypeIndex)
  106. {
  107. const AZStd::string& jobTypeDisplayName =
  108. jobTypeIndex < jobTypeDisplayNames.size() ? jobTypeDisplayNames[jobTypeIndex] : invalidJobTypeDisplayName;
  109. if (jobTypeIndex >= jobTypeDisplayNames.size())
  110. {
  111. AZ_Warning(
  112. "Asset Processor",
  113. false,
  114. "Invalid job type name. Job type indexed %d in scoped enum TaskType does not have a matching display name in "
  115. "jobTypeDisplayNames. Update jobTypeDisplayNames vector in BuilderDataItem.cpp.",
  116. jobTypeIndex);
  117. }
  118. m_children.emplace_back(new BuilderDataItem(ItemType::TaskType, jobTypeDisplayName, 0, 0, builderWeakPointer));
  119. }
  120. return true;
  121. }
  122. void BuilderDataItem::UpdateMetrics(AZ::s64 jobCountDiff, AZ::s64 totalDurationDiff)
  123. {
  124. if (m_itemType == ItemType::InvisibleRoot)
  125. {
  126. return;
  127. }
  128. m_jobCount += jobCountDiff;
  129. m_totalDuration += totalDurationDiff;
  130. if (auto sharedParent = m_parent.lock())
  131. {
  132. sharedParent->UpdateMetrics(jobCountDiff, totalDurationDiff);
  133. }
  134. }
  135. int BuilderDataItem::GetRow() const
  136. {
  137. if (auto sharedParent = m_parent.lock())
  138. {
  139. int index = 0;
  140. for (const auto& item : sharedParent->m_children)
  141. {
  142. if (item.get() == this)
  143. {
  144. return index;
  145. }
  146. ++index;
  147. }
  148. }
  149. return -1;
  150. }
  151. }