1 /*
2  * Copyright (C) 2019 JetBrains s.r.o.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  * @bug 8220231
27  * @summary Cache HarfBuzz face object for same font's text layout calls
28  * @comment Test layout operations for the same font performed simultaneously
29  *          from multiple threads
30  */
31 
32 import java.awt.Font;
33 import java.awt.font.FontRenderContext;
34 import java.awt.font.GlyphVector;
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.concurrent.CyclicBarrier;
38 import java.util.concurrent.atomic.AtomicReference;
39 
40 public class FontLayoutStressTest {
41     private static final int NUMBER_OF_THREADS =
42             Runtime.getRuntime().availableProcessors() * 2;
43     private static final long TIME_TO_RUN_NS = 1_000_000_000; // 1 second
44     private static final Font FONT = new Font(Font.SERIF, Font.PLAIN, 12);
45     private static final FontRenderContext FRC = new FontRenderContext(null,
46             false, false);
47     private static final char[] TEXT = "Lorem ipsum dolor sit amet, ..."
48             .toCharArray();
49 
doLayout()50     private static double doLayout() {
51         GlyphVector gv = FONT.layoutGlyphVector(FRC, TEXT, 0, TEXT.length,
52                 Font.LAYOUT_LEFT_TO_RIGHT);
53         return gv.getGlyphPosition(gv.getNumGlyphs()).getX();
54     }
55 
main(String[] args)56     public static void main(String[] args) throws Throwable {
57         double expectedWidth = doLayout();
58         AtomicReference<Throwable> throwableRef = new AtomicReference<>();
59         CyclicBarrier barrier = new CyclicBarrier(NUMBER_OF_THREADS);
60         List<Thread> threads = new ArrayList<>();
61         for (int i = 0; i < NUMBER_OF_THREADS; i++) {
62             Thread thread = new Thread(() -> {
63                 try {
64                     barrier.await();
65                     long timeToStop = System.nanoTime() + TIME_TO_RUN_NS;
66                     while (System.nanoTime() < timeToStop) {
67                         double width = doLayout();
68                         if (width != expectedWidth) {
69                             throw new RuntimeException(
70                                     "Unexpected layout result");
71                         }
72                     }
73                 } catch (Throwable e) {
74                     throwableRef.set(e);
75                 }
76             });
77             threads.add(thread);
78             thread.start();
79         }
80         for (Thread thread : threads) {
81             thread.join();
82         }
83         Throwable throwable = throwableRef.get();
84         if (throwable != null) {
85             throw throwable;
86         }
87     }
88 }
89