2013-01-18 04:45:11 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* vim: set ts=8 sts=4 et sw=4 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-21 15:12:37 +04:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2010-05-11 21:27:36 +04:00
|
|
|
|
2012-08-23 14:23:43 +04:00
|
|
|
#include "base/basictypes.h"
|
|
|
|
|
2010-05-11 21:27:36 +04:00
|
|
|
#include "gfxAndroidPlatform.h"
|
2011-11-02 23:55:03 +04:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2014-08-25 23:34:00 +04:00
|
|
|
#include "mozilla/CountingAllocatorBase.h"
|
2013-01-17 20:52:41 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2010-05-11 21:27:36 +04:00
|
|
|
|
2014-02-09 12:04:38 +04:00
|
|
|
#include "gfx2DGlue.h"
|
2011-09-23 15:15:36 +04:00
|
|
|
#include "gfxFT2FontList.h"
|
2010-05-11 21:27:36 +04:00
|
|
|
#include "gfxImageSurface.h"
|
2014-09-16 13:58:12 +04:00
|
|
|
#include "gfxTextRun.h"
|
2012-08-23 14:23:43 +04:00
|
|
|
#include "mozilla/dom/ContentChild.h"
|
2012-02-09 02:52:57 +04:00
|
|
|
#include "nsXULAppAPI.h"
|
2012-03-14 00:59:26 +04:00
|
|
|
#include "nsIScreen.h"
|
|
|
|
#include "nsIScreenManager.h"
|
2013-04-07 01:04:30 +04:00
|
|
|
#include "nsILocaleService.h"
|
2013-10-08 03:15:59 +04:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2014-03-04 02:11:06 +04:00
|
|
|
#include "gfxPrefs.h"
|
2011-09-23 15:15:36 +04:00
|
|
|
#include "cairo.h"
|
2014-12-18 19:30:06 +03:00
|
|
|
#include "VsyncSource.h"
|
2010-05-11 21:27:36 +04:00
|
|
|
|
|
|
|
#include "ft2build.h"
|
|
|
|
#include FT_FREETYPE_H
|
2012-11-20 07:00:19 +04:00
|
|
|
#include FT_MODULE_H
|
|
|
|
|
2016-12-20 10:24:49 +03:00
|
|
|
#include "GeneratedJNINatives.h"
|
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
using namespace mozilla;
|
2012-08-23 14:23:43 +04:00
|
|
|
using namespace mozilla::dom;
|
2011-11-02 23:55:03 +04:00
|
|
|
using namespace mozilla::gfx;
|
2010-05-11 21:27:36 +04:00
|
|
|
|
2013-07-31 19:44:31 +04:00
|
|
|
static FT_Library gPlatformFTLibrary = nullptr;
|
2010-05-11 21:27:36 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class FreetypeReporter final : public nsIMemoryReporter,
|
2015-03-27 21:52:19 +03:00
|
|
|
public CountingAllocatorBase<FreetypeReporter>
|
2012-11-20 07:00:19 +04:00
|
|
|
{
|
2014-08-26 23:07:59 +04:00
|
|
|
private:
|
|
|
|
~FreetypeReporter() {}
|
|
|
|
|
2013-01-18 04:45:11 +04:00
|
|
|
public:
|
2013-12-08 10:09:10 +04:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2014-04-08 21:51:20 +04:00
|
|
|
static void* Malloc(FT_Memory, long size)
|
2013-01-18 04:45:11 +04:00
|
|
|
{
|
2014-04-08 21:51:20 +04:00
|
|
|
return CountingMalloc(size);
|
2013-01-18 04:45:11 +04:00
|
|
|
}
|
2012-11-20 07:00:19 +04:00
|
|
|
|
2014-04-08 21:51:20 +04:00
|
|
|
static void Free(FT_Memory, void* p)
|
2013-01-18 04:45:11 +04:00
|
|
|
{
|
2014-04-08 21:51:20 +04:00
|
|
|
return CountingFree(p);
|
2013-01-18 04:45:11 +04:00
|
|
|
}
|
2012-11-20 07:00:19 +04:00
|
|
|
|
2013-01-18 04:45:11 +04:00
|
|
|
static void*
|
2014-04-08 21:51:20 +04:00
|
|
|
Realloc(FT_Memory, long cur_size, long new_size, void* p)
|
2013-01-18 04:45:11 +04:00
|
|
|
{
|
2014-04-08 21:51:20 +04:00
|
|
|
return CountingRealloc(p, new_size);
|
2013-01-18 04:45:11 +04:00
|
|
|
}
|
2012-11-20 07:00:19 +04:00
|
|
|
|
2013-12-08 10:09:10 +04:00
|
|
|
NS_IMETHOD CollectReports(nsIHandleReportCallback* aHandleReport,
|
2016-06-04 01:31:05 +03:00
|
|
|
nsISupports* aData, bool aAnonymize) override
|
2013-12-08 10:09:10 +04:00
|
|
|
{
|
2016-08-24 08:23:45 +03:00
|
|
|
MOZ_COLLECT_REPORT(
|
2014-04-08 21:51:20 +04:00
|
|
|
"explicit/freetype", KIND_HEAP, UNITS_BYTES, MemoryAllocated(),
|
2013-12-08 10:09:10 +04:00
|
|
|
"Memory used by Freetype.");
|
2016-08-24 08:23:45 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
2013-12-08 10:09:10 +04:00
|
|
|
}
|
2013-01-18 04:45:11 +04:00
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(FreetypeReporter, nsIMemoryReporter)
|
2013-12-08 10:09:10 +04:00
|
|
|
|
2014-04-08 21:51:20 +04:00
|
|
|
template<> Atomic<size_t> CountingAllocatorBase<FreetypeReporter>::sAmount(0);
|
2013-01-18 04:45:11 +04:00
|
|
|
|
|
|
|
static FT_MemoryRec_ sFreetypeMemoryRecord;
|
2012-11-20 07:00:19 +04:00
|
|
|
|
2010-05-11 21:27:36 +04:00
|
|
|
gfxAndroidPlatform::gfxAndroidPlatform()
|
|
|
|
{
|
2012-11-20 07:00:19 +04:00
|
|
|
// A custom allocator. It counts allocations, enabling memory reporting.
|
|
|
|
sFreetypeMemoryRecord.user = nullptr;
|
2014-04-08 21:51:20 +04:00
|
|
|
sFreetypeMemoryRecord.alloc = FreetypeReporter::Malloc;
|
|
|
|
sFreetypeMemoryRecord.free = FreetypeReporter::Free;
|
|
|
|
sFreetypeMemoryRecord.realloc = FreetypeReporter::Realloc;
|
2012-11-20 07:00:19 +04:00
|
|
|
|
|
|
|
// These two calls are equivalent to FT_Init_FreeType(), but allow us to
|
|
|
|
// provide a custom memory allocator.
|
|
|
|
FT_New_Library(&sFreetypeMemoryRecord, &gPlatformFTLibrary);
|
|
|
|
FT_Add_Default_Modules(gPlatformFTLibrary);
|
|
|
|
|
2013-11-07 09:35:30 +04:00
|
|
|
RegisterStrongMemoryReporter(new FreetypeReporter());
|
2012-03-14 00:59:26 +04:00
|
|
|
|
2015-08-27 01:37:48 +03:00
|
|
|
mOffscreenFormat = GetScreenDepth() == 16
|
2016-01-08 07:57:38 +03:00
|
|
|
? SurfaceFormat::R5G6B5_UINT16
|
|
|
|
: SurfaceFormat::X8R8G8B8_UINT32;
|
2013-01-17 20:52:41 +04:00
|
|
|
|
2014-03-04 02:11:06 +04:00
|
|
|
if (gfxPrefs::AndroidRGB16Force()) {
|
2016-01-08 07:57:38 +03:00
|
|
|
mOffscreenFormat = SurfaceFormat::R5G6B5_UINT16;
|
2013-01-17 20:52:41 +04:00
|
|
|
}
|
2010-05-11 21:27:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxAndroidPlatform::~gfxAndroidPlatform()
|
|
|
|
{
|
2012-11-20 07:00:19 +04:00
|
|
|
FT_Done_Library(gPlatformFTLibrary);
|
2013-07-31 19:44:31 +04:00
|
|
|
gPlatformFTLibrary = nullptr;
|
2010-05-11 21:27:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<gfxASurface>
|
2015-07-07 04:21:47 +03:00
|
|
|
gfxAndroidPlatform::CreateOffscreenSurface(const IntSize& aSize,
|
|
|
|
gfxImageFormat aFormat)
|
2010-05-11 21:27:36 +04:00
|
|
|
{
|
2016-09-15 17:36:21 +03:00
|
|
|
if (!Factory::AllowedSurfaceSize(aSize)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<gfxASurface> newSurface;
|
2015-07-07 04:21:47 +03:00
|
|
|
newSurface = new gfxImageSurface(aSize, aFormat);
|
2010-05-11 21:27:36 +04:00
|
|
|
|
|
|
|
return newSurface.forget();
|
|
|
|
}
|
|
|
|
|
2013-04-07 01:04:30 +04:00
|
|
|
static bool
|
|
|
|
IsJapaneseLocale()
|
|
|
|
{
|
|
|
|
static bool sInitialized = false;
|
|
|
|
static bool sIsJapanese = false;
|
|
|
|
|
|
|
|
if (!sInitialized) {
|
|
|
|
sInitialized = true;
|
|
|
|
|
|
|
|
do { // to allow 'break' to abandon this block if a call fails
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsILocaleService> ls =
|
|
|
|
do_GetService(NS_LOCALESERVICE_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsILocale> appLocale;
|
|
|
|
rv = ls->GetApplicationLocale(getter_AddRefs(appLocale));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
nsString localeStr;
|
|
|
|
rv = appLocale->
|
|
|
|
GetCategory(NS_LITERAL_STRING(NSILOCALE_MESSAGE), localeStr);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const nsAString& lang = nsDependentSubstring(localeStr, 0, 2);
|
|
|
|
if (lang.EqualsLiteral("ja")) {
|
|
|
|
sIsJapanese = true;
|
|
|
|
}
|
|
|
|
} while (false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sIsJapanese;
|
|
|
|
}
|
|
|
|
|
2013-03-04 21:03:16 +04:00
|
|
|
void
|
2014-09-30 10:27:55 +04:00
|
|
|
gfxAndroidPlatform::GetCommonFallbackFonts(uint32_t aCh, uint32_t aNextCh,
|
2016-04-21 20:58:59 +03:00
|
|
|
Script aRunScript,
|
2013-03-04 21:03:16 +04:00
|
|
|
nsTArray<const char*>& aFontList)
|
|
|
|
{
|
|
|
|
static const char kDroidSansJapanese[] = "Droid Sans Japanese";
|
2013-08-30 14:01:43 +04:00
|
|
|
static const char kMotoyaLMaru[] = "MotoyaLMaru";
|
2016-09-05 06:01:01 +03:00
|
|
|
static const char kNotoSansCJKJP[] = "Noto Sans CJK JP";
|
2015-08-21 11:55:52 +03:00
|
|
|
static const char kNotoColorEmoji[] = "Noto Color Emoji";
|
2013-03-04 21:03:16 +04:00
|
|
|
|
2014-09-30 10:27:55 +04:00
|
|
|
if (aNextCh == 0xfe0fu) {
|
|
|
|
// if char is followed by VS16, try for a color emoji glyph
|
2015-08-21 11:55:52 +03:00
|
|
|
aFontList.AppendElement(kNotoColorEmoji);
|
2014-09-30 10:27:55 +04:00
|
|
|
}
|
|
|
|
|
2015-08-21 11:55:52 +03:00
|
|
|
if (!IS_IN_BMP(aCh)) {
|
|
|
|
uint32_t p = aCh >> 16;
|
|
|
|
if (p == 1) { // try color emoji font, unless VS15 (text style) present
|
|
|
|
if (aNextCh != 0xfe0fu && aNextCh != 0xfe0eu) {
|
|
|
|
aFontList.AppendElement(kNotoColorEmoji);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2014-06-02 11:46:54 +04:00
|
|
|
// try language-specific "Droid Sans *" and "Noto Sans *" fonts for
|
|
|
|
// certain blocks, as most devices probably have these
|
2013-03-04 21:03:16 +04:00
|
|
|
uint8_t block = (aCh >> 8) & 0xff;
|
|
|
|
switch (block) {
|
|
|
|
case 0x05:
|
|
|
|
aFontList.AppendElement("Droid Sans Hebrew");
|
|
|
|
aFontList.AppendElement("Droid Sans Armenian");
|
|
|
|
break;
|
|
|
|
case 0x06:
|
|
|
|
aFontList.AppendElement("Droid Sans Arabic");
|
|
|
|
break;
|
|
|
|
case 0x09:
|
2014-06-02 11:46:54 +04:00
|
|
|
aFontList.AppendElement("Noto Sans Devanagari");
|
2013-03-04 21:03:16 +04:00
|
|
|
aFontList.AppendElement("Droid Sans Devanagari");
|
|
|
|
break;
|
|
|
|
case 0x0b:
|
2014-06-02 11:46:54 +04:00
|
|
|
aFontList.AppendElement("Noto Sans Tamil");
|
2013-03-04 21:03:16 +04:00
|
|
|
aFontList.AppendElement("Droid Sans Tamil");
|
|
|
|
break;
|
|
|
|
case 0x0e:
|
2014-06-02 11:46:54 +04:00
|
|
|
aFontList.AppendElement("Noto Sans Thai");
|
2013-03-04 21:03:16 +04:00
|
|
|
aFontList.AppendElement("Droid Sans Thai");
|
|
|
|
break;
|
|
|
|
case 0x10: case 0x2d:
|
|
|
|
aFontList.AppendElement("Droid Sans Georgian");
|
|
|
|
break;
|
|
|
|
case 0x12: case 0x13:
|
|
|
|
aFontList.AppendElement("Droid Sans Ethiopic");
|
|
|
|
break;
|
|
|
|
case 0xf9: case 0xfa:
|
2013-04-07 01:04:30 +04:00
|
|
|
if (IsJapaneseLocale()) {
|
2013-08-30 14:01:43 +04:00
|
|
|
aFontList.AppendElement(kMotoyaLMaru);
|
2016-09-05 06:01:01 +03:00
|
|
|
aFontList.AppendElement(kNotoSansCJKJP);
|
2013-04-07 01:04:30 +04:00
|
|
|
aFontList.AppendElement(kDroidSansJapanese);
|
|
|
|
}
|
2013-03-04 21:03:16 +04:00
|
|
|
break;
|
|
|
|
default:
|
2013-04-07 01:04:30 +04:00
|
|
|
if (block >= 0x2e && block <= 0x9f && IsJapaneseLocale()) {
|
2013-08-30 14:01:43 +04:00
|
|
|
aFontList.AppendElement(kMotoyaLMaru);
|
2016-09-05 06:01:01 +03:00
|
|
|
aFontList.AppendElement(kNotoSansCJKJP);
|
2013-03-04 21:03:16 +04:00
|
|
|
aFontList.AppendElement(kDroidSansJapanese);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// and try Droid Sans Fallback as a last resort
|
|
|
|
aFontList.AppendElement("Droid Sans Fallback");
|
|
|
|
}
|
|
|
|
|
2011-01-13 07:04:42 +03:00
|
|
|
void
|
2015-05-13 08:11:25 +03:00
|
|
|
gfxAndroidPlatform::GetSystemFontList(InfallibleTArray<FontListEntry>* retValue)
|
2010-05-11 21:27:36 +04:00
|
|
|
{
|
2015-05-13 08:11:25 +03:00
|
|
|
gfxFT2FontList::PlatformFontList()->GetSystemFontList(retValue);
|
2011-01-13 07:04:42 +03:00
|
|
|
}
|
|
|
|
|
2010-07-16 10:03:45 +04:00
|
|
|
gfxPlatformFontList*
|
|
|
|
gfxAndroidPlatform::CreatePlatformFontList()
|
|
|
|
{
|
2010-11-08 14:02:27 +03:00
|
|
|
gfxPlatformFontList* list = new gfxFT2FontList();
|
|
|
|
if (NS_SUCCEEDED(list->InitFontList())) {
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
gfxPlatformFontList::Shutdown();
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2010-07-16 10:03:45 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2012-08-22 19:56:38 +04:00
|
|
|
gfxAndroidPlatform::IsFontFormatSupported(nsIURI *aFontURI, uint32_t aFormatFlags)
|
2010-07-16 10:03:45 +04:00
|
|
|
{
|
|
|
|
// check for strange format flags
|
|
|
|
NS_ASSERTION(!(aFormatFlags & gfxUserFontSet::FLAG_FORMAT_NOT_USED),
|
|
|
|
"strange font format hint set");
|
|
|
|
|
|
|
|
// accept supported formats
|
2014-10-04 14:36:05 +04:00
|
|
|
if (aFormatFlags & gfxUserFontSet::FLAG_FORMATS_COMMON) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-07-16 10:03:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// reject all other formats, known and unknown
|
|
|
|
if (aFormatFlags != 0) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-07-16 10:03:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// no format hint set, need to look at data
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-07-16 10:03:45 +04:00
|
|
|
}
|
|
|
|
|
2010-05-11 21:27:36 +04:00
|
|
|
gfxFontGroup *
|
2014-06-06 10:09:23 +04:00
|
|
|
gfxAndroidPlatform::CreateFontGroup(const FontFamilyList& aFontFamilyList,
|
2015-09-11 07:24:33 +03:00
|
|
|
const gfxFontStyle* aStyle,
|
|
|
|
gfxTextPerfMetrics* aTextPerf,
|
2015-11-20 16:01:12 +03:00
|
|
|
gfxUserFontSet* aUserFontSet,
|
|
|
|
gfxFloat aDevToCssSize)
|
2010-05-11 21:27:36 +04:00
|
|
|
{
|
2015-11-20 16:01:12 +03:00
|
|
|
return new gfxFontGroup(aFontFamilyList, aStyle, aTextPerf,
|
|
|
|
aUserFontSet, aDevToCssSize);
|
2010-05-11 21:27:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
FT_Library
|
|
|
|
gfxAndroidPlatform::GetFTLibrary()
|
|
|
|
{
|
|
|
|
return gPlatformFTLibrary;
|
|
|
|
}
|
|
|
|
|
2015-06-17 17:00:52 +03:00
|
|
|
already_AddRefed<ScaledFont>
|
2012-07-24 14:18:37 +04:00
|
|
|
gfxAndroidPlatform::GetScaledFontForFont(DrawTarget* aTarget, gfxFont *aFont)
|
2011-11-02 23:55:03 +04:00
|
|
|
{
|
2014-01-30 11:01:16 +04:00
|
|
|
return GetScaledFontForFontWithCairoSkia(aTarget, aFont);
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
|
|
|
|
2012-02-09 02:52:57 +04:00
|
|
|
bool
|
|
|
|
gfxAndroidPlatform::FontHintingEnabled()
|
|
|
|
{
|
|
|
|
// In "mobile" builds, we sometimes use non-reflow-zoom, so we
|
|
|
|
// might not want hinting. Let's see.
|
2013-01-15 19:19:28 +04:00
|
|
|
|
2016-09-08 17:03:34 +03:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
|
|
|
// On Android, we currently only use gecko to render web
|
2012-02-09 02:52:57 +04:00
|
|
|
// content that can always be be non-reflow-zoomed. So turn off
|
|
|
|
// hinting.
|
2016-12-20 10:24:49 +03:00
|
|
|
//
|
2013-01-15 19:19:28 +04:00
|
|
|
// XXX when gecko-android-java is used as an "app runtime", we may
|
|
|
|
// want to re-enable hinting for non-browser processes there.
|
|
|
|
return false;
|
2016-09-08 17:03:34 +03:00
|
|
|
#endif // MOZ_WIDGET_ANDROID
|
2013-01-15 19:19:28 +04:00
|
|
|
|
|
|
|
// Currently, we don't have any other targets, but if/when we do,
|
|
|
|
// decide how to handle them here.
|
|
|
|
|
|
|
|
NS_NOTREACHED("oops, what platform is this?");
|
|
|
|
return gfxPlatform::FontHintingEnabled();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
gfxAndroidPlatform::RequiresLinearZoom()
|
|
|
|
{
|
2016-09-08 17:03:34 +03:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
|
|
|
// On Android, we currently only use gecko to render web
|
2013-01-15 19:19:28 +04:00
|
|
|
// content that can always be be non-reflow-zoomed.
|
|
|
|
//
|
|
|
|
// XXX when gecko-android-java is used as an "app runtime", we may
|
2016-11-16 09:01:30 +03:00
|
|
|
// want to use linear zoom only for the web browser process, not other apps.
|
2013-01-15 19:19:28 +04:00
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
NS_NOTREACHED("oops, what platform is this?");
|
|
|
|
return gfxPlatform::RequiresLinearZoom();
|
2012-02-09 02:52:57 +04:00
|
|
|
}
|
2012-05-15 23:41:20 +04:00
|
|
|
|
2016-12-20 10:24:49 +03:00
|
|
|
class AndroidVsyncSource final : public VsyncSource {
|
|
|
|
public:
|
|
|
|
class JavaVsyncSupport final : public java::VsyncSource::Natives<JavaVsyncSupport>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using Base = java::VsyncSource::Natives<JavaVsyncSupport>;
|
|
|
|
using Base::DisposeNative;
|
|
|
|
|
|
|
|
static void NotifyVsync() {
|
|
|
|
GetDisplayInstance().NotifyVsync(TimeStamp::Now());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class Display final : public VsyncSource::Display {
|
|
|
|
public:
|
|
|
|
Display()
|
|
|
|
: mJavaVsync(java::VsyncSource::GetInstance())
|
|
|
|
, mObservingVsync(false)
|
|
|
|
{
|
|
|
|
JavaVsyncSupport::Init(); // To register native methods.
|
|
|
|
}
|
|
|
|
|
|
|
|
~Display() { DisableVsync(); }
|
|
|
|
|
|
|
|
bool IsVsyncEnabled() override
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mJavaVsync);
|
|
|
|
|
|
|
|
return mObservingVsync;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EnableVsync() override
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mJavaVsync);
|
|
|
|
|
|
|
|
if (mObservingVsync) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
bool ok = mJavaVsync->ObserveVsync(true);
|
|
|
|
if (ok && !mVsyncDuration) {
|
|
|
|
float fps = mJavaVsync->GetRefreshRate();
|
|
|
|
mVsyncDuration = TimeDuration::FromMilliseconds(1000.0 / fps);
|
|
|
|
}
|
|
|
|
mObservingVsync = ok;
|
|
|
|
MOZ_ASSERT(mObservingVsync);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DisableVsync() override
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mJavaVsync);
|
|
|
|
|
|
|
|
if (!mObservingVsync) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mObservingVsync = mJavaVsync->ObserveVsync(false);
|
|
|
|
MOZ_ASSERT(!mObservingVsync);
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeDuration GetVsyncRate() override { return mVsyncDuration; }
|
|
|
|
|
|
|
|
void Shutdown() override {
|
|
|
|
DisableVsync();
|
|
|
|
mJavaVsync = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
java::VsyncSource::GlobalRef mJavaVsync;
|
|
|
|
bool mObservingVsync;
|
|
|
|
TimeDuration mVsyncDuration;
|
|
|
|
};
|
|
|
|
|
|
|
|
Display& GetGlobalDisplay() final { return GetDisplayInstance(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual ~AndroidVsyncSource() {}
|
|
|
|
|
|
|
|
static Display& GetDisplayInstance()
|
|
|
|
{
|
|
|
|
static Display globalDisplay;
|
|
|
|
return globalDisplay;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-12-18 19:30:06 +03:00
|
|
|
already_AddRefed<mozilla::gfx::VsyncSource>
|
|
|
|
gfxAndroidPlatform::CreateHardwareVsyncSource()
|
|
|
|
{
|
2016-12-21 14:21:24 +03:00
|
|
|
if (jni::IsAvailable() && java::VsyncSource::IsVsyncSupported()) {
|
2016-12-20 10:24:49 +03:00
|
|
|
RefPtr<AndroidVsyncSource> vsyncSource = new AndroidVsyncSource();
|
|
|
|
return vsyncSource.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_WARNING("Vsync not supported. Falling back to software vsync");
|
2015-03-20 18:12:53 +03:00
|
|
|
return gfxPlatform::CreateHardwareVsyncSource();
|
2014-12-18 19:30:06 +03:00
|
|
|
}
|