From 451a89d75b78ccf0313f5dadbe6f380136500fda Mon Sep 17 00:00:00 2001
From: Andrey Kleshchev <andreykproductengine@lindenlab.com>
Date: Thu, 15 Jun 2023 22:23:29 +0300
Subject: [PATCH] SL-19686 WIP Split gallery initiation over frames

---
 indra/newview/llinventorygallery.cpp | 52 +++++++++++++++++++---------
 indra/newview/llinventorygallery.h   |  6 ++--
 2 files changed, 38 insertions(+), 20 deletions(-)

diff --git a/indra/newview/llinventorygallery.cpp b/indra/newview/llinventorygallery.cpp
index fe9605daa85..083dfdc65ef 100644
--- a/indra/newview/llinventorygallery.cpp
+++ b/indra/newview/llinventorygallery.cpp
@@ -219,6 +219,7 @@ void LLInventoryGallery::updateRootFolder()
         {
             updateRemovedItem(mHiddenItems[i]->getUUID());
         }
+        mItemBuildQuery.clear();
         
         if (gInventory.containsObserver(mCategoriesObserver))
         {
@@ -262,16 +263,15 @@ void LLInventoryGallery::updateRootFolder()
             iter != cat_array->end();
             iter++)
         {
-            updateAddedItem((*iter)->getUUID());
+            mItemBuildQuery.insert((*iter)->getUUID());
         }
         
         for (LLInventoryModel::item_array_t::const_iterator iter = item_array->begin();
             iter != item_array->end();
             iter++)
         {
-            updateAddedItem((*iter)->getUUID());
+            mItemBuildQuery.insert((*iter)->getUUID());
         }
-        reArrangeRows();
         mIsInitialized = true;
         mRootDirty = false;
 
@@ -287,6 +287,11 @@ void LLInventoryGallery::updateRootFolder()
     {
         initGallery();
     }
+
+    if (!mItemBuildQuery.empty())
+    {
+        gIdleCallbacks.addFunction(onIdle, (void*)this);
+    }
 }
 
 void LLInventoryGallery::initGallery()
@@ -640,12 +645,14 @@ void LLInventoryGallery::setFilterSubString(const std::string& string)
     //reArrangeRows();
 }
 
-void LLInventoryGallery::applyFilter(LLInventoryGalleryItem* item, const std::string& filter_substring)
+bool LLInventoryGallery::applyFilter(LLInventoryGalleryItem* item, const std::string& filter_substring)
 {
     if(item)
     {
         item->setHidden(!checkAgainstFilters(item, filter_substring));
+        return false;
     }
+    return true;
 }
 
 bool LLInventoryGallery::checkAgainstFilters(LLInventoryGalleryItem* item, const std::string& filter_substring)
@@ -724,6 +731,21 @@ void LLInventoryGallery::onIdle(void* userdata)
         return;
     }
 
+    const F64 MAX_TIME_VISIBLE = 0.020f;
+    const F64 MAX_TIME_HIDDEN = 0.001f; // take it slow
+    const F64 max_time = self->getVisible() ? MAX_TIME_VISIBLE : MAX_TIME_HIDDEN;
+    F64 curent_time = LLTimer::getTotalSeconds();
+    const F64 end_time = curent_time + max_time;
+
+    while (!self->mItemBuildQuery.empty() && end_time > curent_time)
+    {
+        uuid_set_t::iterator iter = self->mItemBuildQuery.begin();
+        LLUUID item_id = *iter;
+        self->mNeedsArrange |= self->updateAddedItem(item_id);
+        self->mItemBuildQuery.erase(iter);
+        curent_time = LLTimer::getTotalSeconds();
+    }
+
     if (self->mNeedsArrange)
     {
         self->mNeedsArrange = false;
@@ -737,7 +759,7 @@ void LLInventoryGallery::onIdle(void* userdata)
         self->changeItemSelection(item_to_select, true);
     }
 
-    if (self->mItemToSelect.isNull())
+    if (self->mItemToSelect.isNull() && self->mItemBuildQuery.empty())
     {
         gIdleCallbacks.deleteFunction(onIdle, (void*)self);
     }
@@ -768,13 +790,13 @@ void LLInventoryGallery::getCurrentCategories(uuid_vec_t& vcur)
     }
 }
 
-void LLInventoryGallery::updateAddedItem(LLUUID item_id)
+bool LLInventoryGallery::updateAddedItem(LLUUID item_id)
 {
     LLInventoryObject* obj = gInventory.getObject(item_id);
     if (!obj)
     {
         LL_WARNS("InventoryGallery") << "Failed to find item: " << item_id << LL_ENDL;
-        return;
+        return false;
     }
 
     std::string name = obj->getName();
@@ -805,18 +827,20 @@ void LLInventoryGallery::updateAddedItem(LLUUID item_id)
         }
     }
 
