From adeee613c60e8d4d2d57ede9685b8388a681c409 Mon Sep 17 00:00:00 2001 From: andreykproductengine Date: Thu, 25 Jul 2019 18:20:17 +0300 Subject: DRTVWR-493 LLRender2D to LLParamSingleton --- indra/llrender/llrender2dutils.cpp | 48 ++++++++++++++------------------------ indra/llrender/llrender2dutils.h | 28 +++++++++++----------- indra/llrender/lluiimage.cpp | 12 +++++----- 3 files changed, 37 insertions(+), 51 deletions(-) (limited to 'indra/llrender') diff --git a/indra/llrender/llrender2dutils.cpp b/indra/llrender/llrender2dutils.cpp index 4e2ebfd51e..1a7c4144ed 100644 --- a/indra/llrender/llrender2dutils.cpp +++ b/indra/llrender/llrender2dutils.cpp @@ -46,8 +46,6 @@ // Globals // const LLColor4 UI_VERTEX_COLOR(1.f, 1.f, 1.f, 1.f); -/*static*/ LLVector2 LLRender2D::sGLScaleFactor(1.f, 1.f); -/*static*/ LLImageProviderInterface* LLRender2D::sImageProvider = NULL; // // Functions @@ -108,10 +106,10 @@ void gl_rect_2d_offset_local( S32 left, S32 top, S32 right, S32 bottom, S32 pixe top += LLFontGL::sCurOrigin.mY; gGL.loadUIIdentity(); - gl_rect_2d(llfloor((F32)left * LLRender2D::sGLScaleFactor.mV[VX]) - pixel_offset, - llfloor((F32)top * LLRender2D::sGLScaleFactor.mV[VY]) + pixel_offset, - llfloor((F32)right * LLRender2D::sGLScaleFactor.mV[VX]) + pixel_offset, - llfloor((F32)bottom * LLRender2D::sGLScaleFactor.mV[VY]) - pixel_offset, + gl_rect_2d(llfloor((F32)left * LLRender2D::getInstance()->mGLScaleFactor.mV[VX]) - pixel_offset, + llfloor((F32)top * LLRender2D::getInstance()->mGLScaleFactor.mV[VY]) + pixel_offset, + llfloor((F32)right * LLRender2D::getInstance()->mGLScaleFactor.mV[VX]) + pixel_offset, + llfloor((F32)bottom * LLRender2D::getInstance()->mGLScaleFactor.mV[VY]) - pixel_offset, filled); gGL.popUIMatrix(); } @@ -800,7 +798,7 @@ void gl_stippled_line_3d( const LLVector3& start, const LLVector3& end, const LL } gGL.end(); - LLRender2D::setLineWidth(1.f); + LLRender2D::getInstance()->setLineWidth(1.f); } void gl_arc_2d(F32 center_x, F32 center_y, F32 radius, S32 steps, BOOL filled, F32 start_angle, F32 end_angle) @@ -967,7 +965,7 @@ void gl_rect_2d_checkerboard(const LLRect& rect, GLfloat alpha) } else { //polygon stipple is deprecated, use "Checker" texture - LLPointer img = LLRender2D::getUIImage("Checker"); + LLPointer img = LLRender2D::getInstance()->getUIImage("Checker"); gGL.getTexUnit(0)->bind(img->getImage()); gGL.getTexUnit(0)->setTextureAddressMode(LLTexUnit::TAM_WRAP); gGL.getTexUnit(0)->setTextureFilteringOption(LLTexUnit::TFO_POINT); @@ -1567,25 +1565,22 @@ void gl_segmented_rect_3d_tex(const LLRectf& clip_rect, const LLRectf& center_uv } -// static -void LLRender2D::initClass(LLImageProviderInterface* image_provider, +LLRender2D::LLRender2D(LLImageProviderInterface* image_provider, const LLVector2* scale_factor) { - sGLScaleFactor = (scale_factor == NULL) ? LLVector2(1.f, 1.f) : *scale_factor; - sImageProvider = image_provider; + mGLScaleFactor = (scale_factor == NULL) ? LLVector2(1.f, 1.f) : *scale_factor; + mImageProvider = image_provider; } -// static -void LLRender2D::cleanupClass() +LLRender2D::~LLRender2D() { - if(sImageProvider) + if(mImageProvider) { - sImageProvider->cleanUp(); + mImageProvider->cleanUp(); } } -//static void LLRender2D::translate(F32 x, F32 y, F32 z) { gGL.translateUI(x,y,z); @@ -1594,14 +1589,12 @@ void LLRender2D::translate(F32 x, F32 y, F32 z) LLFontGL::sCurDepth += z; } -//static void LLRender2D::pushMatrix() { gGL.pushUIMatrix(); LLFontGL::sOriginStack.push_back(std::make_pair(LLFontGL::sCurOrigin, LLFontGL::sCurDepth)); } -//static void LLRender2D::popMatrix() { gGL.popUIMatrix(); @@ -1610,7 +1603,6 @@ void LLRender2D::popMatrix() LLFontGL::sOriginStack.pop_back(); } -//static void LLRender2D::loadIdentity() { gGL.loadUIIdentity(); @@ -1619,25 +1611,22 @@ void LLRender2D::loadIdentity() LLFontGL::sCurDepth = 0.f; } -//static void LLRender2D::setScaleFactor(const LLVector2 &scale_factor) { - sGLScaleFactor = scale_factor; + mGLScaleFactor = scale_factor; } -//static void LLRender2D::setLineWidth(F32 width) { gGL.flush(); - glLineWidth(width * lerp(sGLScaleFactor.mV[VX], sGLScaleFactor.mV[VY], 0.5f)); + glLineWidth(width * lerp(mGLScaleFactor.mV[VX], mGLScaleFactor.mV[VY], 0.5f)); } -//static LLPointer LLRender2D::getUIImageByID(const LLUUID& image_id, S32 priority) { - if (sImageProvider) + if (mImageProvider) { - return sImageProvider->getUIImageByID(image_id, priority); + return mImageProvider->getUIImageByID(image_id, priority); } else { @@ -1645,11 +1634,10 @@ LLPointer LLRender2D::getUIImageByID(const LLUUID& image_id, S32 prio } } -//static LLPointer LLRender2D::getUIImage(const std::string& name, S32 priority) { - if (!name.empty() && sImageProvider) - return sImageProvider->getUIImage(name, priority); + if (!name.empty() && mImageProvider) + return mImageProvider->getUIImage(name, priority); else return NULL; } diff --git a/indra/llrender/llrender2dutils.h b/indra/llrender/llrender2dutils.h index cce3b4ed51..4e4696be41 100644 --- a/indra/llrender/llrender2dutils.h +++ b/indra/llrender/llrender2dutils.h @@ -121,28 +121,26 @@ inline void gl_rect_2d_offset_local( const LLRect& rect, S32 pixel_offset, BOOL class LLImageProviderInterface; -class LLRender2D +class LLRender2D : public LLParamSingleton { + LLPARAMSINGLETON(LLRender2D, LLImageProviderInterface* image_provider, const LLVector2* scale_factor); LOG_CLASS(LLRender2D); + ~LLRender2D(); public: - static void initClass(LLImageProviderInterface* image_provider, - const LLVector2* scale_factor); - static void cleanupClass(); + void pushMatrix(); + void popMatrix(); + void loadIdentity(); + void translate(F32 x, F32 y, F32 z = 0.0f); - static void pushMatrix(); - static void popMatrix(); - static void loadIdentity(); - static void translate(F32 x, F32 y, F32 z = 0.0f); + void setLineWidth(F32 width); + void setScaleFactor(const LLVector2& scale_factor); - static void setLineWidth(F32 width); - static void setScaleFactor(const LLVector2& scale_factor); + LLPointer getUIImageByID(const LLUUID& image_id, S32 priority = 0); + LLPointer getUIImage(const std::string& name, S32 priority = 0); - static LLPointer getUIImageByID(const LLUUID& image_id, S32 priority = 0); - static LLPointer getUIImage(const std::string& name, S32 priority = 0); - - static LLVector2 sGLScaleFactor; + LLVector2 mGLScaleFactor; private: - static LLImageProviderInterface* sImageProvider; + LLImageProviderInterface* mImageProvider; }; class LLImageProviderInterface diff --git a/indra/llrender/lluiimage.cpp b/indra/llrender/lluiimage.cpp index 5d8f92b2e6..c8337feabb 100644 --- a/indra/llrender/lluiimage.cpp +++ b/indra/llrender/lluiimage.cpp @@ -120,12 +120,12 @@ void LLUIImage::draw3D(const LLVector3& origin_agent, const LLVector3& x_axis, c } } - LLRender2D::pushMatrix(); + LLRender2D::getInstance()->pushMatrix(); { LLVector3 rect_origin = origin_agent + (rect.mLeft * x_axis) + (rect.mBottom * y_axis); - LLRender2D::translate(rect_origin.mV[VX], - rect_origin.mV[VY], - rect_origin.mV[VZ]); + LLRender2D::getInstance()->translate(rect_origin.mV[VX], + rect_origin.mV[VY], + rect_origin.mV[VZ]); gGL.getTexUnit(0)->bind(getImage()); gGL.color4fv(color.mV); @@ -142,7 +142,7 @@ void LLUIImage::draw3D(const LLVector3& origin_agent, const LLVector3& x_axis, c rect.getWidth() * x_axis, rect.getHeight() * y_axis); - } LLRender2D::popMatrix(); + } LLRender2D::getInstance()->popMatrix(); } @@ -199,7 +199,7 @@ namespace LLInitParam return; } - LLUIImage* imagep = LLRender2D::getUIImage(name()); + LLUIImage* imagep = LLRender2D::getInstance()->getUIImage(name()); if (imagep) { updateValue(imagep); -- cgit v1.3 From 34d8200d0f0dde1b8205c802edbb08cc1ff503b2 Mon Sep 17 00:00:00 2001 From: andreykproductengine Date: Sat, 10 Aug 2019 09:01:54 +0300 Subject: DRTVWR-493 LLRender2D init cleanup --- indra/llrender/llrender2dutils.cpp | 5 ++--- indra/llrender/llrender2dutils.h | 2 +- indra/llui/llui.cpp | 6 ++---- indra/llui/llui.h | 4 +--- 4 files changed, 6 insertions(+), 11 deletions(-) (limited to 'indra/llrender') diff --git a/indra/llrender/llrender2dutils.cpp b/indra/llrender/llrender2dutils.cpp index 1a7c4144ed..0467321e3b 100644 --- a/indra/llrender/llrender2dutils.cpp +++ b/indra/llrender/llrender2dutils.cpp @@ -1565,10 +1565,9 @@ void gl_segmented_rect_3d_tex(const LLRectf& clip_rect, const LLRectf& center_uv } -LLRender2D::LLRender2D(LLImageProviderInterface* image_provider, - const LLVector2* scale_factor) +LLRender2D::LLRender2D(LLImageProviderInterface* image_provider) { - mGLScaleFactor = (scale_factor == NULL) ? LLVector2(1.f, 1.f) : *scale_factor; + mGLScaleFactor = LLVector2(1.f, 1.f); mImageProvider = image_provider; } diff --git a/indra/llrender/llrender2dutils.h b/indra/llrender/llrender2dutils.h index 4e4696be41..f2640ff998 100644 --- a/indra/llrender/llrender2dutils.h +++ b/indra/llrender/llrender2dutils.h @@ -123,7 +123,7 @@ class LLImageProviderInterface; class LLRender2D : public LLParamSingleton { - LLPARAMSINGLETON(LLRender2D, LLImageProviderInterface* image_provider, const LLVector2* scale_factor); + LLPARAMSINGLETON(LLRender2D, LLImageProviderInterface* image_provider); LOG_CLASS(LLRender2D); ~LLRender2D(); public: diff --git a/indra/llui/llui.cpp b/indra/llui/llui.cpp index 2e2833c197..b5c4ca1d9f 100644 --- a/indra/llui/llui.cpp +++ b/indra/llui/llui.cpp @@ -160,11 +160,9 @@ void make_ui_sound_deferred(const char* namep) void LLUI::initClass(const settings_map_t& settings, LLImageProviderInterface* image_provider, LLUIAudioCallback audio_callback, - LLUIAudioCallback deferred_audio_callback, - const LLVector2* scale_factor, - const std::string& language) + LLUIAudioCallback deferred_audio_callback) { - LLRender2D::initParamSingleton(image_provider,scale_factor); + LLRender2D::initParamSingleton(image_provider); sSettingGroups = settings; if ((get_ptr_in_map(sSettingGroups, std::string("config")) == NULL) || diff --git a/indra/llui/llui.h b/indra/llui/llui.h index c6ee11a96e..ad32b093aa 100644 --- a/indra/llui/llui.h +++ b/indra/llui/llui.h @@ -240,9 +240,7 @@ public: static void initClass(const settings_map_t& settings, LLImageProviderInterface* image_provider, LLUIAudioCallback audio_callback = NULL, - LLUIAudioCallback deferred_audio_callback = NULL, - const LLVector2 *scale_factor = NULL, - const std::string& language = LLStringUtil::null); + LLUIAudioCallback deferred_audio_callback = NULL); static void setPopupFuncs(const add_popup_t& add_popup, const remove_popup_t&, const clear_popups_t& ); -- cgit v1.3 From 1be08814e252654db98ce8c39dc8b4cf89c2e1dc Mon Sep 17 00:00:00 2001 From: andreykproductengine Date: Sun, 11 Aug 2019 12:50:24 +0300 Subject: DRTVWR-493 tiny optimization --- indra/llrender/llrender2dutils.cpp | 9 +++++---- indra/newview/llviewerdisplay.cpp | 4 ++-- 2 files changed, 7 insertions(+), 6 deletions(-) (limited to 'indra/llrender') diff --git a/indra/llrender/llrender2dutils.cpp b/indra/llrender/llrender2dutils.cpp index 0467321e3b..508bccec11 100644 --- a/indra/llrender/llrender2dutils.cpp +++ b/indra/llrender/llrender2dutils.cpp @@ -106,10 +106,11 @@ void gl_rect_2d_offset_local( S32 left, S32 top, S32 right, S32 bottom, S32 pixe top += LLFontGL::sCurOrigin.mY; gGL.loadUIIdentity(); - gl_rect_2d(llfloor((F32)left * LLRender2D::getInstance()->mGLScaleFactor.mV[VX]) - pixel_offset, - llfloor((F32)top * LLRender2D::getInstance()->mGLScaleFactor.mV[VY]) + pixel_offset, - llfloor((F32)right * LLRender2D::getInstance()->mGLScaleFactor.mV[VX]) + pixel_offset, - llfloor((F32)bottom * LLRender2D::getInstance()->mGLScaleFactor.mV[VY]) - pixel_offset, + LLRender2D *r2d_inst = LLRender2D::getInstance(); + gl_rect_2d(llfloor((F32)left * r2d_inst->mGLScaleFactor.mV[VX]) - pixel_offset, + llfloor((F32)top * r2d_inst->mGLScaleFactor.mV[VY]) + pixel_offset, + llfloor((F32)right * r2d_inst->mGLScaleFactor.mV[VX]) + pixel_offset, + llfloor((F32)bottom * r2d_inst->mGLScaleFactor.mV[VY]) - pixel_offset, filled); gGL.popUIMatrix(); } diff --git a/indra/newview/llviewerdisplay.cpp b/indra/newview/llviewerdisplay.cpp index 48eed20861..e1409d31ce 100644 --- a/indra/newview/llviewerdisplay.cpp +++ b/indra/newview/llviewerdisplay.cpp @@ -1508,9 +1508,9 @@ void render_ui_2d() if (gSavedSettings.getBOOL("RenderUIBuffer")) { LLUI* ui_inst = LLUI::getInstance(); - if (LLUI::getInstance()->mDirty) + if (ui_inst->mDirty) { - LLUI::getInstance()->mDirty = FALSE; + ui_inst->mDirty = FALSE; LLRect t_rect; gPipeline.mUIScreen.bindTarget(); -- cgit v1.3 From 98be6e141c1232bad28cc115bc7092f175b18809 Mon Sep 17 00:00:00 2001 From: Nat Goodspeed Date: Mon, 12 Aug 2019 09:44:56 -0400 Subject: DRTVWR-493: Streamline LLParamSingleton, LLLockedSingleton. Simplify LLSingleton::SingletonLifetimeManager to SingletonInitializer: that struct has not been responsible for deletion ever since LLSingletonBase acquired dependency-ordered deleteAll(). Move SingletonData::mInitState changes from SingletonLifetimeManager to constructSingleton() method. Similarly, constructSingleton() now sets SingletonData::mInstance instead of making its caller store the pointer. Add variadic arguments to LLSingleton::constructSingleton() so we can reuse it for LLParamSingleton. Add finishInitializing() method to encapsulate logic reused for getInstance()'s INITIALIZING and DELETED cases. Make LLParamSingleton a subclass of LLSingleton, just as LLLockedSingleton is a subclass of LLParamSingleton. Make LLParamSingleton a friend of LLSingleton, so it can access private members of LLSingleton without also granting access to any DERIVED_CLASS subclass. This eliminates the need for protected getInitState(). LLParamSingleton::initParamSingleton() reuses LLSingleton::constructSingleton() and finishInitializing(). Its getInstance() method completely replaces LLSingleton::getInstance(): in most EInitStates, LLParamSingleton::getInstance() is an error. Use a std::mutex to serialize calls to LLParamSingleton::initParamSingleton() and getInstance(). While LLSingleton::getInstance() relies on the "initialized exactly once" guarantee for block-scope static declarations, LLParamSingleton cannot rely on the same mechanism. LLLockedSingleton is now a very succinct subclass of LLParamSingleton -- they have very similar functionality. Giving the LLSINGLETON() macro variadic arguments eliminates the need for a separate LLPARAMSINGLETON() macro, while continuing to support existing usage. --- indra/llcommon/llsingleton.h | 401 ++++++++++++++++---------------------- indra/llimage/llimage.h | 2 +- indra/llrender/llrender2dutils.h | 2 +- indra/llui/llui.h | 2 +- indra/newview/llconversationlog.h | 2 +- 5 files changed, 176 insertions(+), 233 deletions(-) (limited to 'indra/llrender') diff --git a/indra/llcommon/llsingleton.h b/indra/llcommon/llsingleton.h index b127f4f529..38d5af5309 100644 --- a/indra/llcommon/llsingleton.h +++ b/indra/llcommon/llsingleton.h @@ -31,6 +31,18 @@ #include #include +#if LL_WINDOWS +#pragma warning (push) +#pragma warning (disable:4265) +#endif +// warning C4265: 'std::_Pad' : class has virtual functions, but destructor is not virtual + +#include + +#if LL_WINDOWS +#pragma warning (pop) +#endif + class LLSingletonBase: private boost::noncopyable { public: @@ -205,6 +217,10 @@ LLSingletonBase::LLSingletonBase(tag): LLSingleton_manage_master().push_initializing(this); } +// forward declare for friend directive within LLSingleton +template +class LLParamSingleton; + /** * LLSingleton implements the getInstance() method part of the Singleton * pattern. It can't make the derived class constructors protected, though, so @@ -270,9 +286,41 @@ template class LLSingleton : public LLSingletonBase { private: - static DERIVED_TYPE* constructSingleton() + // Allow LLParamSingleton subclass -- but NOT DERIVED_TYPE itself -- to + // access our private members. + friend class LLParamSingleton; + + // LLSingleton only supports a nullary constructor. However, the specific + // purpose for its subclass LLParamSingleton is to support Singletons + // requiring constructor arguments. constructSingleton() supports both use + // cases. + template + static void constructSingleton(Args&&... args) { - return new DERIVED_TYPE(); + sData.mInitState = CONSTRUCTING; + sData.mInstance = new DERIVED_TYPE(std::forward(args)...); + sData.mInitState = INITIALIZING; + } + + static void finishInitializing() + { + // go ahead and flag ourselves as initialized so we can be + // reentrant during initialization + sData.mInitState = INITIALIZED; + // initialize singleton after constructing it so that it can + // reference other singletons which in turn depend on it, thus + // breaking cyclic dependencies + sData.mInstance->initSingleton(); + // pop this off stack of initializing singletons + LLSingleton_manage_master().pop_initializing(sData.mInstance); + + // The remaining top of that stack, if any, is an LLSingleton that + // directly depends on DERIVED_TYPE. If getInstance() was called by + // another LLSingleton, rather than from vanilla application code, + // record the dependency. + sData.mInstance->capture_dependency( + LLSingleton_manage_master().get_initializing(sData.mInstance), + sData.mInitState); } // We know of no way to instruct the compiler that every subclass @@ -285,34 +333,17 @@ private: // subclass body. virtual void you_must_use_LLSINGLETON_macro() = 0; - // stores pointer to singleton instance - struct SingletonLifetimeManager + // The purpose of this struct is to engage the C++11 guarantee that static + // variables declared in function scope are initialized exactly once, even + // if multiple threads concurrently reach the same declaration. + // https://en.cppreference.com/w/cpp/language/storage_duration#Static_local_variables + // Since getInstance() declares a static instance of SingletonInitializer, + // only the first call to getInstance() calls constructSingleton(). + struct SingletonInitializer { - SingletonLifetimeManager() - { - construct(); - } - - static void construct() + SingletonInitializer() { - sData.mInitState = CONSTRUCTING; - sData.mInstance = constructSingleton(); - sData.mInitState = INITIALIZING; - } - - ~SingletonLifetimeManager() - { - // The dependencies between LLSingletons, and the arbitrary order - // of static-object destruction, mean that we DO NOT WANT this - // destructor to delete this LLSingleton. This destructor will run - // without regard to any other LLSingleton whose cleanup might - // depend on its existence. If you want to clean up LLSingletons, - // call LLSingletonBase::deleteAll() sometime before static-object - // destruction begins. That method will properly honor cross- - // LLSingleton dependencies. Otherwise we simply leak LLSingleton - // instances at shutdown. Since the whole process is terminating - // anyway, that's not necessarily a bad thing; it depends on what - // resources your LLSingleton instances are managing. + constructSingleton(); } }; @@ -369,7 +400,8 @@ public: static DERIVED_TYPE* getInstance() { - static SingletonLifetimeManager sLifeTimeMgr; + // call constructSingleton() only the first time we get here + static SingletonInitializer sInitializer; switch (sData.mInitState) { @@ -380,47 +412,33 @@ public: return NULL; case CONSTRUCTING: + // here if DERIVED_TYPE's constructor (directly or indirectly) + // calls DERIVED_TYPE::getInstance() logerrs("Tried to access singleton ", demangle(typeid(DERIVED_TYPE).name()).c_str(), " from singleton constructor!"); return NULL; case INITIALIZING: - // go ahead and flag ourselves as initialized so we can be - // reentrant during initialization - sData.mInitState = INITIALIZED; - // initialize singleton after constructing it so that it can - // reference other singletons which in turn depend on it, thus - // breaking cyclic dependencies - sData.mInstance->initSingleton(); - // pop this off stack of initializing singletons - LLSingleton_manage_master().pop_initializing(sData.mInstance); + // first time through: set to INITIALIZING by + // constructSingleton(), called by sInitializer's constructor + finishInitializing(); break; case INITIALIZED: + // normal subsequent calls break; case DELETED: + // called after deleteSingleton() logwarns("Trying to access deleted singleton ", demangle(typeid(DERIVED_TYPE).name()).c_str(), " -- creating new instance"); - SingletonLifetimeManager::construct(); - // same as first time construction - sData.mInitState = INITIALIZED; - sData.mInstance->initSingleton(); - // pop this off stack of initializing singletons - LLSingleton_manage_master().pop_initializing(sData.mInstance); + constructSingleton(); + finishInitializing(); break; } - // By this point, if DERIVED_TYPE was pushed onto the initializing - // stack, it has been popped off. So the top of that stack, if any, is - // an LLSingleton that directly depends on DERIVED_TYPE. If this call - // came from another LLSingleton, rather than from vanilla application - // code, record the dependency. - sData.mInstance->capture_dependency( - LLSingleton_manage_master().get_initializing(sData.mInstance), - sData.mInitState); return sData.mInstance; } @@ -446,12 +464,6 @@ public: return sData.mInitState == DELETED; } -protected: - static EInitState getInitState() - { - return sData.mInitState; - } - private: struct SingletonData { @@ -463,176 +475,138 @@ private: static SingletonData sData; }; +template +typename LLSingleton::SingletonData LLSingleton::sData; + +/** + * LLParamSingleton is like LLSingleton, except in the following ways: + * + * * It is NOT instantiated on demand (instance() or getInstance()). You must + * first call initParamSingleton(constructor args...). + * * Before initParamSingleton(), calling instance() or getInstance() dies with + * LL_ERRS. + * * initParamSingleton() may be called only once. A second call dies with + * LL_ERRS. + * * However, distinct initParamSingleton() calls can be used to engage + * different constructors, as long as only one such call is executed at + * runtime. + * * Circularity is not permitted. No LLSingleton referenced by an + * LLParamSingleton's constructor or initSingleton() method may call this + * LLParamSingleton's instance() or getInstance() methods. + * * Unlike LLSingleton, an LLParamSingleton cannot be "revived" by an + * instance() or getInstance() call after deleteSingleton(). + * + * Importantly, though, each LLParamSingleton subclass does participate in the + * dependency-ordered LLSingletonBase::deleteAll() processing. + */ template -class LLParamSingleton : public LLSingletonBase +class LLParamSingleton : public LLSingleton { private: - - template - static DERIVED_TYPE* constructSingleton(Args&&... args) - { - return new DERIVED_TYPE(std::forward(args)...); - } - - // We know of no way to instruct the compiler that every subclass - // constructor MUST be private. - // However, we can make the LLPARAMSINGLETON() macro both declare - // a private constructor and provide the required friend declaration. - // How can we ensure that every subclass uses LLPARAMSINGLETON()? - // By making that macro provide a definition for this pure virtual - // method. If you get "can't instantiate class due to missing pure - // virtual method" for this method, then add LLPARAMSINGLETON(yourclass) - // in the subclass body. - virtual void you_must_use_LLSINGLETON_macro() = 0; - -protected: - // Pass DERIVED_TYPE explicitly to LLSingletonBase's constructor because, - // until our subclass constructor completes, *this isn't yet a - // full-fledged DERIVED_TYPE. - LLParamSingleton() : LLSingletonBase(LLSingletonBase::tag()) - { - // populate base-class function pointer with the static - // deleteSingleton() function for this particular specialization - mDeleteSingleton = &deleteSingleton; - - // add this new instance to the master list - LLSingleton_manage_master().add(this); - } + typedef LLSingleton super; public: + using super::deleteSingleton; + using super::instance; + using super::instanceExists; + using super::wasDeleted; - virtual ~LLParamSingleton() - { - // remove this instance from the master list - LLSingleton_manage_master().remove(this); - sData.mInstance = NULL; - sData.mInitState = DELETED; - } - - // Passes arguments to DERIVED_TYPE's constructor and sets apropriate states + // Passes arguments to DERIVED_TYPE's constructor and sets appropriate states template static void initParamSingleton(Args&&... args) { - sData.mInitState = CONSTRUCTING; - sData.mInstance = constructSingleton(std::forward(args)...); - sData.mInitState = INITIALIZED; - // initialize singleton after constructing it so that it can - // reference other singletons which in turn depend on it, thus - // breaking cyclic dependencies - sData.mInstance->initSingleton(); - // pop this off stack of initializing singletons - LLSingleton_manage_master().pop_initializing(sData.mInstance); - } - - /** - * @brief Immediately delete the singleton. - * - * A subsequent call to LLProxy::getInstance() will construct a new - * instance of the class. - * - * Without an explicit call to LLSingletonBase::deleteAll(), LLSingletons - * are implicitly destroyed after main() has exited and the C++ runtime is - * cleaning up statically-constructed objects. Some classes derived from - * LLSingleton have objects that are part of a runtime system that is - * terminated before main() exits. Calling the destructor of those objects - * after the termination of their respective systems can cause crashes and - * other problems during termination of the project. Using this method to - * destroy the singleton early can prevent these crashes. - * - * An example where this is needed is for a LLSingleton that has an APR - * object as a member that makes APR calls on destruction. The APR system is - * shut down explicitly before main() exits. This causes a crash on exit. - * Using this method before the call to apr_terminate() and NOT calling - * getInstance() again will prevent the crash. - */ - static void deleteSingleton() - { - delete sData.mInstance; - sData.mInstance = NULL; - sData.mInitState = DELETED; + // In case racing threads both call initParamSingleton() at the same + // time, serialize them. One should initialize; the other should see + // mInitState already set. + std::unique_lock lk(mMutex); + // For organizational purposes this function shouldn't be called twice + if (super::sData.mInitState != super::UNINITIALIZED) + { + super::logerrs("Tried to initialize singleton ", + super::demangle(typeid(DERIVED_TYPE).name()).c_str(), + " twice!"); + } + else + { + super::constructSingleton(std::forward(args)...); + super::finishInitializing(); + } } static DERIVED_TYPE* getInstance() { - switch (sData.mInitState) + // In case racing threads call getInstance() at the same moment as + // initParamSingleton(), serialize the calls. + std::unique_lock lk(mMutex); + + switch (super::sData.mInitState) { - case UNINITIALIZED: - logerrs("Uninitialized param singleton ", - demangle(typeid(DERIVED_TYPE).name()).c_str()); - return NULL; + case super::UNINITIALIZED: + super::logerrs("Uninitialized param singleton ", + super::demangle(typeid(DERIVED_TYPE).name()).c_str()); + break; - case CONSTRUCTING: - logerrs("Tried to access singleton ", - demangle(typeid(DERIVED_TYPE).name()).c_str(), + case super::CONSTRUCTING: + super::logerrs("Tried to access param singleton ", + super::demangle(typeid(DERIVED_TYPE).name()).c_str(), " from singleton constructor!"); - return NULL; - - case INITIALIZING: - logerrs("State not supported by ", - demangle(typeid(DERIVED_TYPE).name()).c_str(), - " since it is a parametric singleton!"); break; - case INITIALIZED: + case super::INITIALIZING: + super::logerrs("Tried to access param singleton ", + super::demangle(typeid(DERIVED_TYPE).name()).c_str(), + " from initSingleton() method!"); break; - case DELETED: - logerrs("Trying to access deleted param singleton ", - demangle(typeid(DERIVED_TYPE).name()).c_str()); + case super::INITIALIZED: + return super::sData.mInstance; + case super::DELETED: + super::logerrs("Trying to access deleted param singleton ", + super::demangle(typeid(DERIVED_TYPE).name()).c_str()); break; } - // By this point, if DERIVED_TYPE was pushed onto the initializing - // stack, it has been popped off. So the top of that stack, if any, is - // an LLSingleton that directly depends on DERIVED_TYPE. If this call - // came from another LLSingleton, rather than from vanilla application - // code, record the dependency. - sData.mInstance->capture_dependency( - LLSingleton_manage_master().get_initializing(sData.mInstance), - sData.mInitState); - return sData.mInstance; - } - - // Reference version of getInstance() - // Preferred over getInstance() as it disallows checking for NULL - static DERIVED_TYPE& instance() - { - return *getInstance(); - } - - // Has this singleton been created yet? - // Use this to avoid accessing singletons before they can safely be constructed. - static bool instanceExists() - { - return sData.mInitState == INITIALIZED; - } - - // Has this singleton been deleted? This can be useful during shutdown - // processing to avoid "resurrecting" a singleton we thought we'd already - // cleaned up. - static bool wasDeleted() - { - return sData.mInitState == DELETED; + // should never actually get here; this is to pacify the compiler, + // which assumes control might return from logerrs() + return nullptr; } private: - struct SingletonData - { - // explicitly has a default constructor so that member variables are zero initialized in BSS - // and only changed by singleton logic, not constructor running during startup - EInitState mInitState; - DERIVED_TYPE* mInstance; - }; - static SingletonData sData; + static std::mutex mMutex; }; template -typename LLSingleton::SingletonData LLSingleton::sData; +typename std::mutex LLParamSingleton::mMutex; -template -typename LLParamSingleton::SingletonData LLParamSingleton::sData; +/** + * Initialization locked singleton, only derived class can decide when to initialize. + * Starts locked. + * For cases when singleton has a dependency onto something or. + * + * LLLockedSingleton is like an LLParamSingleton with a nullary constructor. + * It cannot be instantiated on demand (instance() or getInstance() call) -- + * it must be instantiated by calling construct(). However, it does + * participate in dependency-ordered LLSingletonBase::deleteAll() processing. + */ +template +class LLLockedSingleton : public LLParamSingleton
+{ + typedef LLParamSingleton
super; + +public: + using super::deleteSingleton; + using super::getInstance; + using super::instance; + using super::instanceExists; + using super::wasDeleted; + + static void construct() + { + super::initParamSingleton(); + } +}; /** * Use LLSINGLETON(Foo); at the start of an LLSingleton subclass body @@ -658,13 +632,13 @@ typename LLParamSingleton::SingletonData LLParamSingleton::sData; * file, use 'inline' (unless it's a template class) to avoid duplicate-symbol * errors at link time. */ -#define LLSINGLETON(DERIVED_CLASS) \ +#define LLSINGLETON(DERIVED_CLASS, ...) \ private: \ /* implement LLSingleton pure virtual method whose sole purpose */ \ /* is to remind people to use this macro */ \ virtual void you_must_use_LLSINGLETON_macro() {} \ friend class LLSingleton; \ - DERIVED_CLASS() + DERIVED_CLASS(__VA_ARGS__) /** * Use LLSINGLETON_EMPTY_CTOR(Foo); at the start of an LLSingleton @@ -684,35 +658,4 @@ private: \ /* LLSINGLETON() is carefully implemented to permit exactly this */ \ LLSINGLETON(DERIVED_CLASS) {} -/** -* Use LLPARAMSINGLETON(Foo); at the start of an LLParamSingleton subclass body -* when you want to declare an out-of-line constructor: -* -* @code -* class Foo: public LLParamSingleton -* { -* // use this macro at start of every LLSingleton subclass -* LLPARAMSINGLETON(Foo); -* public: -* // ... -* }; -* // ... -* [inline] -* Foo::Foo() { ... } -* @endcode -* -* Unfortunately, this mechanism does not permit you to define even a simple -* (but nontrivial) constructor within the class body. Use LLPARAMSINGLETON() -* and define the constructor outside the class body. If you must define it -* in a header file, use 'inline' (unless it's a template class) to avoid -* duplicate-symbol errors at link time. -*/ -#define LLPARAMSINGLETON(DERIVED_CLASS, ...) \ -private: \ - /* implement LLSingleton pure virtual method whose sole purpose */ \ - /* is to remind people to use this macro */ \ - virtual void you_must_use_LLSINGLETON_macro() {} \ - friend class LLParamSingleton; \ - DERIVED_CLASS(__VA_ARGS__) - #endif diff --git a/indra/llimage/llimage.h b/indra/llimage/llimage.h index e5526ba9c0..9f8d061293 100644 --- a/indra/llimage/llimage.h +++ b/indra/llimage/llimage.h @@ -91,7 +91,7 @@ typedef enum e_image_codec class LLImage : public LLParamSingleton { - LLPARAMSINGLETON(LLImage, bool use_new_byte_range = false, S32 minimal_reverse_byte_range_percent = 75); + LLSINGLETON(LLImage, bool use_new_byte_range = false, S32 minimal_reverse_byte_range_percent = 75); ~LLImage(); public: diff --git a/indra/llrender/llrender2dutils.h b/indra/llrender/llrender2dutils.h index f2640ff998..cf408336e6 100644 --- a/indra/llrender/llrender2dutils.h +++ b/indra/llrender/llrender2dutils.h @@ -123,7 +123,7 @@ class LLImageProviderInterface; class LLRender2D : public LLParamSingleton { - LLPARAMSINGLETON(LLRender2D, LLImageProviderInterface* image_provider); + LLSINGLETON(LLRender2D, LLImageProviderInterface* image_provider); LOG_CLASS(LLRender2D); ~LLRender2D(); public: diff --git a/indra/llui/llui.h b/indra/llui/llui.h index 9decaf92cb..e1c51deba9 100644 --- a/indra/llui/llui.h +++ b/indra/llui/llui.h @@ -115,7 +115,7 @@ public: typedef std::map settings_map_t; private: - LLPARAMSINGLETON(LLUI , const settings_map_t &settings, + LLSINGLETON(LLUI , const settings_map_t &settings, LLImageProviderInterface* image_provider, LLUIAudioCallback audio_callback, LLUIAudioCallback deferred_audio_callback); diff --git a/indra/newview/llconversationlog.h b/indra/newview/llconversationlog.h index 38247f8eff..f241276abb 100644 --- a/indra/newview/llconversationlog.h +++ b/indra/newview/llconversationlog.h @@ -109,7 +109,7 @@ private: class LLConversationLog : public LLParamSingleton, LLIMSessionObserver { - LLPARAMSINGLETON(LLConversationLog); + LLSINGLETON(LLConversationLog); public: void removeConversation(const LLConversation& conversation); -- cgit v1.3 From 1233842012a257b7eb49eab354bb945593c974ed Mon Sep 17 00:00:00 2001 From: andreykproductengine Date: Tue, 13 Aug 2019 17:22:58 +0300 Subject: SL-11718 Crash in LLRender2D --- indra/llrender/llrender2dutils.cpp | 43 ++++++++++++++++++++++++++++++++++++++ indra/llrender/llrender2dutils.h | 19 ++++++++++++++++- 2 files changed, 61 insertions(+), 1 deletion(-) (limited to 'indra/llrender') diff --git a/indra/llrender/llrender2dutils.cpp b/indra/llrender/llrender2dutils.cpp index 508bccec11..4eb0203245 100644 --- a/indra/llrender/llrender2dutils.cpp +++ b/indra/llrender/llrender2dutils.cpp @@ -1570,6 +1570,10 @@ LLRender2D::LLRender2D(LLImageProviderInterface* image_provider) { mGLScaleFactor = LLVector2(1.f, 1.f); mImageProvider = image_provider; + if(mImageProvider) + { + mImageProvider->addOnRemovalCallback(resetProvider); + } } LLRender2D::~LLRender2D() @@ -1577,6 +1581,7 @@ LLRender2D::~LLRender2D() if(mImageProvider) { mImageProvider->cleanUp(); + mImageProvider->deleteOnRemovalCallback(resetProvider); } } @@ -1642,3 +1647,41 @@ LLPointer LLRender2D::getUIImage(const std::string& name, S32 priorit return NULL; } +// static +void LLRender2D::resetProvider() +{ + if (LLRender2D::instanceExists) + { + LLRender2D::getInstance()->mImageProvider = NULL; + } +} + +// class LLImageProviderInterface + +LLImageProviderInterface::~LLImageProviderInterface() +{ + for (callback_list_t::iterator iter = mCallbackList.begin(); iter != mCallbackList.end();) + { + callback_list_t::iterator curiter = iter++; + (*curiter)(); + } +} + +void LLImageProviderInterface::addOnRemovalCallback(callback_t func) +{ + if (!func) + { + return; + } + mCallbackList.push_back(func); +} + +void LLImageProviderInterface::deleteOnRemovalCallback(callback_t func) +{ + callback_list_t::iterator iter = std::find(mCallbackList.begin(), mCallbackList.end(), func); + if (iter != mCallbackList.end()) + { + mCallbackList.erase(iter); + } +} + diff --git a/indra/llrender/llrender2dutils.h b/indra/llrender/llrender2dutils.h index cf408336e6..70ab006fd6 100644 --- a/indra/llrender/llrender2dutils.h +++ b/indra/llrender/llrender2dutils.h @@ -139,6 +139,13 @@ public: LLPointer getUIImage(const std::string& name, S32 priority = 0); LLVector2 mGLScaleFactor; + +protected: + // since LLRender2D has no control of image provider's lifecycle + // we need a way to tell LLRender2D that provider died and + // LLRender2D needs to be updated. + static void resetProvider(); + private: LLImageProviderInterface* mImageProvider; }; @@ -147,11 +154,21 @@ class LLImageProviderInterface { protected: LLImageProviderInterface() {}; - virtual ~LLImageProviderInterface() {}; + virtual ~LLImageProviderInterface(); public: virtual LLPointer getUIImage(const std::string& name, S32 priority) = 0; virtual LLPointer getUIImageByID(const LLUUID& id, S32 priority) = 0; virtual void cleanUp() = 0; + + // to notify holders when pointer gets deleted + typedef void(*callback_t)(); + void addOnRemovalCallback(callback_t func); + void deleteOnRemovalCallback(callback_t func); + +private: + + typedef std::list< callback_t > callback_list_t; + callback_list_t mCallbackList; }; -- cgit v1.3 From db344c5844c8f2dcb941de16e9dcdae52296dfe9 Mon Sep 17 00:00:00 2001 From: andreykproductengine Date: Tue, 12 Nov 2019 16:07:36 +0200 Subject: Fix instance existance check --- indra/llrender/llrender2dutils.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'indra/llrender') diff --git a/indra/llrender/llrender2dutils.cpp b/indra/llrender/llrender2dutils.cpp index 4eb0203245..801b945806 100644 --- a/indra/llrender/llrender2dutils.cpp +++ b/indra/llrender/llrender2dutils.cpp @@ -1650,7 +1650,7 @@ LLPointer LLRender2D::getUIImage(const std::string& name, S32 priorit // static void LLRender2D::resetProvider() { - if (LLRender2D::instanceExists) + if (LLRender2D::instanceExists()) { LLRender2D::getInstance()->mImageProvider = NULL; } -- cgit v1.3