[libbluray-devel] BDFontMetrics: synchronize freetype access

hpi1 git at videolan.org
Wed Mar 19 00:20:16 CET 2014


libbluray | branch: master | hpi1 <hpi1 at anonymous.org> | Wed Mar 19 01:06:40 2014 +0200| [88d86c540551dc01ecafb3af74747ae823be6a81] | committer: hpi1

BDFontMetrics: synchronize freetype access

Fixes crashes when multiple threads use the same font

> http://git.videolan.org/gitweb.cgi/libbluray.git/?a=commit;h=88d86c540551dc01ecafb3af74747ae823be6a81
---

 src/libbluray/bdj/java-j2me/java/awt/BDGraphics.java |    6 ++++--
 src/libbluray/bdj/java-j2se/java/awt/BDGraphics.java |    8 +++++---
 src/libbluray/bdj/java/java/awt/BDFontMetrics.java   |   12 ++++++++----
 3 files changed, 17 insertions(+), 9 deletions(-)

diff --git a/src/libbluray/bdj/java-j2me/java/awt/BDGraphics.java b/src/libbluray/bdj/java-j2me/java/awt/BDGraphics.java
index 7831fd6..c8dcfe4 100644
--- a/src/libbluray/bdj/java-j2me/java/awt/BDGraphics.java
+++ b/src/libbluray/bdj/java-j2me/java/awt/BDGraphics.java
@@ -502,12 +502,14 @@ class BDGraphics extends Graphics2D implements ConstrainableGraphics {
 
     /** Draws the given string. */
     public void drawString(String string, int x, int y) {
-        drawStringN(fontMetrics.ftFace, string, x, y, foreground.getRGB());
+        if (fontMetrics != null) {
+            fontMetrics.drawString(this, string, x, y, foreground.getRGB());
+        }
     }
 
     /** Draws the given character array. */
     public void drawChars(char chars[], int offset, int length, int x, int y) {
-        drawStringN(fontMetrics.ftFace, new String(chars, offset, length), x, y, foreground.getRGB());
+        drawString(new String(chars, offset, length), x, y);
     }
 
     public void drawString(AttributedCharacterIterator arg0, int arg1, int arg2) {
diff --git a/src/libbluray/bdj/java-j2se/java/awt/BDGraphics.java b/src/libbluray/bdj/java-j2se/java/awt/BDGraphics.java
index a7203aa..26f7ea5 100644
--- a/src/libbluray/bdj/java-j2se/java/awt/BDGraphics.java
+++ b/src/libbluray/bdj/java-j2se/java/awt/BDGraphics.java
@@ -584,11 +584,13 @@ class BDGraphics extends Graphics2D implements ConstrainableGraphics {
 
     /** Draws the given string. */
     public void drawString(String string, int x, int y) {
-        drawStringN(fontMetrics.ftFace, string, x, y, foreground.getRGB());
+        if (fontMetrics != null) {
+            fontMetrics.drawString(this, string, x, y, foreground.getRGB());
+        }
     }
 
     public void drawString(String string, float x, float y) {
-        drawStringN(fontMetrics.ftFace, string, (int)x, (int)y, foreground.getRGB());
+        drawString(string, (int)x, (int)y);
     }
 
     public void drawRenderableImage(RenderableImage img, AffineTransform xform)  {
@@ -601,7 +603,7 @@ class BDGraphics extends Graphics2D implements ConstrainableGraphics {
 
     /** Draws the given character array. */
     public void drawChars(char chars[], int offset, int length, int x, int y) {
-        drawStringN(fontMetrics.ftFace, new String(chars, offset, length), x, y, foreground.getRGB());
+        drawString(new String(chars, offset, length), x, y);
     }
 
     public void drawString(AttributedCharacterIterator arg0, int arg1, int arg2) {
diff --git a/src/libbluray/bdj/java/java/awt/BDFontMetrics.java b/src/libbluray/bdj/java/java/awt/BDFontMetrics.java
index 8cc5ffb..5666fc0 100644
--- a/src/libbluray/bdj/java/java/awt/BDFontMetrics.java
+++ b/src/libbluray/bdj/java/java/awt/BDFontMetrics.java
@@ -162,7 +162,7 @@ public class BDFontMetrics extends FontMetrics {
         fontNameMap.remove(name);
     }
 
-    long ftFace;
+    private long ftFace;
     private int ascent;
     private int descent;
     private int leading;
@@ -195,6 +195,10 @@ public class BDFontMetrics extends FontMetrics {
     private native int stringWidthN(long ftFace, String string);
     private native int charsWidthN(long ftFace, char chars[], int offset, int len);
 
+    protected synchronized void drawString(BDGraphics g, String string, int x, int y, int rgb) {
+        g.drawStringN(ftFace, string, x, y, rgb);
+    }
+
     public int getAscent() {
         return ascent;
     }
@@ -214,7 +218,7 @@ public class BDFontMetrics extends FontMetrics {
     /**
      * Fast lookup of first 256 chars as these are always the same eg. ASCII charset.
      */
-    public int charWidth(char c) {
+    public synchronized int charWidth(char c) {
         if (c < 256)
             return widths[c];
         return charWidthN(ftFace, c);
@@ -223,14 +227,14 @@ public class BDFontMetrics extends FontMetrics {
     /**
      * Return the width of the specified string in this Font.
      */
-    public int stringWidth(String string) {
+    public synchronized int stringWidth(String string) {
         return stringWidthN(ftFace, string);
     }
 
     /**
      * Return the width of the specified char[] in this Font.
      */
-    public int charsWidth(char chars[], int offset, int length) {
+    public synchronized int charsWidth(char chars[], int offset, int length) {
         return charsWidthN(ftFace, chars, offset, length);
     }
 



More information about the libbluray-devel mailing list