+    bool res = false;
     LLInventoryGalleryItem* item = buildGalleryItem(name, item_id, obj->getType(), thumbnail_id, inventory_type, misc_flags, obj->getIsLinkType(), is_worn);
     mItemMap.insert(LLInventoryGallery::gallery_item_map_t::value_type(item_id, item));
     item->setRightMouseDownCallback(boost::bind(&LLInventoryGallery::showContextMenu, this, _1, _2, _3, item_id));
     item->setFocusReceivedCallback(boost::bind(&LLInventoryGallery::changeItemSelection, this, item_id, false));
     if (mGalleryCreated)
     {
-        applyFilter(item, mFilterSubString);
+        res = applyFilter(item, mFilterSubString);
         addToGallery(item);
     }
 
     mThumbnailsObserver->addItem(item_id,
         boost::bind(&LLInventoryGallery::updateItemThumbnail, this, item_id));
+    return res;
 }
 
 void LLInventoryGallery::updateRemovedItem(LLUUID item_id)
@@ -838,6 +862,8 @@ void LLInventoryGallery::updateRemovedItem(LLUUID item_id)
             item->die();
         }
     }
+
+    mItemBuildQuery.erase(item_id);
 }
 
 void LLInventoryGallery::updateChangedItemName(LLUUID item_id, std::string name)
@@ -893,15 +919,6 @@ void LLInventoryGallery::updateItemThumbnail(LLUUID item_id)
     }
 }
 
-void LLInventoryGallery::onThumbnailAdded(LLUUID item_id)
-{
-    if((mItemMap.count(item_id) == 0) && mFilter->checkAgainstFilterThumbnails(item_id))
-    {
-        updateAddedItem(item_id);
-        reArrangeRows();
-    }
-}
-
 BOOL LLInventoryGallery::handleRightMouseDown(S32 x, S32 y, MASK mask)
 {
     LLUUID old_selection = mSelectedItemID;
@@ -1666,6 +1683,7 @@ void LLInventoryGallery::computeDifference(
 
     uuid_vec_t vcur;
     getCurrentCategories(vcur);
+    std::copy(mItemBuildQuery.begin(), mItemBuildQuery.end(), std::back_inserter(vcur));
 
     LLCommonUtils::computeDifference(vnew, vcur, vadded, vremoved);
 }
diff --git a/indra/newview/llinventorygallery.h b/indra/newview/llinventorygallery.h
index 8519ee07315..66cffccebc1 100644
--- a/indra/newview/llinventorygallery.h
+++ b/indra/newview/llinventorygallery.h
@@ -92,11 +92,10 @@ class LLInventoryGallery : public LLPanel, public LLEditMenuHandler
     bool checkAgainstFilterType(const LLUUID& object_id);
 
     void getCurrentCategories(uuid_vec_t& vcur);
-    void updateAddedItem(LLUUID item_id);
+    bool updateAddedItem(LLUUID item_id); // returns true if added item is visible
     void updateRemovedItem(LLUUID item_id);
     void updateChangedItemName(LLUUID item_id, std::string name);
     void updateItemThumbnail(LLUUID item_id);
-    void onThumbnailAdded(LLUUID item_id);
     void updateWornItem(LLUUID item_id, bool is_worn);
 
     void updateMessageVisibility();
@@ -167,7 +166,7 @@ class LLInventoryGallery : public LLPanel, public LLEditMenuHandler
 
     void showContextMenu(LLUICtrl* ctrl, S32 x, S32 y, const LLUUID& item_id);
 
-    void applyFilter(LLInventoryGalleryItem* item, const std::string& filter_substring);
+    bool applyFilter(LLInventoryGalleryItem* item, const std::string& filter_substring);
     bool checkAgainstFilters(LLInventoryGalleryItem* item, const std::string& filter_substring);
     static void onIdle(void* userdata);
     void dirtyRootFolder();
@@ -247,6 +246,7 @@ class LLInventoryGallery : public LLPanel, public LLEditMenuHandler
     gallery_item_map_t mItemMap;
     uuid_vec_t mCOFLinkedItems;
     uuid_vec_t mActiveGestures;
+    uuid_set_t mItemBuildQuery;
     std::map<LLInventoryGalleryItem*, S32> mItemIndexMap;
     std::map<S32, LLInventoryGalleryItem*> mIndexToItemMap;
 
-- 
GitLab