• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <gtest/gtest.h>
18 
19 #include <condition_variable>
20 #include <mutex>
21 #include <random>
22 #include <thread>
23 
24 #include <cutils/log.h>
25 
26 #include "MinikinInternal.h"
27 #include "minikin/FontCollection.h"
28 #include "minikin/Layout.h"
29 #include "../util/FontTestUtils.h"
30 
31 namespace minikin {
32 
33 const char* SYSTEM_FONT_PATH = "/system/fonts/";
34 const char* SYSTEM_FONT_XML = "/system/etc/fonts.xml";
35 
36 constexpr int LAYOUT_COUNT_PER_COLLECTION = 500;
37 constexpr int COLLECTION_COUNT_PER_THREAD = 15;
38 constexpr int NUM_THREADS = 10;
39 
40 std::mutex gMutex;
41 std::condition_variable gCv;
42 bool gReady = false;
43 
generateTestText(std::mt19937 * mt,int lettersInWord,int wordsInText)44 static std::vector<uint16_t> generateTestText(
45         std::mt19937* mt, int lettersInWord, int wordsInText) {
46     std::uniform_int_distribution<uint16_t> dist('A', 'Z');
47 
48     std::vector<uint16_t> text;
49     text.reserve((lettersInWord + 1) * wordsInText - 1);
50     for (int i = 0; i < wordsInText; ++i) {
51         if (i != 0) {
52             text.emplace_back(' ');
53         }
54         for (int j = 0; j < lettersInWord; ++j) {
55             text.emplace_back(dist(*mt));
56         }
57     }
58     return text;
59 }
60 
thread_main(int tid)61 static void thread_main(int tid) {
62     {
63         // Wait until all threads are created.
64         std::unique_lock<std::mutex> lock(gMutex);
65         gCv.wait(lock, [] { return gReady; });
66     }
67 
68     std::mt19937 mt(tid);
69     MinikinPaint paint;
70 
71     for (int i = 0; i < COLLECTION_COUNT_PER_THREAD; ++i) {
72         std::shared_ptr<FontCollection> collection(
73                 getFontCollection(SYSTEM_FONT_PATH, SYSTEM_FONT_XML));
74 
75         for (int j = 0; j < LAYOUT_COUNT_PER_COLLECTION; ++j) {
76             // Generates 10 of 3-letter words so that the word sometimes hit the cache.
77             Layout layout;
78             std::vector<uint16_t> text = generateTestText(&mt, 3, 10);
79             layout.doLayout(text.data(), 0, text.size(), text.size(), kBidi_LTR, FontStyle(),
80                     paint, collection);
81             std::vector<float> advances(text.size());
82             layout.getAdvances(advances.data());
83             for (size_t k = 0; k < advances.size(); ++k) {
84                 // MinikinFontForTest always returns 10.0f for horizontal advance.
85                 LOG_ALWAYS_FATAL_IF(advances[k] != 10.0f, "Memory corruption detected.");
86             }
87         }
88     }
89 }
90 
TEST(MultithreadTest,ThreadSafeStressTest)91 TEST(MultithreadTest, ThreadSafeStressTest) {
92     std::vector<std::thread> threads;
93 
94     {
95         std::unique_lock<std::mutex> lock(gMutex);
96         threads.reserve(NUM_THREADS);
97         for (int i = 0; i < NUM_THREADS; ++i) {
98             threads.emplace_back(&thread_main, i);
99         }
100         gReady = true;
101     }
102     gCv.notify_all();
103 
104     for (auto& thread : threads) {
105         thread.join();
106     }
107 }
108 
109 }  // namespace minikin
110