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 "minikin/Layout.h"
18 
19 #include <condition_variable>
20 #include <mutex>
21 #include <random>
22 #include <thread>
23 
24 #include <android-base/thread_annotations.h>
25 #include <cutils/log.h>
26 #include <gtest/gtest.h>
27 
28 #include "minikin/FontCollection.h"
29 #include "minikin/Macros.h"
30 #include "minikin/MinikinPaint.h"
31 #include "FontTestUtils.h"
32 #include "MinikinInternal.h"
33 #include "PathUtils.h"
34 
35 using android::base::ScopedLockAssertion;
36 
37 namespace minikin {
38 
39 constexpr int LAYOUT_COUNT_PER_COLLECTION = 500;
40 constexpr int COLLECTION_COUNT_PER_THREAD = 15;
41 constexpr int NUM_THREADS = 10;
42 
43 std::mutex gMutex;
44 std::condition_variable gCv;
45 bool gReady GUARDED_BY(gMutex) = false;
46 
generateTestText(std::mt19937 * mt,int lettersInWord,int wordsInText)47 static std::vector<uint16_t> generateTestText(std::mt19937* mt, int lettersInWord,
48                                               int wordsInText) {
49     std::uniform_int_distribution<uint16_t> dist('A', 'Z');
50 
51     std::vector<uint16_t> text;
52     text.reserve((lettersInWord + 1) * wordsInText - 1);
53     for (int i = 0; i < wordsInText; ++i) {
54         if (i != 0) {
55             text.emplace_back(' ');
56         }
57         for (int j = 0; j < lettersInWord; ++j) {
58             text.emplace_back(dist(*mt));
59         }
60     }
61     return text;
62 }
63 
thread_main(int tid)64 static void thread_main(int tid) {
65     {
66         // Wait until all threads are created.
67         std::unique_lock<std::mutex> lock(gMutex);
68         gCv.wait(lock, []() EXCLUSIVE_LOCKS_REQUIRED(gMutex) { return gReady; });
69     }
70 
71     std::mt19937 mt(tid);
72 
73     for (int i = 0; i < COLLECTION_COUNT_PER_THREAD; ++i) {
74         MinikinPaint paint(buildFontCollection("Ascii.ttf"));
75         paint.size = 10.0f;  // Make 1em = 10px
76 
77         for (int j = 0; j < LAYOUT_COUNT_PER_COLLECTION; ++j) {
78             // Generates 10 of 3-letter words so that the word sometimes hit the cache.
79             std::vector<uint16_t> text = generateTestText(&mt, 3, 10);
80             Layout layout(text, Range(0, text.size()), Bidi::LTR, paint, StartHyphenEdit::NO_EDIT,
81                           EndHyphenEdit::NO_EDIT);
82             for (size_t k = 0; k < text.size(); ++k) {
83                 // All characters in Ascii.ttf has 1.0em horizontal advance.
84                 LOG_ALWAYS_FATAL_IF(layout.getCharAdvance(k) != 10.0f,
85                                     "Memory corruption detected.");
86             }
87         }
88     }
89 }
90 
TEST(MultithreadTest,ThreadSafeStressTest)91 TEST(MultithreadTest, ThreadSafeStressTest) {
92     std::vector<std::thread> threads;
93 
94     {
95         ScopedLockAssertion 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