Merge mozilla-central and tracemonkey. (a=blockers)
[mozilla-central.git] / gfx / tests / gfxWordCacheTest.cpp
blob5eb0dbd8dacd487dbe7b51e5981c7fa335b3ab35
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is Mozilla Corporation code.
17 * The Initial Developer of the Original Code is Mozilla Foundation.
18 * Portions created by the Initial Developer are Copyright (C) 2007
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
22 * Vladimir Vukicevic <vladimir@pobox.com>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "nsCOMPtr.h"
39 #include "nsTArray.h"
40 #include "nsString.h"
41 #include "nsDependentString.h"
43 #include "prinrval.h"
45 #include "nsServiceManagerUtils.h"
46 #include "nsIPrefService.h"
47 #include "nsIPrefBranch.h"
49 #include "gfxContext.h"
50 #include "gfxFont.h"
51 #include "gfxPlatform.h"
53 #include "gfxFontTest.h"
55 #include "gfxTextRunWordCache.h"
57 #if defined(XP_MACOSX)
58 #include "gfxTestCocoaHelper.h"
59 #endif
61 #ifdef MOZ_WIDGET_GTK2
62 #include "gtk/gtk.h"
63 #endif
65 class FrameTextRunCache;
67 static FrameTextRunCache *gTextRuns = nsnull;
70 * Cache textruns and expire them after 3*10 seconds of no use.
72 class FrameTextRunCache : public nsExpirationTracker<gfxTextRun,3> {
73 public:
74 enum { TIMEOUT_SECONDS = 10 };
75 FrameTextRunCache()
76 : nsExpirationTracker<gfxTextRun,3>(TIMEOUT_SECONDS*1000) {}
77 ~FrameTextRunCache() {
78 AgeAllGenerations();
81 void RemoveFromCache(gfxTextRun* aTextRun) {
82 if (aTextRun->GetExpirationState()->IsTracked()) {
83 RemoveObject(aTextRun);
85 gfxTextRunWordCache::RemoveTextRun(aTextRun);
88 // This gets called when the timeout has expired on a gfxTextRun
89 virtual void NotifyExpired(gfxTextRun* aTextRun) {
90 RemoveFromCache(aTextRun);
91 delete aTextRun;
95 static gfxTextRun *
96 MakeTextRun(const PRUnichar *aText, PRUint32 aLength,
97 gfxFontGroup *aFontGroup, const gfxFontGroup::Parameters* aParams,
98 PRUint32 aFlags)
100 nsAutoPtr<gfxTextRun> textRun;
101 if (aLength == 0) {
102 textRun = aFontGroup->MakeEmptyTextRun(aParams, aFlags);
103 } else if (aLength == 1 && aText[0] == ' ') {
104 textRun = aFontGroup->MakeSpaceTextRun(aParams, aFlags);
105 } else {
106 textRun = gfxTextRunWordCache::MakeTextRun(aText, aLength, aFontGroup,
107 aParams, aFlags);
109 if (!textRun)
110 return nsnull;
111 nsresult rv = gTextRuns->AddObject(textRun);
112 if (NS_FAILED(rv)) {
113 gTextRuns->RemoveFromCache(textRun);
114 return nsnull;
116 return textRun.forget();
119 already_AddRefed<gfxContext>
120 MakeContext ()
122 const int size = 200;
124 nsRefPtr<gfxASurface> surface;
126 surface = gfxPlatform::GetPlatform()->
127 CreateOffscreenSurface(gfxIntSize(size, size),
128 gfxASurface::ContentFromFormat(gfxASurface::ImageFormatRGB24));
129 gfxContext *ctx = new gfxContext(surface);
130 NS_IF_ADDREF(ctx);
131 return ctx;
135 main (int argc, char **argv) {
136 #ifdef MOZ_WIDGET_GTK2
137 gtk_init(&argc, &argv);
138 #endif
139 #ifdef XP_MACOSX
140 CocoaPoolInit();
141 #endif
143 // Initialize XPCOM
144 nsresult rv = NS_InitXPCOM2(nsnull, nsnull, nsnull);
145 if (NS_FAILED(rv))
146 return -1;
148 rv = gfxPlatform::Init();
149 if (NS_FAILED(rv))
150 return -1;
152 // let's get all the xpcom goop out of the system
153 fflush (stderr);
154 fflush (stdout);
156 gTextRuns = new FrameTextRunCache();
158 nsRefPtr<gfxContext> ctx = MakeContext();
160 gfxFontStyle style (FONT_STYLE_NORMAL,
161 NS_FONT_STRETCH_NORMAL,
162 139,
163 10.0,
164 NS_NewPermanentAtom(NS_LITERAL_STRING("en")),
165 0.0,
166 PR_FALSE, PR_FALSE, PR_FALSE,
167 NS_LITERAL_STRING(""),
168 NS_LITERAL_STRING(""));
170 nsRefPtr<gfxFontGroup> fontGroup =
171 gfxPlatform::GetPlatform()->CreateFontGroup(NS_LITERAL_STRING("Geneva, MS Sans Serif, Helvetica,serif"), &style, nsnull);
173 gfxTextRunFactory::Parameters params = {
174 ctx, nsnull, nsnull, nsnull, 0, 60
177 PRUint32 flags = gfxTextRunFactory::TEXT_IS_PERSISTENT;
179 // First load an Arabic word into the cache
180 const char cString[] = "\xd8\xaa\xd9\x85";
181 nsDependentCString cStr(cString);
182 NS_ConvertUTF8toUTF16 str(cStr);
183 gfxTextRun *tr = MakeTextRun(str.get(), str.Length(), fontGroup, &params, flags);
184 tr->GetAdvanceWidth(0, str.Length(), nsnull);
186 // Now try to trigger an assertion with a word cache bug. The first
187 // word is in the cache so it gets added to the new textrun directly.
188 // The second word is not in the cache
189 const char cString2[] = "\xd8\xaa\xd9\x85\n\xd8\xaa\xd8\x85 ";
190 nsDependentCString cStr2(cString2);
191 NS_ConvertUTF8toUTF16 str2(cStr2);
192 gfxTextRun *tr2 = MakeTextRun(str2.get(), str2.Length(), fontGroup, &params, flags);
193 tr2->GetAdvanceWidth(0, str2.Length(), nsnull);
196 fflush (stderr);
197 fflush (stdout);