diff options
author | 2016-11-14 14:07:41 +0900 | |
---|---|---|
committer | 2016-11-15 10:45:01 +0900 | |
commit | b6e20139755afbb4968ec0ac71182c179ea33ac0 (patch) | |
tree | 3bc4010d61fb3eca5e4530d92e199b68e9a743ab | |
parent | d1b6feeef307fa05b5685243f72302ca7f2677d4 (diff) |
Move test only initialization to each test setup.
Global default typeface initialization is only used by test code.
It is good to do in test and remove from production.
Test: ran hwuimicro hwui_unit_tests hwuimacro
Change-Id: I7090b1794828072112540b4e357a6d24bf8f664a
-rw-r--r-- | core/jni/android/graphics/Typeface.cpp | 2 | ||||
-rw-r--r-- | libs/hwui/hwui/Typeface.cpp | 77 | ||||
-rw-r--r-- | libs/hwui/hwui/Typeface.h | 3 | ||||
-rw-r--r-- | libs/hwui/tests/macrobench/main.cpp | 3 | ||||
-rw-r--r-- | libs/hwui/tests/microbench/main.cpp | 3 | ||||
-rw-r--r-- | libs/hwui/tests/unit/main.cpp | 10 |
6 files changed, 45 insertions, 53 deletions
diff --git a/core/jni/android/graphics/Typeface.cpp b/core/jni/android/graphics/Typeface.cpp index 7fc79d29f046..c920b8d653ab 100644 --- a/core/jni/android/graphics/Typeface.cpp +++ b/core/jni/android/graphics/Typeface.cpp @@ -70,7 +70,7 @@ static jlong Typeface_createFromArray(JNIEnv *env, jobject, jlongArray familyArr static void Typeface_setDefault(JNIEnv *env, jobject, jlong faceHandle) { Typeface* face = reinterpret_cast<Typeface*>(faceHandle); - return Typeface::setDefault(face); + Typeface::setDefault(face); } /////////////////////////////////////////////////////////////////////////////// diff --git a/libs/hwui/hwui/Typeface.cpp b/libs/hwui/hwui/Typeface.cpp index 59b1185d9005..f95d98cac8d6 100644 --- a/libs/hwui/hwui/Typeface.cpp +++ b/libs/hwui/hwui/Typeface.cpp @@ -49,60 +49,10 @@ static void resolveStyle(Typeface* typeface) { } Typeface* gDefaultTypeface = NULL; -pthread_once_t gDefaultTypefaceOnce = PTHREAD_ONCE_INIT; - -// This installs a default typeface (from a hardcoded path) that allows -// layouts to work (not crash on null pointer) before the default -// typeface is set. This happens if HWUI is used outside of zygote/app_process. -static minikin::FontCollection *makeFontCollection() { - std::vector<minikin::FontFamily *>typefaces; - const char *fns[] = { - "/system/fonts/Roboto-Regular.ttf", - }; - - minikin::FontFamily *family = new minikin::FontFamily(); - for (size_t i = 0; i < sizeof(fns)/sizeof(fns[0]); i++) { - const char *fn = fns[i]; - ALOGD("makeFontCollection adding %s", fn); - sk_sp<SkTypeface> skFace = SkTypeface::MakeFromFile(fn); - if (skFace != NULL) { - // TODO: might be a nice optimization to get access to the underlying font - // data, but would require us opening the file ourselves and passing that - // to the appropriate Create method of SkTypeface. - minikin::MinikinFont *font = new MinikinFontSkia(std::move(skFace), NULL, 0, 0); - family->addFont(font); - font->Unref(); - } else { - ALOGE("failed to create font %s", fn); - } - } - typefaces.push_back(family); - - minikin::FontCollection *result = new minikin::FontCollection(typefaces); - family->Unref(); - return result; -} - -static void getDefaultTypefaceOnce() { - minikin::Layout::init(); - if (gDefaultTypeface == NULL) { - // We expect the client to set a default typeface, but provide a - // default so we can make progress before that happens. - gDefaultTypeface = new Typeface; - gDefaultTypeface->fFontCollection = makeFontCollection(); - gDefaultTypeface->fSkiaStyle = SkTypeface::kNormal; - gDefaultTypeface->fBaseWeight = 400; - resolveStyle(gDefaultTypeface); - } -} Typeface* Typeface::resolveDefault(Typeface* src) { - if (src == NULL) { - pthread_once(&gDefaultTypefaceOnce, getDefaultTypefaceOnce); - return gDefaultTypeface; - } else { - return src; - } + LOG_ALWAYS_FATAL_IF(gDefaultTypeface == nullptr); + return src == nullptr ? gDefaultTypeface : src; } Typeface* Typeface::createFromTypeface(Typeface* src, SkTypeface::Style style) { @@ -164,4 +114,27 @@ void Typeface::setDefault(Typeface* face) { gDefaultTypeface = face; } +void Typeface::setRobotoTypefaceForTest() { + const char* kRobotoFont = "/system/fonts/Roboto-Regular.ttf"; + sk_sp<SkTypeface> typeface = SkTypeface::MakeFromFile(kRobotoFont); + LOG_ALWAYS_FATAL_IF(typeface == nullptr, "Failed to make typeface from %s", kRobotoFont); + + minikin::FontFamily* family = new minikin::FontFamily(); + minikin::MinikinFont* font = new MinikinFontSkia(std::move(typeface), nullptr, 0, 0); + family->addFont(font); + font->Unref(); + + std::vector<minikin::FontFamily*> typefaces = { family }; + minikin::FontCollection *collection = new minikin::FontCollection(typefaces); + family->Unref(); + + Typeface* hwTypeface = new Typeface(); + hwTypeface->fFontCollection = collection; + hwTypeface->fSkiaStyle = SkTypeface::kNormal; + hwTypeface->fBaseWeight = 400; + hwTypeface->fStyle = minikin::FontStyle(4 /* weight */, false /* italic */); + + Typeface::setDefault(hwTypeface); +} + } diff --git a/libs/hwui/hwui/Typeface.h b/libs/hwui/hwui/Typeface.h index ed0a7ebd56ae..1be630c1e978 100644 --- a/libs/hwui/hwui/Typeface.h +++ b/libs/hwui/hwui/Typeface.h @@ -48,6 +48,9 @@ struct ANDROID_API Typeface { static Typeface* createFromFamilies(const std::vector<minikin::FontFamily*>& families); static void setDefault(Typeface* face); + + // Sets roboto font as the default typeface for testing purpose. + static void setRobotoTypefaceForTest(); }; } diff --git a/libs/hwui/tests/macrobench/main.cpp b/libs/hwui/tests/macrobench/main.cpp index ebc1dd7516c2..1f5622252f6a 100644 --- a/libs/hwui/tests/macrobench/main.cpp +++ b/libs/hwui/tests/macrobench/main.cpp @@ -17,6 +17,7 @@ #include "tests/common/LeakChecker.h" #include "tests/common/TestScene.h" +#include "hwui/Typeface.h" #include "protos/hwui.pb.h" #include "Properties.h" @@ -303,6 +304,8 @@ int main(int argc, char* argv[]) { // set defaults gOpts.count = 150; + Typeface::setRobotoTypefaceForTest(); + parseOptions(argc, argv); if (!gBenchmarkReporter && gOpts.renderOffscreen) { gBenchmarkReporter.reset(new benchmark::ConsoleReporter()); diff --git a/libs/hwui/tests/microbench/main.cpp b/libs/hwui/tests/microbench/main.cpp index 9771c85382b4..b5abf5bc5efa 100644 --- a/libs/hwui/tests/microbench/main.cpp +++ b/libs/hwui/tests/microbench/main.cpp @@ -17,6 +17,8 @@ #include "debug/GlesDriver.h" #include "debug/NullGlesDriver.h" +#include "hwui/Typeface.h" + #include <benchmark/benchmark.h> #include <memory> @@ -27,6 +29,7 @@ using namespace android::uirenderer; int main(int argc, char** argv) { debug::GlesDriver::replace(std::make_unique<debug::NullGlesDriver>()); benchmark::Initialize(&argc, argv); + Typeface::setRobotoTypefaceForTest(); benchmark::RunSpecifiedBenchmarks(); return 0; } diff --git a/libs/hwui/tests/unit/main.cpp b/libs/hwui/tests/unit/main.cpp index d05bdbf1709e..cea84c057b63 100644 --- a/libs/hwui/tests/unit/main.cpp +++ b/libs/hwui/tests/unit/main.cpp @@ -20,6 +20,7 @@ #include "Caches.h" #include "debug/GlesDriver.h" #include "debug/NullGlesDriver.h" +#include "hwui/Typeface.h" #include "thread/TaskManager.h" #include "tests/common/LeakChecker.h" @@ -50,6 +51,13 @@ static void gtestSigHandler(int sig, siginfo_t* siginfo, void* context) { raise(sig); } +class TypefaceEnvironment : public testing::Environment { +public: + virtual void SetUp() { + Typeface::setRobotoTypefaceForTest(); + } +}; + int main(int argc, char* argv[]) { // Register a crash handler struct sigaction sa; @@ -69,6 +77,8 @@ int main(int argc, char* argv[]) { testing::InitGoogleTest(&argc, argv); testing::InitGoogleMock(&argc, argv); + testing::AddGlobalTestEnvironment(new TypefaceEnvironment()); + int ret = RUN_ALL_TESTS(); test::LeakChecker::checkForLeaks(); return ret; |