Bug 1801778 - Add some assertions in nsFontCache. r=aosmond

Differential Revision: https://phabricator.services.mozilla.com/D162834
This commit is contained in:
Jonathan Kew 2022-11-28 17:16:34 +00:00
Родитель 08a9cdce1d
Коммит 4fd05a6382
1 изменённых файлов: 12 добавлений и 0 удалений

Просмотреть файл

@ -7,6 +7,7 @@
#include "gfxTextRun.h"
#include "mozilla/Services.h"
#include "mozilla/ServoUtils.h"
#include "nsCRT.h"
using mozilla::services::GetObserverService;
@ -17,6 +18,7 @@ NS_IMPL_ISUPPORTS(nsFontCache, nsIObserver)
// safe to call AddObserver from a constructor or RemoveObserver
// from a destructor. That should be fixed.
void nsFontCache::Init(nsPresContext* aContext) {
MOZ_DIAGNOSTIC_ASSERT(NS_IsMainThread());
mContext = aContext;
// register as a memory-pressure observer to free font resources
// in low-memory situations.
@ -32,6 +34,7 @@ void nsFontCache::Init(nsPresContext* aContext) {
}
void nsFontCache::Destroy() {
MOZ_DIAGNOSTIC_ASSERT(NS_IsMainThread());
nsCOMPtr<nsIObserverService> obs = GetObserverService();
if (obs) {
obs->RemoveObserver(this, "memory-pressure");
@ -41,6 +44,7 @@ void nsFontCache::Destroy() {
NS_IMETHODIMP
nsFontCache::Observe(nsISupports*, const char* aTopic, const char16_t*) {
MOZ_DIAGNOSTIC_ASSERT(NS_IsMainThread());
if (!nsCRT::strcmp(aTopic, "memory-pressure")) {
Compact();
}
@ -49,6 +53,10 @@ nsFontCache::Observe(nsISupports*, const char* aTopic, const char16_t*) {
already_AddRefed<nsFontMetrics> nsFontCache::GetMetricsFor(
const nsFont& aFont, const nsFontMetrics::Params& aParams) {
// We may eventually want to put an nsFontCache on canvas2d workers, but for
// now it is only used by the main-thread layout code and stylo.
mozilla::AssertIsMainThreadOrServoFontMetricsLocked();
nsAtom* language = aParams.language && !aParams.language->IsEmpty()
? aParams.language
: mLocaleLanguage.get();
@ -97,6 +105,7 @@ already_AddRefed<nsFontMetrics> nsFontCache::GetMetricsFor(
}
void nsFontCache::UpdateUserFonts(gfxUserFontSet* aUserFontSet) {
MOZ_DIAGNOSTIC_ASSERT(NS_IsMainThread());
for (nsFontMetrics* fm : mFontMetrics) {
gfxFontGroup* fg = fm->GetThebesFontGroup();
if (fg->GetUserFontSet() == aUserFontSet) {
@ -106,10 +115,12 @@ void nsFontCache::UpdateUserFonts(gfxUserFontSet* aUserFontSet) {
}
void nsFontCache::FontMetricsDeleted(const nsFontMetrics* aFontMetrics) {
MOZ_DIAGNOSTIC_ASSERT(NS_IsMainThread());
mFontMetrics.RemoveElement(aFontMetrics);
}
void nsFontCache::Compact() {
MOZ_DIAGNOSTIC_ASSERT(NS_IsMainThread());
// Need to loop backward because the running element can be removed on
// the way
for (int32_t i = mFontMetrics.Length() - 1; i >= 0; --i) {
@ -129,6 +140,7 @@ void nsFontCache::Compact() {
// Flush the aFlushCount oldest entries, or all if (aFlushCount < 0)
void nsFontCache::Flush(int32_t aFlushCount) {
MOZ_DIAGNOSTIC_ASSERT(NS_IsMainThread());
int32_t n = aFlushCount < 0
? mFontMetrics.Length()
: std::min<int32_t>(aFlushCount, mFontMetrics.Length());