Skip to content

Commit 1e1b932

Browse files
committed
JBR-1517 Update font layout speedup code to match the variant submitted to OpenJDK (JDK-8220231)
apply corresponding change from OpenJDK 13
1 parent bac12d4 commit 1e1b932

File tree

16 files changed

+235
-271
lines changed

16 files changed

+235
-271
lines changed

src/java.desktop/macosx/classes/sun/font/CFont.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,11 @@ GeneralPath getGlyphVectorOutline(long pScalerContext,
7676
throw new InternalError("Not implemented");
7777
}
7878

79-
@Override
80-
protected long getLayoutTableCache() {
81-
return getLayoutTableCacheNative(getNativeFontPtr());
82-
}
83-
8479
@Override
8580
protected byte[] getTableBytes(int tag) {
8681
return getTableBytesNative(getNativeFontPtr(), tag);
8782
}
8883

89-
private native synchronized long getLayoutTableCacheNative(long nativeFontPtr);
90-
9184
private native byte[] getTableBytesNative(long nativeFontPtr, int tag);
9285

9386
private static native long createNativeFont(final String nativeFontName,

src/java.desktop/macosx/native/libawt_lwawt/font/AWTFont.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,13 @@
2626
#import <Cocoa/Cocoa.h>
2727
#import <JavaRuntimeSupport/JavaRuntimeSupport.h>
2828

29-
#import "fontscalerdefs.h"
30-
3129
#define MAX_STACK_ALLOC_GLYPH_BUFFER_SIZE 256
3230

3331
@interface AWTFont : NSObject {
3432
@public
3533
NSFont *fFont;
3634
CGFontRef fNativeCGFont;
3735
BOOL fIsFakeItalic;
38-
TTLayoutTableCache* layoutTableCache;
3936
}
4037

4138
+ (AWTFont *) awtFontForName:(NSString *)name

src/java.desktop/macosx/native/libawt_lwawt/font/AWTFont.m

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -43,44 +43,17 @@ - (id) initWithFont:(NSFont *)font {
4343
if (self) {
4444
fFont = [font retain];
4545
fNativeCGFont = CTFontCopyGraphicsFont((CTFontRef)font, NULL);
46-
layoutTableCache = NULL;
4746
}
4847
return self;
4948
}
5049

51-
static TTLayoutTableCache* newCFontLayoutTableCache() {
52-
TTLayoutTableCache* ltc = calloc(1, sizeof(TTLayoutTableCache));
53-
if (ltc) {
54-
int i;
55-
for(i=0;i<LAYOUTCACHE_ENTRIES;i++) {
56-
ltc->entries[i].len = -1;
57-
}
58-
}
59-
return ltc;
60-
}
61-
62-
static void freeCFontLayoutTableCache(TTLayoutTableCache* ltc) {
63-
if (ltc) {
64-
int i;
65-
for(i=0;i<LAYOUTCACHE_ENTRIES;i++) {
66-
if(ltc->entries[i].ptr) free (ltc->entries[i].ptr);
67-
}
68-
if (ltc->kernPairs) free(ltc->kernPairs);
69-
free(ltc);
70-
}
71-
}
72-
7350
- (void) dealloc {
7451
[fFont release];
7552
fFont = nil;
7653

7754
if (fNativeCGFont) {
7855
CGFontRelease(fNativeCGFont);
7956
fNativeCGFont = NULL;
80-
if (layoutTableCache != NULL) {
81-
freeCFontLayoutTableCache(layoutTableCache);
82-
layoutTableCache = NULL;
83-
}
8457
}
8558

8659
[super dealloc];
@@ -91,10 +64,6 @@ - (void) finalize {
9164
CGFontRelease(fNativeCGFont);
9265
fNativeCGFont = NULL;
9366
}
94-
if (layoutTableCache != NULL) {
95-
freeCFontLayoutTableCache(layoutTableCache);
96-
layoutTableCache = NULL;
97-
}
9867
[super finalize];
9968
}
10069

@@ -476,23 +445,6 @@ static JNF_MEMBER_CACHE(jm_registerFont, jc_CFontManager,
476445
return (jlong)(awtFont->fNativeCGFont);
477446
}
478447

479-
/*
480-
* Class: sun_font_CFont
481-
* Method: getLayoutTableCacheNative
482-
* Signature: (J)J
483-
*/
484-
JNIEXPORT jlong JNICALL
485-
Java_sun_font_CFont_getLayoutTableCacheNative
486-
(JNIEnv *env, jclass clazz,
487-
jlong awtFontPtr)
488-
{
489-
AWTFont *awtFont = (AWTFont *)jlong_to_ptr(awtFontPtr);
490-
if (awtFont->layoutTableCache == NULL) {
491-
awtFont->layoutTableCache = newCFontLayoutTableCache();
492-
}
493-
return (jlong)(awtFont->layoutTableCache);
494-
}
495-
496448
/*
497449
* Class: sun_font_CFont
498450
* Method: getTableBytesNative

src/java.desktop/share/classes/sun/font/Font2D.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -467,13 +467,6 @@ protected byte[] getTableBytes(int tag) {
467467
return null;
468468
}
469469

470-
/* implemented for fonts backed by an sfnt that has
471-
* OpenType or AAT layout tables.
472-
*/
473-
protected long getLayoutTableCache() {
474-
return 0L;
475-
}
476-
477470
/* Used only on OS X.
478471
*/
479472
protected long getPlatformNativeFontPtr() {

src/java.desktop/share/classes/sun/font/FontScaler.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -181,25 +181,6 @@ public void dispose() {}
181181
abstract int getMissingGlyphCode() throws FontScalerException;
182182
abstract int getGlyphCode(char charCode) throws FontScalerException;
183183

184-
/* This method returns table cache used by native layout engine.
185-
* This cache is essentially just small collection of
186-
* pointers to various truetype tables. See definition of TTLayoutTableCache
187-
* in the fontscalerdefs.h for more details.
188-
*
189-
* Note that tables themselves have same format as defined in the truetype
190-
* specification, i.e. font scaler do not need to perform any preprocessing.
191-
*
192-
* Probably it is better to have API to request pointers to each table
193-
* separately instead of requesting pointer to some native structure.
194-
* (then there is not need to share its definition by different
195-
* implementations of scaler).
196-
* However, this means multiple JNI calls and potential impact on performance.
197-
*
198-
* Note: return value 0 is legal.
199-
* This means tables are not available (e.g. type1 font).
200-
*/
201-
abstract long getLayoutTableCache() throws FontScalerException;
202-
203184
/* Used by the OpenType engine for mark positioning. */
204185
abstract Point2D.Float getGlyphPoint(long pScalerContext,
205186
int glyphCode, int ptNumber)

src/java.desktop/share/classes/sun/font/FreetypeFontScaler.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,6 @@ synchronized GeneralPath getGlyphVectorOutline(
178178
.getNullScaler().getGlyphVectorOutline(0L, glyphs, numGlyphs, x, y);
179179
}
180180

181-
synchronized long getLayoutTableCache() throws FontScalerException {
182-
return getLayoutTableCacheNative(nativeScaler);
183-
}
184-
185181
public synchronized void dispose() {
186182
if (nativeScaler != 0L) {
187183
disposeNativeScaler(font.get(), nativeScaler);
@@ -258,8 +254,6 @@ private native GeneralPath getGlyphVectorOutlineNative(Font2D font,
258254
native Point2D.Float getGlyphPointNative(Font2D font,
259255
long pScalerContext, long pScaler, int glyphCode, int ptNumber);
260256

261-
private native long getLayoutTableCacheNative(long pScaler);
262-
263257
private native void disposeNativeScaler(Font2D font2D, long pScaler);
264258

265259
private native int getGlyphCodeNative(Font2D font, long pScaler, char charCode);

src/java.desktop/share/classes/sun/font/NullFontScaler.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ GeneralPath getGlyphVectorOutline(long pScalerContext, int[] glyphs,
6464
return new GeneralPath();
6565
}
6666

67-
long getLayoutTableCache() {return 0L;}
68-
6967
long createScalerContext(double[] matrix, int aa,
7068
int fm, float boldness, float italic, boolean disableHinting) {
7169
return getNullScalerContext();

src/java.desktop/share/classes/sun/font/SunLayoutEngine.java

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@
3131
package sun.font;
3232

3333
import sun.font.GlyphLayout.*;
34+
import sun.java2d.Disposer;
35+
import sun.java2d.DisposerRecord;
36+
3437
import java.awt.geom.Point2D;
3538
import java.lang.ref.SoftReference;
3639
import java.util.concurrent.ConcurrentHashMap;
37-
import java.util.Locale;
40+
import java.util.WeakHashMap;
3841

3942
/*
4043
* different ways to do this
@@ -143,8 +146,10 @@ public LayoutEngine getEngine(LayoutEngineKey key) {
143146
}
144147
private SoftReference<ConcurrentHashMap<LayoutEngineKey, LayoutEngine>> cacheref =
145148
new SoftReference<>(null);
149+
private static final WeakHashMap<Font2D, FaceRef> facePtr =
150+
new WeakHashMap<>();
146151

147-
private boolean isAAT(Font2D font) {
152+
private static boolean isAAT(Font2D font) {
148153
// CoreText layout code ignores fractional metrics font attribute
149154
// also, using CoreText layout in Harfbuzz code leads to wrong advances for emoji glyphs
150155
return false;
@@ -154,30 +159,67 @@ private SunLayoutEngine(LayoutEngineKey key) {
154159
this.key = key;
155160
}
156161

162+
private long getFacePtr(Font2D font2D) {
163+
FaceRef ref;
164+
synchronized (facePtr) {
165+
ref = facePtr.computeIfAbsent(font2D, FaceRef::new);
166+
}
167+
return ref.getNativePtr();
168+
}
169+
157170
public void layout(FontStrikeDesc desc, float[] mat, float ptSize, int gmask,
158171
int baseIndex, TextRecord tr, int typo_flags,
159172
Point2D.Float pt, GVData data) {
160173
Font2D font = key.font();
161174
FontStrike strike = font.getStrike(desc);
162-
long layoutTables = font.getLayoutTableCache();
163175
long pNativeFont = font.getPlatformNativeFontPtr(); // used on OSX
164-
// pScaler probably not needed long term.
165-
long pScaler = 0L;
166-
if (font instanceof FileFont) {
167-
pScaler = ((FileFont)font).getScaler().nativeScaler;
176+
long pFace = getFacePtr(font);
177+
if (pFace != 0) {
178+
shape(font, strike, ptSize, mat, pNativeFont,
179+
pFace, isAAT(font),
180+
tr.text, data, key.script(),
181+
tr.start, tr.limit, baseIndex, pt,
182+
typo_flags, gmask);
168183
}
169-
shape(font, strike, ptSize, mat, pScaler, pNativeFont,
170-
layoutTables, isAAT(font),
171-
tr.text, data, key.script(),
172-
tr.start, tr.limit, baseIndex, pt,
173-
typo_flags, gmask);
174184
}
175185

176186
/* Native method to invoke harfbuzz layout engine */
177187
private static native boolean
178188
shape(Font2D font, FontStrike strike, float ptSize, float[] mat,
179-
long pscaler, long pNativeFont, long layoutTables, boolean aat,
189+
long pNativeFont, long pFace, boolean aat,
180190
char[] chars, GVData data,
181191
int script, int offset, int limit,
182192
int baseIndex, Point2D.Float pt, int typo_flags, int slot);
193+
194+
private static native long createFace(Font2D font,
195+
boolean aat,
196+
long platformNativeFontPtr);
197+
198+
private static native void disposeFace(long facePtr);
199+
200+
private static class FaceRef implements DisposerRecord {
201+
private Font2D font;
202+
private Long facePtr;
203+
204+
private FaceRef(Font2D font) {
205+
this.font = font;
206+
}
207+
208+
private synchronized long getNativePtr() {
209+
if (facePtr == null) {
210+
facePtr = createFace(font, isAAT(font),
211+
font.getPlatformNativeFontPtr());
212+
if (facePtr != 0) {
213+
Disposer.addObjectRecord(font, this);
214+
}
215+
font = null;
216+
}
217+
return facePtr;
218+
}
219+
220+
@Override
221+
public void dispose() {
222+
disposeFace(facePtr);
223+
}
224+
}
183225
}

src/java.desktop/share/classes/sun/font/TrueTypeFont.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -898,15 +898,6 @@ ByteBuffer getTableBuffer(int tag) {
898898
}
899899
}
900900

901-
@Override
902-
protected long getLayoutTableCache() {
903-
try {
904-
return getScaler().getLayoutTableCache();
905-
} catch(FontScalerException fe) {
906-
return 0L;
907-
}
908-
}
909-
910901
@Override
911902
protected byte[] getTableBytes(int tag) {
912903
ByteBuffer buffer = getTableBuffer(tag);

src/java.desktop/share/native/common/font/fontscalerdefs.h

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -88,32 +88,8 @@ typedef struct GlyphInfo {
8888
*/
8989
#define INVISIBLE_GLYPHS 0xfffe
9090

91-
#define GSUB_TAG 0x47535542 /* 'GSUB' */
92-
#define GPOS_TAG 0x47504F53 /* 'GPOS' */
93-
#define GDEF_TAG 0x47444546 /* 'GDEF' */
94-
#define HEAD_TAG 0x68656164 /* 'head' */
95-
#define MORT_TAG 0x6D6F7274 /* 'mort' */
96-
#define MORX_TAG 0x6D6F7278 /* 'morx' */
97-
#define KERN_TAG 0x6B65726E /* 'kern' */
98-
99-
typedef struct TTLayoutTableCacheEntry {
100-
const void* ptr;
101-
int len;
102-
int tag;
103-
} TTLayoutTableCacheEntry;
104-
105-
#define LAYOUTCACHE_ENTRIES 7
106-
107-
typedef struct TTLayoutTableCache {
108-
TTLayoutTableCacheEntry entries[LAYOUTCACHE_ENTRIES];
109-
void* kernPairs;
110-
} TTLayoutTableCache;
111-
11291
#include "sunfontids.h"
11392

114-
JNIEXPORT extern TTLayoutTableCache* newLayoutTableCache();
115-
JNIEXPORT extern void freeLayoutTableCache(TTLayoutTableCache* ltc);
116-
11793
/* If font is malformed then scaler context created by particular scaler
11894
* will be replaced by null scaler context.
11995
* Note that this context is not compatible with structure of the context

0 commit comments

Comments
 (0)