JetBrains / skija

Java bindings for Skia
Apache License 2.0
2.63k stars 127 forks source link

Possible incompatibility with JavaFx #113

Closed mjmeintjes closed 3 years ago

mjmeintjes commented 3 years ago

Not sure where this problem lies, but I've been experiencing a core dump crash somewhere in libfreetype when JavaFx is initialized before rendering text using canvas drawString.

Ubuntu 18.04
openjdk version "12.0.1" 2019-04-16
OpenJDK Runtime Environment Zulu12.2+3-CA (build 12.0.1+12)
OpenJDK 64-Bit Server VM Zulu12.2+3-CA (build 12.0.1+12, mixed mode, sharing)

I am trying to use skija's text rendering functionality. However, when JavaFx is initialized before trying to render with skija, the whole repl crashes with a core dump somewhere in libfreetype.

When I do the skija text rendering first, and then initialize JavaFx, things seem to work.

I have added a demo project here: https://github.com/mjmeintjes/skija-cljfx-crash-demo

If I execute (Platform/startup #(Platform/setImplicitExit false)) before (create-image-with-text), then the JVM crashes due to a crash in native code, somewhere in libfreetype.

However, if I execute (create-image-with-text) first, then I can run the startup code and then (create-image-with-text) can successfully run.

tonsky commented 3 years ago

Interesting! It worked for me on macOS, will take a look on a Linux soon

mjmeintjes commented 3 years ago

I've added a Dockerfile and instructions to run to the repo, should be easier to reproduce now.

tonsky commented 3 years ago

Looks like it has something to do with Freetype. I haven't figured out what exactly that is

Stack: [0x00007f65e7139000,0x00007f65e723a000],  sp=0x00007f65e7235ac0,  free space=1010k
Native frames: (J=compiled Java code, A=aot compiled Java code, j=interpreted, Vv=VM code, C=native code)
C  [libfreetype.so.6+0x53718]
C  [libskija.so+0x67377c]  SkScalerContext::internalMakeGlyph(SkPackedGlyphID, SkMask::Format)+0x12a
C  [libskija.so+0x673602]  SkScalerContext::makeGlyph(SkPackedGlyphID)+0x44
C  [libskija.so+0xeb5764]  SkScalerCache::digest(SkPackedGlyphID)+0x9c
C  [libskija.so+0xeb564f]  SkScalerCache::glyph(SkPackedGlyphID)+0x3d
C  [libskija.so+0xeb5b6e]  SkScalerCache::internalPrepare(SkSpan<unsigned short const>, SkScalerCache::PathDetail, SkGlyph const**)+0xca
C  [libskija.so+0xeb5ec9]  SkScalerCache::metrics(SkSpan<unsigned short const>, SkGlyph const**)+0x71
C  [libskija.so+0x68f3d0]  SkStrikeCache::Strike::metrics(SkSpan<unsigned short const>, SkGlyph const**)+0x5a
C  [libskija.so+0x68eb59]  SkBulkGlyphMetrics::glyphs(SkSpan<unsigned short const>)+0x79
C  [libskija.so+0x5975bc]  draw_text_positions(SkFont const&, SkSpan<unsigned short const>, SkPoint, SkPoint*)+0xa6
C  [libskija.so+0x597880]  SkGlyphRunBuilder::textToGlyphRunList(SkFont const&, SkPaint const&, void const*, unsigned long, SkPoint, SkTextEncoding)+0x146
C  [libskija.so+0x555f5f]  SkCanvas::drawSimpleText(void const*, unsigned long, SkTextEncoding, float, float, SkFont const&, SkPaint const&)+0x1e9
C  [libskija.so+0x20e952]  SkCanvas::drawString(SkString const&, float, float, SkFont const&, SkPaint const&)+0x76
C  [libskija.so+0x20d822]  Java_org_jetbrains_skija_Canvas__1nDrawString+0x94
j  org.jetbrains.skija.Canvas._nDrawString(JLjava/lang/String;FFJJ)V+0
j  org.jetbrains.skija.Canvas.drawString(Ljava/lang/String;FFLorg/jetbrains/skija/Font;Lorg/jetbrains/skija/Paint;)Lorg/jetbrains/skija/Canvas;+63
j  mattsum.skija_test$create_image_with_text.invokeStatic()Ljava/lang/Object;+203
j  mattsum.skija_test$create_image_with_text.invoke()Ljava/lang/Object;+0
tonsky commented 3 years ago

More specifically, this line fails https://github.com/google/skia/blob/d9b8efde6df32e7480c985177118cdd4b72a5b0e/src/ports/SkFontHost_FreeType.cpp#L1305

tonsky commented 3 years ago

Ok, I think we fixed it in 0.92.30. The core of the problem was Freetype, which was initialized both statically (us) and dynamically (JFX). I switched it to dynamic for Linux and the problem seems to be gone. Works on my Ubuntu 20.04. Please let me know if it bugs you again.

mjmeintjes commented 3 years ago

Thanks, 0.92.30 seems to have fixed this issue for me as well.