1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/tests/gtest/gfxTextRunPerfTest.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,120 @@ 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 "mozilla/ArrayUtils.h" 1.12 + 1.13 +#include "nsCOMPtr.h" 1.14 +#include "nsTArray.h" 1.15 +#include "nsString.h" 1.16 +#include "nsDependentString.h" 1.17 + 1.18 +#include "prinrval.h" 1.19 + 1.20 +#include "gfxContext.h" 1.21 +#include "gfxFont.h" 1.22 +#include "gfxPlatform.h" 1.23 + 1.24 +#include "gfxFontTest.h" 1.25 + 1.26 +using namespace mozilla; 1.27 + 1.28 +struct TestEntry { 1.29 + const char* mFamilies; 1.30 + const char* mString; 1.31 +}; 1.32 + 1.33 +TestEntry testList[] = { 1.34 +#include "per-word-runs.h" 1.35 +{ nullptr, nullptr } // terminator 1.36 +}; 1.37 + 1.38 +static already_AddRefed<gfxContext> 1.39 +MakeContext () 1.40 +{ 1.41 + const int size = 200; 1.42 + 1.43 + nsRefPtr<gfxASurface> surface; 1.44 + 1.45 + surface = gfxPlatform::GetPlatform()-> 1.46 + CreateOffscreenSurface(IntSize(size, size), 1.47 + gfxASurface::ContentFromFormat(gfxImageFormat::RGB24)); 1.48 + nsRefPtr<gfxContext> ctx = new gfxContext(surface); 1.49 + return ctx.forget(); 1.50 +} 1.51 + 1.52 +const char* lastFamilies = nullptr; 1.53 + 1.54 +static void 1.55 +RunTest (TestEntry *test, gfxContext *ctx) { 1.56 + nsRefPtr<gfxFontGroup> fontGroup; 1.57 + if (!lastFamilies || strcmp(lastFamilies, test->mFamilies)) { 1.58 + gfxFontStyle style_western_normal_16 (mozilla::gfx::FontStyle::NORMAL, 1.59 + 400, 1.60 + 0, 1.61 + 16.0, 1.62 + NS_NewPermanentAtom(NS_LITERAL_STRING("en")), 1.63 + 0.0, 1.64 + false, false, 1.65 + NS_LITERAL_STRING("")); 1.66 + 1.67 + fontGroup = gfxPlatform::GetPlatform()->CreateFontGroup(NS_ConvertUTF8toUTF16(test->mFamilies), &style_western_normal_16, nullptr); 1.68 + } 1.69 + 1.70 + nsAutoPtr<gfxTextRun> textRun; 1.71 + uint32_t i; 1.72 + bool isASCII = true; 1.73 + for (i = 0; test->mString[i]; ++i) { 1.74 + if (test->mString[i] & 0x80) { 1.75 + isASCII = false; 1.76 + } 1.77 + } 1.78 + gfxTextRunFactory::Parameters params = { 1.79 + ctx, nullptr, nullptr, nullptr, 0, 60 1.80 + }; 1.81 + uint32_t flags = gfxTextRunFactory::TEXT_IS_PERSISTENT; 1.82 + uint32_t length; 1.83 + gfxFontTestStore::NewStore(); 1.84 + if (isASCII) { 1.85 + flags |= gfxTextRunFactory::TEXT_IS_ASCII | 1.86 + gfxTextRunFactory::TEXT_IS_8BIT; 1.87 + length = strlen(test->mString); 1.88 + textRun = fontGroup->MakeTextRun(reinterpret_cast<const uint8_t*>(test->mString), length, ¶ms, flags); 1.89 + } else { 1.90 + NS_ConvertUTF8toUTF16 str(nsDependentCString(test->mString)); 1.91 + length = str.Length(); 1.92 + textRun = fontGroup->MakeTextRun(str.get(), length, ¶ms, flags); 1.93 + } 1.94 + 1.95 + // Should we test drawing? 1.96 + // textRun->Draw(ctx, gfxPoint(0,0), 0, length, nullptr, nullptr, nullptr); 1.97 + 1.98 + textRun->GetAdvanceWidth(0, length, nullptr); 1.99 + gfxFontTestStore::DeleteStore(); 1.100 +} 1.101 + 1.102 +uint32_t iterations = 1; 1.103 + 1.104 +TEST(Gfx, TextRunPref) { 1.105 + nsRefPtr<gfxContext> context = MakeContext(); 1.106 + 1.107 + // Start timing 1.108 + PRIntervalTime start = PR_IntervalNow(); 1.109 + 1.110 + for (uint32_t i = 0; i < iterations; ++i) { 1.111 + for (uint test = 0; 1.112 + test < ArrayLength(testList) - 1; 1.113 + test++) 1.114 + { 1.115 + RunTest(&testList[test], context); 1.116 + } 1.117 + } 1.118 + 1.119 + PRIntervalTime end = PR_IntervalNow(); 1.120 + 1.121 + printf("Elapsed time (ms): %d\n", PR_IntervalToMilliseconds(end - start)); 1.122 + 1.123 +}