skia2/include/ports/SkFontMgr_indirect.h
mtklein d9dd428211 Modernize and trim down SkOnce.
The API and implementation are very much simplified.
You may not want to bother reading the diff.

As is our trend, SkOnce now uses <atomic> directly.

Member initialization means we don't need SK_DECLARE_STATIC_ONCE.
SkSpinlock already works this same way.

All uses of the old API taking an external bool* and Lock* were pessimal,
so I have not carried this sort of API forward.  It's simpler, faster,
and more space-efficient to always use this single SkOnce class interface.

SkOnce weighs 2 bytes: a done bool and an SkSpinlock, also a bool internally.
This API refactoring opens up the opportunity to fuse those into a single
three-state byte if we'd like.

No public API changes.
TBR=reed@google.com

BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1894893002

Review URL: https://codereview.chromium.org/1894893002
2016-04-18 08:09:11 -07:00

106 lines
3.5 KiB
C++

/*
* Copyright 2014 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SkFontMgr_indirect_DEFINED
#define SkFontMgr_indirect_DEFINED
#include "../private/SkMutex.h"
#include "../private/SkOnce.h"
#include "../private/SkTArray.h"
#include "SkDataTable.h"
#include "SkFontMgr.h"
#include "SkRefCnt.h"
#include "SkRemotableFontMgr.h"
#include "SkTypeface.h"
#include "SkTypes.h"
class SkData;
class SkFontStyle;
class SkStreamAsset;
class SkString;
class SK_API SkFontMgr_Indirect : public SkFontMgr {
public:
// TODO: The SkFontMgr is only used for createFromStream/File/Data.
// In the future these calls should be broken out into their own interface
// with a name like SkFontRenderer.
SkFontMgr_Indirect(SkFontMgr* impl, SkRemotableFontMgr* proxy)
: fImpl(SkRef(impl)), fProxy(SkRef(proxy))
{ }
protected:
int onCountFamilies() const override;
void onGetFamilyName(int index, SkString* familyName) const override;
SkFontStyleSet* onCreateStyleSet(int index) const override;
SkFontStyleSet* onMatchFamily(const char familyName[]) const override;
SkTypeface* onMatchFamilyStyle(const char familyName[],
const SkFontStyle& fontStyle) const override;
SkTypeface* onMatchFamilyStyleCharacter(const char familyName[],
const SkFontStyle&,
const char* bcp47[],
int bcp47Count,
SkUnichar character) const override;
SkTypeface* onMatchFaceStyle(const SkTypeface* familyMember,
const SkFontStyle& fontStyle) const override;
SkTypeface* onCreateFromStream(SkStreamAsset* stream, int ttcIndex) const override;
SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override;
SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const override;
SkTypeface* onLegacyCreateTypeface(const char familyName[], SkFontStyle) const override;
private:
SkTypeface* createTypefaceFromFontId(const SkFontIdentity& fontId) const;
SkAutoTUnref<SkFontMgr> fImpl;
SkAutoTUnref<SkRemotableFontMgr> fProxy;
struct DataEntry {
uint32_t fDataId; // key1
uint32_t fTtcIndex; // key2
SkTypeface* fTypeface; // value: weak ref to typeface
DataEntry() { }
DataEntry(DataEntry&& that)
: fDataId(that.fDataId)
, fTtcIndex(that.fTtcIndex)
, fTypeface(that.fTypeface)
{
SkDEBUGCODE(that.fDataId = SkFontIdentity::kInvalidDataId;)
SkDEBUGCODE(that.fTtcIndex = 0xbbadbeef;)
that.fTypeface = NULL;
}
~DataEntry() {
if (fTypeface) {
fTypeface->weak_unref();
}
}
};
/**
* This cache is essentially { dataId: { ttcIndex: typeface } }
* For data caching we want a mapping from data id to weak references to
* typefaces with that data id. By storing the index next to the typeface,
* this data cache also acts as a typeface cache.
*/
mutable SkTArray<DataEntry> fDataCache;
mutable SkMutex fDataCacheMutex;
mutable SkAutoTUnref<SkDataTable> fFamilyNames;
mutable SkOnce fFamilyNamesInitOnce;
static void set_up_family_names(const SkFontMgr_Indirect* self);
friend class SkStyleSet_Indirect;
};
#endif