diff --git a/indra/llui/llfocusmgr.cpp b/indra/llui/llfocusmgr.cpp
index b57d4461c427277fa8850a337b82e8ef51635f2c..f117c8237dc9b371dd2f27b907bc62bdaae1fe1b 100644
--- a/indra/llui/llfocusmgr.cpp
+++ b/indra/llui/llfocusmgr.cpp
@@ -197,8 +197,8 @@ void LLFocusMgr::setKeyboardFocus(LLFocusableElement* new_focus, BOOL lock, BOOL
 	if (mLockedView && 
 		(new_focus == NULL || 
 			(new_focus != mLockedView 
-			&& dynamic_cast<LLView*>(new_focus)
-			&& !dynamic_cast<LLView*>(new_focus)->hasAncestor(mLockedView))))
+			&& new_focus->isFocusView()
+			&& !static_cast<LLView*>(new_focus)->hasAncestor(mLockedView))))
 	{
 		// don't allow focus to go to anything that is not the locked focus
 		// or one of its descendants
@@ -217,9 +217,12 @@ void LLFocusMgr::setKeyboardFocus(LLFocusableElement* new_focus, BOOL lock, BOOL
 		view_handle_list_t new_focus_list;
 
 		// walk up the tree to root and add all views to the new_focus_list
-		for (LLView* ctrl = dynamic_cast<LLView*>(mKeyboardFocus); ctrl; ctrl = ctrl->getParent())
+		if (mKeyboardFocus && mKeyboardFocus->isFocusView())
 		{
-			new_focus_list.push_back(ctrl->getHandle());
+			for (LLView* ctrl = static_cast<LLView*>(mKeyboardFocus); ctrl; ctrl = ctrl->getParent())
+			{
+				new_focus_list.push_back(ctrl->getHandle());
+			}
 		}
 
 		// remove all common ancestors since their focus is unchanged
@@ -272,23 +275,25 @@ void LLFocusMgr::setKeyboardFocus(LLFocusableElement* new_focus, BOOL lock, BOOL
 			mDefaultKeyboardFocus->setFocus(TRUE);
 		}
 
-		LLView* focus_subtree = dynamic_cast<LLView*>(mKeyboardFocus);
-		LLView* viewp = dynamic_cast<LLView*>(mKeyboardFocus);
-		// find root-most focus root
-		while(viewp)
+		if (mKeyboardFocus && mKeyboardFocus->isFocusView())
 		{
-			if (viewp->isFocusRoot())
+			LLView* focus_subtree = static_cast<LLView*>(mKeyboardFocus);
+			LLView* viewp = static_cast<LLView*>(mKeyboardFocus);
+			// find root-most focus root
+			while (viewp)
 			{
-				focus_subtree = viewp;
+				if (viewp->isFocusRoot())
+				{
+					focus_subtree = viewp;
+				}
+				viewp = viewp->getParent();
 			}
-			viewp = viewp->getParent();
-		}
 
-		
-		if (focus_subtree)
-		{
-			LLView* focused_view = dynamic_cast<LLView*>(mKeyboardFocus);
-			mImpl->mFocusHistory[focus_subtree->getHandle()] = focused_view ? focused_view->getHandle() : LLHandle<LLView>(); 
+			if (focus_subtree)
+			{
+				LLView* focused_view = static_cast<LLView*>(mKeyboardFocus);
+				mImpl->mFocusHistory[focus_subtree->getHandle()] = focused_view ? focused_view->getHandle() : LLHandle<LLView>();
+			}
 		}
 	}
 	
@@ -304,7 +309,10 @@ void LLFocusMgr::setKeyboardFocus(LLFocusableElement* new_focus, BOOL lock, BOOL
 // Returns TRUE is parent or any descedent of parent has keyboard focus.
 BOOL LLFocusMgr::childHasKeyboardFocus(const LLView* parent ) const
 {
-	LLView* focus_view = dynamic_cast<LLView*>(mKeyboardFocus);
+	if (!mKeyboardFocus || !mKeyboardFocus->isFocusView())
+		return FALSE;
+
+	LLView* focus_view = static_cast<LLView*>(mKeyboardFocus);
 	while( focus_view )
 	{
 		if( focus_view == parent )
@@ -351,15 +359,18 @@ void LLFocusMgr::removeKeyboardFocusWithoutCallback( const LLFocusableElement* f
 
 bool LLFocusMgr::keyboardFocusHasAccelerators() const
 {
-	LLView* focus_view = dynamic_cast<LLView*>(mKeyboardFocus);
-	while( focus_view )
+	if (mKeyboardFocus && mKeyboardFocus->isFocusView())
 	{
-		if(focus_view->hasAccelerators())
+		LLView* focus_view = static_cast<LLView*>(mKeyboardFocus);
+		while (focus_view)
 		{
-			return true;
-		}
+			if (focus_view->hasAccelerators())
+			{
+				return true;
+			}
 
-		focus_view = focus_view->getParent();
+			focus_view = focus_view->getParent();
+		}
 	}
 	return false;
 }
@@ -441,7 +452,7 @@ void LLFocusMgr::removeTopCtrlWithoutCallback( const LLUICtrl* top_view )
 
 void LLFocusMgr::lockFocus()
 {
-	mLockedView = dynamic_cast<LLUICtrl*>(mKeyboardFocus); 
+	mLockedView = (mKeyboardFocus && mKeyboardFocus->isFocusView() && static_cast<LLView*>(mKeyboardFocus)->isCtrl()) ? static_cast<LLUICtrl*>(mKeyboardFocus) : nullptr;
 }
 
 void LLFocusMgr::unlockFocus()
diff --git a/indra/llui/llfocusmgr.h b/indra/llui/llfocusmgr.h
index afce6154351b6e1c72ee8b1a7d90e6d7f1c0e4c6..244bfb941318da30970574361fa6ae58ca1b3aaa 100644
--- a/indra/llui/llfocusmgr.h
+++ b/indra/llui/llfocusmgr.h
@@ -45,6 +45,8 @@ class LLFocusableElement
 	LLFocusableElement();
 	virtual ~LLFocusableElement();
 
+	virtual bool isFocusView() const { return false; }
+
 	virtual void	setFocus( BOOL b );
 	virtual BOOL	hasFocus() const;
 
diff --git a/indra/llui/llscrollcontainer.cpp b/indra/llui/llscrollcontainer.cpp
index 3db38bbfacf56e2bb6bee810757771fd7041427f..ef5d71a93f5f9f39c3773e57aa0a047afa552c76 100644
--- a/indra/llui/llscrollcontainer.cpp
+++ b/indra/llui/llscrollcontainer.cpp
@@ -414,7 +414,7 @@ bool LLScrollContainer::autoScroll(S32 x, S32 y, bool do_scroll)
 
 void LLScrollContainer::calcVisibleSize( S32 *visible_width, S32 *visible_height, BOOL* show_h_scrollbar, BOOL* show_v_scrollbar ) const
 {
-	const LLRect& doc_rect = getScrolledViewRect();
+	const LLRect doc_rect = getScrolledViewRect();
 	static LLUICachedControl<S32> scrollbar_size_control ("UIScrollbarSize", 0);
 	S32 scrollbar_size = (mSize == -1 ? scrollbar_size_control : mSize);
 
diff --git a/indra/llui/llview.h b/indra/llui/llview.h
index 7f0e54ddd6ed66f04a75e31ed203174a15ce2b36..4b2d2b47646df2744de048b64963df5acdc81b26 100644
--- a/indra/llui/llview.h
+++ b/indra/llui/llview.h
@@ -169,6 +169,9 @@ class LLView
 	// widgets in general are not copyable
 	LLView(const LLView& other);
 public:
+
+	bool isFocusView() const override { return true; }
+
 //#if LL_DEBUG
 	static BOOL sIsDrawing;
 //#endif
diff --git a/indra/newview/llviewerwindow.cpp b/indra/newview/llviewerwindow.cpp
index 1b2b5223d2a4e6b87a23b7713914447e39b9faa7..71aa7437a6655caeadd6d5f71179aec8af8366e2 100644
--- a/indra/newview/llviewerwindow.cpp
+++ b/indra/newview/llviewerwindow.cpp
@@ -2663,6 +2663,11 @@ void LLViewerWindow::setMenuBackgroundColor(bool god_mode, bool dev_grid)
 
 void LLViewerWindow::drawDebugText()
 {
+	if (!gPipeline.hasRenderDebugFeatureMask(LLPipeline::RENDER_DEBUG_FEATURE_UI))
+	{
+		return;
+	}
+
 	gGL.color4f(1,1,1,1);
 	gGL.pushMatrix();
 	gGL.pushUIMatrix();