1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/tests/gtest/gfxWordCacheTest.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,133 @@ 1.4 +/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "gtest/gtest.h" 1.10 + 1.11 +#include "nsCOMPtr.h" 1.12 +#include "nsTArray.h" 1.13 +#include "nsString.h" 1.14 +#include "nsDependentString.h" 1.15 + 1.16 +#include "prinrval.h" 1.17 + 1.18 +#include "gfxContext.h" 1.19 +#include "gfxFont.h" 1.20 +#include "gfxPlatform.h" 1.21 + 1.22 +#include "gfxFontTest.h" 1.23 +#include "mozilla/Attributes.h" 1.24 + 1.25 +class FrameTextRunCache; 1.26 + 1.27 +static FrameTextRunCache *gTextRuns = nullptr; 1.28 + 1.29 +/* 1.30 + * Cache textruns and expire them after 3*10 seconds of no use. 1.31 + */ 1.32 +class FrameTextRunCache MOZ_FINAL : public nsExpirationTracker<gfxTextRun,3> { 1.33 +public: 1.34 + enum { TIMEOUT_SECONDS = 10 }; 1.35 + FrameTextRunCache() 1.36 + : nsExpirationTracker<gfxTextRun,3>(TIMEOUT_SECONDS*1000) {} 1.37 + ~FrameTextRunCache() { 1.38 + AgeAllGenerations(); 1.39 + } 1.40 + 1.41 + void RemoveFromCache(gfxTextRun* aTextRun) { 1.42 + if (aTextRun->GetExpirationState()->IsTracked()) { 1.43 + RemoveObject(aTextRun); 1.44 + } 1.45 + } 1.46 + 1.47 + // This gets called when the timeout has expired on a gfxTextRun 1.48 + virtual void NotifyExpired(gfxTextRun* aTextRun) { 1.49 + RemoveFromCache(aTextRun); 1.50 + delete aTextRun; 1.51 + } 1.52 +}; 1.53 + 1.54 +static gfxTextRun * 1.55 +MakeTextRun(const char16_t *aText, uint32_t aLength, 1.56 + gfxFontGroup *aFontGroup, const gfxFontGroup::Parameters* aParams, 1.57 + uint32_t aFlags) 1.58 +{ 1.59 + nsAutoPtr<gfxTextRun> textRun; 1.60 + if (aLength == 0) { 1.61 + abort(); 1.62 + //textRun = aFontGroup->MakeEmptyTextRun(aParams, aFlags); 1.63 + } else if (aLength == 1 && aText[0] == ' ') { 1.64 + abort(); 1.65 + //textRun = aFontGroup->MakeSpaceTextRun(aParams, aFlags); 1.66 + } else { 1.67 + textRun = aFontGroup->MakeTextRun(aText, aLength, aParams, aFlags); 1.68 + } 1.69 + if (!textRun) 1.70 + return nullptr; 1.71 + nsresult rv = gTextRuns->AddObject(textRun); 1.72 + if (NS_FAILED(rv)) { 1.73 + gTextRuns->RemoveFromCache(textRun); 1.74 + return nullptr; 1.75 + } 1.76 + return textRun.forget(); 1.77 +} 1.78 + 1.79 +static already_AddRefed<gfxContext> 1.80 +MakeContext () 1.81 +{ 1.82 + const int size = 200; 1.83 + 1.84 + nsRefPtr<gfxASurface> surface; 1.85 + 1.86 + surface = gfxPlatform::GetPlatform()-> 1.87 + CreateOffscreenSurface(IntSize(size, size), 1.88 + gfxASurface::ContentFromFormat(gfxImageFormat::RGB24)); 1.89 + nsRefPtr<gfxContext> ctx = new gfxContext(surface); 1.90 + return ctx.forget(); 1.91 +} 1.92 + 1.93 +TEST(Gfx, WordCache) { 1.94 + gTextRuns = new FrameTextRunCache(); 1.95 + 1.96 + nsRefPtr<gfxContext> ctx = MakeContext(); 1.97 + { 1.98 + gfxFontStyle style (mozilla::gfx::FontStyle::NORMAL, 1.99 + 139, 1.100 + 10.0, 1.101 + 0, 1.102 + NS_NewPermanentAtom(NS_LITERAL_STRING("en")), 1.103 + 0.0, 1.104 + false, false, 1.105 + NS_LITERAL_STRING("")); 1.106 + 1.107 + nsRefPtr<gfxFontGroup> fontGroup = 1.108 + gfxPlatform::GetPlatform()->CreateFontGroup(NS_LITERAL_STRING("Geneva, MS Sans Serif, Helvetica,serif"), &style, nullptr); 1.109 + 1.110 + gfxTextRunFactory::Parameters params = { 1.111 + ctx, nullptr, nullptr, nullptr, 0, 60 1.112 + }; 1.113 + 1.114 + uint32_t flags = gfxTextRunFactory::TEXT_IS_PERSISTENT; 1.115 + 1.116 + // First load an Arabic word into the cache 1.117 + const char cString[] = "\xd8\xaa\xd9\x85"; 1.118 + nsDependentCString cStr(cString); 1.119 + NS_ConvertUTF8toUTF16 str(cStr); 1.120 + gfxTextRun *tr = MakeTextRun(str.get(), str.Length(), fontGroup, ¶ms, flags); 1.121 + tr->GetAdvanceWidth(0, str.Length(), nullptr); 1.122 + 1.123 + // Now try to trigger an assertion with a word cache bug. The first 1.124 + // word is in the cache so it gets added to the new textrun directly. 1.125 + // The second word is not in the cache 1.126 + const char cString2[] = "\xd8\xaa\xd9\x85\n\xd8\xaa\xd8\x85 "; 1.127 + nsDependentCString cStr2(cString2); 1.128 + NS_ConvertUTF8toUTF16 str2(cStr2); 1.129 + gfxTextRun *tr2 = MakeTextRun(str2.get(), str2.Length(), fontGroup, ¶ms, flags); 1.130 + tr2->GetAdvanceWidth(0, str2.Length(), nullptr); 1.131 + } 1.132 + 1.133 + delete gTextRuns; 1.134 + gTextRuns = nullptr; 1.135 + 1.136 +}