Bug 1364628 - implement thread-safe variants of FT_New_Face/FT_Done_Face. r=jrmuizel

MozReview-Commit-ID: 6xbOm2nYhXo
This commit is contained in:
Lee Salzman committed 2017-05-17 21:56:24 -04:00
1 parent 2f24c4f229
commit eca3956cf5
9 files changed
+154 -79

No files matched your search

+11 -5
View File
@@ -31,10 +31,6 @@
#include "mozilla/DebugOnly.h"
#ifdef MOZ_ENABLE_FREETYPE
#include <string>
#endif
struct _cairo_surface;
typedef _cairo_surface cairo_surface_t;
@@ -47,6 +43,9 @@ typedef _FcPattern FcPattern;
struct FT_LibraryRec_;
typedef FT_LibraryRec_* FT_Library;
struct FT_FaceRec_;
typedef FT_FaceRec_* FT_Face;
struct ID3D11Texture2D;
struct ID3D11Device;
struct ID2D1Device;
@@ -63,6 +62,8 @@ typedef struct CGContext *CGContextRef;
namespace mozilla {
class Mutex;
namespace gfx {
class UnscaledFont;
}
@@ -820,7 +821,7 @@ protected:
* Derived classes hold a native font resource from which to create
* ScaledFonts.
*/
class NativeFontResource : public RefCounted<NativeFontResource>
class NativeFontResource : public external::AtomicRefCounted<NativeFontResource>
{
public:
MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(NativeFontResource)
@@ -1616,8 +1617,13 @@ public:
static void SetFTLibrary(FT_Library aFTLibrary);
static FT_Library GetFTLibrary();
static FT_Face NewFTFace(FT_Library aFTLibrary, const char* aFileName, int aFaceIndex);
static FT_Face NewFTFaceFromData(FT_Library aFTLibrary, const uint8_t* aData, size_t aDataSize, int aFaceIndex);
static void ReleaseFTFace(FT_Face aFace);
private:
static FT_Library mFTLibrary;
static Mutex* mFTLock;
public:
#endif
+85 -2
View File
@@ -59,6 +59,13 @@
#include "mozilla/CheckedInt.h"
#ifdef MOZ_ENABLE_FREETYPE
#include "ft2build.h"
#include FT_FREETYPE_H
#include "mozilla/Mutex.h"
#endif
#if defined(MOZ_LOGGING)
GFX2D_API mozilla::LogModule*
GetGFX2DLog()
@@ -155,6 +162,30 @@ HasCPUIDBit(unsigned int level, CPUIDRegister reg, unsigned int bit)
#endif
#endif
#ifdef MOZ_ENABLE_FREETYPE
extern "C" {
FT_Face
mozilla_NewFTFace(FT_Library aFTLibrary, const char* aFileName, int aFaceIndex)
{
return mozilla::gfx::Factory::NewFTFace(aFTLibrary, aFileName, aFaceIndex);
}
FT_Face
mozilla_NewFTFaceFromData(FT_Library aFTLibrary, const uint8_t* aData, size_t aDataSize, int aFaceIndex)
{
return mozilla::gfx::Factory::NewFTFaceFromData(aFTLibrary, aData, aDataSize, aFaceIndex);
}
void
mozilla_ReleaseFTFace(FT_Face aFace)
{
mozilla::gfx::Factory::ReleaseFTFace(aFace);
}
}
#endif
namespace mozilla {
namespace gfx {
@@ -163,6 +194,7 @@ int32_t LoggingPrefs::sGfxLogLevel = LOG_DEFAULT;
#ifdef MOZ_ENABLE_FREETYPE
FT_Library Factory::mFTLibrary = nullptr;
Mutex* Factory::mFTLock = nullptr;
#endif
#ifdef WIN32
@@ -192,6 +224,10 @@ Factory::Init(const Config& aConfig)
if (sConfig->mMaxTextureSize < kMinSizePref) {
sConfig->mMaxTextureSize = kMinSizePref;
}
#ifdef MOZ_ENABLE_FREETYPE
mFTLock = new Mutex("Factory::mFTLock");
#endif
}
void
@@ -204,8 +240,10 @@ Factory::ShutDown()
}
#ifdef MOZ_ENABLE_FREETYPE
if (mFTLibrary) {
mFTLibrary = nullptr;
mFTLibrary = nullptr;
if (mFTLock) {
delete mFTLock;
mFTLock = nullptr;
}
#endif
}
@@ -622,6 +660,51 @@ Factory::GetFTLibrary()
MOZ_ASSERT(mFTLibrary);
return mFTLibrary;
}
FT_Face
Factory::NewFTFace(FT_Library aFTLibrary, const char* aFileName, int aFaceIndex)
{
MOZ_ASSERT(mFTLock);
MutexAutoLock lock(*mFTLock);
if (!aFTLibrary) {
aFTLibrary = mFTLibrary;
}
FT_Face face;
if (FT_New_Face(aFTLibrary, aFileName, aFaceIndex, &face) != FT_Err_Ok) {
return nullptr;
}
return face;
}
FT_Face
Factory::NewFTFaceFromData(FT_Library aFTLibrary, const uint8_t* aData, size_t aDataSize, int aFaceIndex)
{
MOZ_ASSERT(mFTLock);
MutexAutoLock lock(*mFTLock);
if (!aFTLibrary) {
aFTLibrary = mFTLibrary;
}
FT_Face face;
if (FT_New_Memory_Face(aFTLibrary, aData, aDataSize, aFaceIndex, &face) != FT_Err_Ok) {
return nullptr;
}
return face;
}
void
Factory::ReleaseFTFace(FT_Face aFace)
{
// May be called during shutdown when the lock is already destroyed.
// However, there are no other threads using the face by this point,
// so it is safe to skip locking if the lock is not around.
if (mFTLock) {
mFTLock->Lock();
}
FT_Done_Face(aFace);
if (mFTLock) {
mFTLock->Unlock();
}
}
#endif
#ifdef WIN32
+4 -4
View File
@@ -21,7 +21,7 @@ NativeFontResourceFontconfig::NativeFontResourceFontconfig(UniquePtr<uint8_t[]>&
NativeFontResourceFontconfig::~NativeFontResourceFontconfig()
{
if (mFace) {
FT_Done_Face(mFace);
Factory::ReleaseFTFace(mFace);
mFace = nullptr;
}
}
@@ -35,12 +35,12 @@ NativeFontResourceFontconfig::Create(uint8_t *aFontData, uint32_t aDataLength)
UniquePtr<uint8_t[]> fontData(new uint8_t[aDataLength]);
memcpy(fontData.get(), aFontData, aDataLength);
FT_Face face;
if (FT_New_Memory_Face(Factory::GetFTLibrary(), fontData.get(), aDataLength, 0, &face) != FT_Err_Ok) {
FT_Face face = Factory::NewFTFaceFromData(nullptr, fontData.get(), aDataLength, 0);
if (!face) {
return nullptr;
}
if (FT_Select_Charmap(face, FT_ENCODING_UNICODE) != FT_Err_Ok) {
FT_Done_Face(face);
Factory::ReleaseFTFace(face);
return nullptr;
}
+1 -1
View File
@@ -33,7 +33,7 @@ public:
~UnscaledFontFreeType()
{
if (mOwnsFace) {
FT_Done_Face(mFace);
Factory::ReleaseFTFace(mFace);
}
}
+7 -5
View File
@@ -104,6 +104,10 @@ static setLcdFilterFunc setLcdFilter;
*/
#define MAX_FONT_SIZE 1000
extern FT_Face mozilla_NewFTFace(FT_Library aFTLibrary, const char* aFileName, int aFaceIndex);
extern FT_Face mozilla_NewFTFaceFromData(FT_Library aFTLibrary, const uint8_t* aData, size_t aDataSize, int aFaceIndex);
extern void mozilla_ReleaseFTFace(FT_Face aFace);
/**
* SECTION:cairo-ft
* @Title: FreeType Fonts
@@ -243,7 +247,7 @@ _font_map_release_face_lock_held (cairo_ft_unscaled_font_map_t *font_map,
cairo_ft_unscaled_font_t *unscaled)
{
if (unscaled->face) {
FT_Done_Face (unscaled->face);
mozilla_ReleaseFTFace (unscaled->face);
unscaled->face = NULL;
unscaled->have_scale = FALSE;
@@ -669,10 +673,8 @@ _cairo_ft_unscaled_font_lock_face (cairo_ft_unscaled_font_t *unscaled)
}
_cairo_ft_unscaled_font_map_unlock ();
if (FT_New_Face (font_map->ft_library,
unscaled->filename,
unscaled->id,
&face) != FT_Err_Ok)
face = mozilla_NewFTFace (font_map->ft_library, unscaled->filename, unscaled->id);
if (!face)
{
unscaled->lock_count--;
CAIRO_MUTEX_UNLOCK (unscaled->mutex);
+2
View File
@@ -95,6 +95,8 @@ gfxAndroidPlatform::gfxAndroidPlatform()
FT_New_Library(&sFreetypeMemoryRecord, &gPlatformFTLibrary);
FT_Add_Default_Modules(gPlatformFTLibrary);
Factory::SetFTLibrary(gPlatformFTLibrary);
RegisterStrongMemoryReporter(new FreetypeReporter());
mOffscreenFormat = GetScreenDepth() == 16
+21 -30
View File
@@ -85,8 +85,6 @@ public:
NS_ASSERTION(!aFontEntry->mFilename.IsEmpty(),
"can't use AutoFTFace for fonts without a filename");
FT_Library ft = gfxPlatform::GetPlatform()->GetFTLibrary();
MOZ_ASSERT(ft);
// A relative path (no initial "/") means this is a resource in
// omnijar, not an installed font on the device.
@@ -108,15 +106,14 @@ public:
NS_ASSERTION(bufSize == item->RealSize(),
"error reading bundled font");
if (FT_Err_Ok != FT_New_Memory_Face(ft, mFontDataBuf, bufSize,
aFontEntry->mFTFontIndex,
&mFace)) {
mFace = Factory::NewFTFaceFromData(nullptr, mFontDataBuf, bufSize, aFontEntry->mFTFontIndex);
if (!mFace) {
NS_WARNING("failed to create freetype face");
}
}
} else {
if (FT_Err_Ok != FT_New_Face(ft, aFontEntry->mFilename.get(),
aFontEntry->mFTFontIndex, &mFace)) {
mFace = Factory::NewFTFace(nullptr, aFontEntry->mFilename.get(), aFontEntry->mFTFontIndex);
if (!mFace) {
NS_WARNING("failed to create freetype face");
}
}
@@ -128,7 +125,7 @@ public:
~AutoFTFace() {
if (mFace && mOwnsFace) {
FT_Done_Face(mFace);
Factory::ReleaseFTFace(mFace);
if (mFontDataBuf) {
free(mFontDataBuf);
}
@@ -268,16 +265,13 @@ FT2FontEntry::CreateFontEntry(const nsAString& aFontName,
// Ownership of aFontData is passed in here; the fontEntry must
// retain it as long as the FT_Face needs it, and ensure it is
// eventually deleted.
FT_Face face;
FT_Error error =
FT_New_Memory_Face(gfxPlatform::GetPlatform()->GetFTLibrary(),
aFontData, aLength, 0, &face);
if (error != FT_Err_Ok) {
FT_Face face = Factory::NewFTFaceFromData(nullptr, aFontData, aLength, 0);
if (!face) {
free((void*)aFontData);
return nullptr;
}
if (FT_Err_Ok != FT_Select_Charmap(face, FT_ENCODING_UNICODE)) {
FT_Done_Face(face);
Factory::ReleaseFTFace(face);
free((void*)aFontData);
return nullptr;
}
@@ -304,7 +298,7 @@ public:
~FTUserFontData()
{
FT_Done_Face(mFace);
Factory::ReleaseFTFace(mFace);
if (mFontData) {
free((void*)mFontData);
}
@@ -988,22 +982,21 @@ gfxFT2FontList::AppendFacesFromFontFile(const nsCString& aFileName,
return;
}
FT_Library ftLibrary = gfxPlatform::GetPlatform()->GetFTLibrary();
FT_Face dummy;
if (FT_Err_Ok == FT_New_Face(ftLibrary, aFileName.get(), -1, &dummy)) {
FT_Face dummy = Factory::NewFTFace(nullptr, aFileName.get(), -1);
if (dummy) {
LOG(("reading font info via FreeType for %s", aFileName.get()));
nsCString newFaceList;
timestamp = s.st_mtime;
filesize = s.st_size;
for (FT_Long i = 0; i < dummy->num_faces; i++) {
FT_Face face;
if (FT_Err_Ok != FT_New_Face(ftLibrary, aFileName.get(), i, &face)) {
FT_Face face = Factory::NewFTFace(nullptr, aFileName.get(), i);
if (!face) {
continue;
}
AddFaceToList(aFileName, i, aStdFile, aVisibility, face, newFaceList);
FT_Done_Face(face);
Factory::ReleaseFTFace(face);
}
FT_Done_Face(dummy);
Factory::ReleaseFTFace(dummy);
if (aCache && 0 == statRetval && !newFaceList.IsEmpty()) {
aCache->CacheFileInfo(aFileName, newFaceList, timestamp, filesize);
}
@@ -1140,24 +1133,22 @@ gfxFT2FontList::AppendFacesFromOmnijarEntry(nsZipArchive* aArchive,
return;
}
FT_Library ftLibrary = gfxPlatform::GetPlatform()->GetFTLibrary();
FT_Face dummy;
if (FT_Err_Ok != FT_New_Memory_Face(ftLibrary, buf.get(), bufSize, 0, &dummy)) {
FT_Face dummy = Factory::NewFTFaceFromData(nullptr, buf.get(), bufSize, 0);
if (!dummy) {
return;
}
for (FT_Long i = 0; i < dummy->num_faces; i++) {
FT_Face face;
if (FT_Err_Ok != FT_New_Memory_Face(ftLibrary, buf.get(), bufSize, i, &face)) {
FT_Face face = Factory::NewFTFaceFromData(nullptr, buf.get(), bufSize, i);
if (!face) {
continue;
}
AddFaceToList(aEntryName, i, kStandard, FT2FontFamily::kVisible,
face, faceList);
FT_Done_Face(face);
Factory::ReleaseFTFace(face);
}
FT_Done_Face(dummy);
Factory::ReleaseFTFace(dummy);
if (aCache && !faceList.IsEmpty()) {
aCache->CacheFileInfo(aEntryName, faceList, 0, bufSize);
+21 -13
View File
@@ -448,7 +448,7 @@ gfxFontconfigFontEntry::MaybeReleaseFTFace()
// only close out FT_Face for system fonts, not for data fonts
if (!mIsDataUserFont) {
if (mFTFace) {
FT_Done_Face(mFTFace);
Factory::ReleaseFTFace(mFTFace);
mFTFace = nullptr;
}
mFTFaceInitialized = false;
@@ -652,6 +652,12 @@ PrepareFontOptions(FcPattern* aPattern,
cairo_font_options_set_antialias(aFontOptions, antialias);
}
static void
ReleaseFTUserFontData(void* aData)
{
static_cast<FTUserFontData*>(aData)->Release();
}
cairo_scaled_font_t*
gfxFontconfigFontEntry::CreateScaledFont(FcPattern* aRenderPattern,
gfxFloat aAdjustedSize,
@@ -681,10 +687,15 @@ gfxFontconfigFontEntry::CreateScaledFont(FcPattern* aRenderPattern,
// so that it gets deleted whenever cairo decides
NS_ASSERTION(mFTFace, "FT_Face is null when setting user data");
NS_ASSERTION(mUserFontData, "user font data is null when setting user data");
cairo_font_face_set_user_data(face,
&sFcFontlistUserFontDataKey,
new FTUserFontDataRef(mUserFontData),
FTUserFontDataRef::Destroy);
if (cairo_font_face_set_user_data(face,
&sFcFontlistUserFontDataKey,
mUserFontData,
ReleaseFTUserFontData) != CAIRO_STATUS_SUCCESS) {
NS_WARNING("Failed binding FTUserFontData to Cairo font face");
cairo_font_face_destroy(face);
return nullptr;
}
mUserFontData.get()->AddRef();
}
cairo_scaled_font_t *scaledFont = nullptr;
@@ -933,8 +944,8 @@ gfxFontconfigFontEntry::CopyFontTable(uint32_t aTableTag,
if (FcPatternGetInteger(mFontPattern, FC_INDEX, 0, &index) != FcResultMatch) {
index = 0; // default to 0 if not found in pattern
}
if (FT_New_Face(gfxFcPlatformFontList::GetFTLibrary(),
(const char*)filename, index, &mFTFace) != 0) {
mFTFace = Factory::NewFTFace(nullptr, ToCharPtr(filename), index);
if (!mFTFace) {
return NS_ERROR_FAILURE;
}
}
@@ -1433,16 +1444,13 @@ gfxFcPlatformFontList::MakePlatformFont(const nsAString& aFontName,
const uint8_t* aFontData,
uint32_t aLength)
{
FT_Face face;
FT_Error error =
FT_New_Memory_Face(gfxFcPlatformFontList::GetFTLibrary(),
aFontData, aLength, 0, &face);
if (error != FT_Err_Ok) {
FT_Face face = Factory::NewFTFaceFromData(nullptr, aFontData, aLength, 0);
if (!face) {
NS_Free((void*)aFontData);
return nullptr;
}
if (FT_Err_Ok != FT_Select_Charmap(face, FT_ENCODING_UNICODE)) {
FT_Done_Face(face);
Factory::ReleaseFTFace(face);
NS_Free((void*)aFontData);
return nullptr;
}
+2 -19
View File
@@ -46,7 +46,7 @@ public:
class FTUserFontData {
public:
NS_INLINE_DECL_REFCOUNTING(FTUserFontData)
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(FTUserFontData)
explicit FTUserFontData(FT_Face aFace, const uint8_t* aData)
: mFace(aFace), mFontData(aData)
@@ -58,7 +58,7 @@ public:
private:
~FTUserFontData()
{
FT_Done_Face(mFace);
mozilla::gfx::Factory::ReleaseFTFace(mFace);
if (mFontData) {
NS_Free((void*)mFontData);
}
@@ -68,23 +68,6 @@ private:
const uint8_t *mFontData;
};
class FTUserFontDataRef {
public:
explicit FTUserFontDataRef(FTUserFontData *aUserFontData)
: mUserFontData(aUserFontData)
{
}
static void Destroy(void* aData) {
FTUserFontDataRef* aUserFontDataRef =
static_cast<FTUserFontDataRef*>(aData);
delete aUserFontDataRef;
}
private:
RefPtr<FTUserFontData> mUserFontData;
};
// The names for the font entry and font classes should really
// the common 'Fc' abbreviation but the gfxPangoFontGroup code already
// defines versions of these, so use the verbose name for now.