2007-06-20 23:49:33 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2012-05-21 15:12:37 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* 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/. */
|
2007-06-20 23:49:33 +04:00
|
|
|
|
2013-04-26 03:02:13 +04:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
2007-06-20 23:49:33 +04:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsDependentString.h"
|
|
|
|
|
|
|
|
#include "prinrval.h"
|
|
|
|
|
|
|
|
#include "gfxContext.h"
|
|
|
|
#include "gfxFont.h"
|
|
|
|
#include "gfxPlatform.h"
|
|
|
|
|
|
|
|
#include "gfxFontTest.h"
|
2012-06-19 07:26:34 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2007-06-20 23:49:33 +04:00
|
|
|
|
|
|
|
class FrameTextRunCache;
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
static FrameTextRunCache *gTextRuns = nullptr;
|
2007-06-20 23:49:33 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Cache textruns and expire them after 3*10 seconds of no use.
|
|
|
|
*/
|
2012-06-19 07:26:34 +04:00
|
|
|
class FrameTextRunCache MOZ_FINAL : public nsExpirationTracker<gfxTextRun,3> {
|
2007-06-20 23:49:33 +04:00
|
|
|
public:
|
|
|
|
enum { TIMEOUT_SECONDS = 10 };
|
|
|
|
FrameTextRunCache()
|
|
|
|
: nsExpirationTracker<gfxTextRun,3>(TIMEOUT_SECONDS*1000) {}
|
|
|
|
~FrameTextRunCache() {
|
|
|
|
AgeAllGenerations();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoveFromCache(gfxTextRun* aTextRun) {
|
|
|
|
if (aTextRun->GetExpirationState()->IsTracked()) {
|
|
|
|
RemoveObject(aTextRun);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This gets called when the timeout has expired on a gfxTextRun
|
|
|
|
virtual void NotifyExpired(gfxTextRun* aTextRun) {
|
|
|
|
RemoveFromCache(aTextRun);
|
|
|
|
delete aTextRun;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static gfxTextRun *
|
2014-01-04 19:02:17 +04:00
|
|
|
MakeTextRun(const char16_t *aText, uint32_t aLength,
|
2007-06-20 23:49:33 +04:00
|
|
|
gfxFontGroup *aFontGroup, const gfxFontGroup::Parameters* aParams,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aFlags)
|
2007-06-20 23:49:33 +04:00
|
|
|
{
|
|
|
|
nsAutoPtr<gfxTextRun> textRun;
|
|
|
|
if (aLength == 0) {
|
2013-04-26 03:02:13 +04:00
|
|
|
abort();
|
|
|
|
//textRun = aFontGroup->MakeEmptyTextRun(aParams, aFlags);
|
2007-06-20 23:49:33 +04:00
|
|
|
} else if (aLength == 1 && aText[0] == ' ') {
|
2013-04-26 03:02:13 +04:00
|
|
|
abort();
|
|
|
|
//textRun = aFontGroup->MakeSpaceTextRun(aParams, aFlags);
|
2007-06-20 23:49:33 +04:00
|
|
|
} else {
|
2011-12-06 16:39:18 +04:00
|
|
|
textRun = aFontGroup->MakeTextRun(aText, aLength, aParams, aFlags);
|
2007-06-20 23:49:33 +04:00
|
|
|
}
|
|
|
|
if (!textRun)
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2007-06-20 23:49:33 +04:00
|
|
|
nsresult rv = gTextRuns->AddObject(textRun);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
gTextRuns->RemoveFromCache(textRun);
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2007-06-20 23:49:33 +04:00
|
|
|
}
|
|
|
|
return textRun.forget();
|
|
|
|
}
|
|
|
|
|
2013-04-26 03:02:13 +04:00
|
|
|
static already_AddRefed<gfxContext>
|
2007-06-20 23:49:33 +04:00
|
|
|
MakeContext ()
|
|
|
|
{
|
|
|
|
const int size = 200;
|
|
|
|
|
|
|
|
nsRefPtr<gfxASurface> surface;
|
|
|
|
|
2010-09-17 01:34:53 +04:00
|
|
|
surface = gfxPlatform::GetPlatform()->
|
|
|
|
CreateOffscreenSurface(gfxIntSize(size, size),
|
2013-09-25 00:45:13 +04:00
|
|
|
gfxASurface::ContentFromFormat(gfxImageFormatRGB24));
|
2013-04-26 03:02:13 +04:00
|
|
|
nsRefPtr<gfxContext> ctx = new gfxContext(surface);
|
|
|
|
return ctx.forget();
|
2007-06-20 23:49:33 +04:00
|
|
|
}
|
|
|
|
|
2013-04-26 03:02:13 +04:00
|
|
|
TEST(Gfx, WordCache) {
|
2007-06-20 23:49:33 +04:00
|
|
|
gTextRuns = new FrameTextRunCache();
|
|
|
|
|
|
|
|
nsRefPtr<gfxContext> ctx = MakeContext();
|
|
|
|
{
|
2014-01-10 23:06:16 +04:00
|
|
|
gfxFontStyle style (mozilla::gfx::FontStyle::NORMAL,
|
2007-06-20 23:49:33 +04:00
|
|
|
139,
|
|
|
|
10.0,
|
2013-04-26 03:02:13 +04:00
|
|
|
0,
|
2010-03-08 18:45:00 +03:00
|
|
|
NS_NewPermanentAtom(NS_LITERAL_STRING("en")),
|
2007-06-20 23:49:33 +04:00
|
|
|
0.0,
|
2013-04-26 03:02:13 +04:00
|
|
|
false, false,
|
2010-07-14 00:31:31 +04:00
|
|
|
NS_LITERAL_STRING(""));
|
2007-06-20 23:49:33 +04:00
|
|
|
|
|
|
|
nsRefPtr<gfxFontGroup> fontGroup =
|
2012-07-30 18:20:58 +04:00
|
|
|
gfxPlatform::GetPlatform()->CreateFontGroup(NS_LITERAL_STRING("Geneva, MS Sans Serif, Helvetica,serif"), &style, nullptr);
|
2007-06-20 23:49:33 +04:00
|
|
|
|
|
|
|
gfxTextRunFactory::Parameters params = {
|
2012-07-30 18:20:58 +04:00
|
|
|
ctx, nullptr, nullptr, nullptr, 0, 60
|
2007-06-20 23:49:33 +04:00
|
|
|
};
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t flags = gfxTextRunFactory::TEXT_IS_PERSISTENT;
|
2007-06-20 23:49:33 +04:00
|
|
|
|
|
|
|
// First load an Arabic word into the cache
|
|
|
|
const char cString[] = "\xd8\xaa\xd9\x85";
|
|
|
|
nsDependentCString cStr(cString);
|
|
|
|
NS_ConvertUTF8toUTF16 str(cStr);
|
|
|
|
gfxTextRun *tr = MakeTextRun(str.get(), str.Length(), fontGroup, ¶ms, flags);
|
2012-07-30 18:20:58 +04:00
|
|
|
tr->GetAdvanceWidth(0, str.Length(), nullptr);
|
2007-06-20 23:49:33 +04:00
|
|
|
|
|
|
|
// Now try to trigger an assertion with a word cache bug. The first
|
|
|
|
// word is in the cache so it gets added to the new textrun directly.
|
|
|
|
// The second word is not in the cache
|
|
|
|
const char cString2[] = "\xd8\xaa\xd9\x85\n\xd8\xaa\xd8\x85 ";
|
|
|
|
nsDependentCString cStr2(cString2);
|
|
|
|
NS_ConvertUTF8toUTF16 str2(cStr2);
|
|
|
|
gfxTextRun *tr2 = MakeTextRun(str2.get(), str2.Length(), fontGroup, ¶ms, flags);
|
2012-07-30 18:20:58 +04:00
|
|
|
tr2->GetAdvanceWidth(0, str2.Length(), nullptr);
|
2007-06-20 23:49:33 +04:00
|
|
|
}
|
|
|
|
|
2013-04-26 03:02:13 +04:00
|
|
|
delete gTextRuns;
|
|
|
|
gTextRuns = nullptr;
|
|
|
|
|
2007-06-20 23:49:33 +04:00
|
|
|
}
|