From 02b2136122459ed32d4665ec502efdbf95af019f Mon Sep 17 00:00:00 2001 From: Ehsan Akhgari Date: Tue, 13 Jun 2017 20:09:22 -0400 Subject: [PATCH] Bug 1372761 - Avoid needlessly grabbing the timer thread lock when selections change in order to reset the caret timer; r=mats --- layout/base/nsCaret.cpp | 15 +++++++++++++-- layout/base/nsCaret.h | 6 ++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/layout/base/nsCaret.cpp b/layout/base/nsCaret.cpp index cb8a0699227e..795475c36850 100644 --- a/layout/base/nsCaret.cpp +++ b/layout/base/nsCaret.cpp @@ -46,6 +46,9 @@ using namespace mozilla::gfx; // an insignificant dot static const int32_t kMinBidiIndicatorPixels = 2; +// The default caret blinking rate (in ms of blinking interval) +static const uint32_t kDefaultCaretBlinkRate = 500; + /** * Find the first frame in an in-order traversal of the frame subtree rooted * at aFrame which is either a text frame logically at the end of a line, @@ -120,6 +123,7 @@ IsBidiUI() nsCaret::nsCaret() : mOverrideOffset(0) , mBlinkCount(-1) +, mBlinkRate(0) , mHideCount(0) , mIsBlinkOn(false) , mVisible(false) @@ -636,6 +640,15 @@ void nsCaret::ResetBlinking() return; } + uint32_t blinkRate = static_cast( + LookAndFeel::GetInt(LookAndFeel::eIntID_CaretBlinkTime, + kDefaultCaretBlinkRate)); + if (mBlinkRate == blinkRate) { + // If the rate hasn't changed, then there is nothing to do. + return; + } + mBlinkRate = blinkRate; + if (mBlinkTimer) { mBlinkTimer->Cancel(); } else { @@ -645,8 +658,6 @@ void nsCaret::ResetBlinking() return; } - uint32_t blinkRate = static_cast( - LookAndFeel::GetInt(LookAndFeel::eIntID_CaretBlinkTime, 500)); if (blinkRate > 0) { mBlinkCount = Preferences::GetInt("ui.caretBlinkCount", -1); mBlinkTimer->InitWithNamedFuncCallback(CaretBlinkCallback, this, blinkRate, diff --git a/layout/base/nsCaret.h b/layout/base/nsCaret.h index 6394d592c7ad..519584a42b83 100644 --- a/layout/base/nsCaret.h +++ b/layout/base/nsCaret.h @@ -231,6 +231,12 @@ protected: * blinking. */ int32_t mBlinkCount; + /** + * mBlinkRate is the rate of the caret blinking the last time we read it. + * It is used as a way to optimize whether we need to reset the blinking + * timer. + */ + uint32_t mBlinkRate; /** * mHideCount is not 0, it means that somebody doesn't want the caret * to be visible. See AddForceHide() and RemoveForceHide().