Skip to content

Commit d11d4e2

Browse files
committed
perf(paintpool): Avoid unnecessary managed/unmanaged transitions
1 parent 04dba74 commit d11d4e2

File tree

2 files changed

+53
-32
lines changed

2 files changed

+53
-32
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package Uno.UI;
2+
3+
import android.graphics.*;
4+
import android.text.*;
5+
6+
public final class TextPaintPoolNative {
7+
8+
private TextPaintPoolNative() {
9+
}
10+
11+
public static TextPaint buildPaint(
12+
float density,
13+
float size,
14+
float letterSpacing,
15+
Typeface typeface,
16+
int color,
17+
boolean underline,
18+
boolean strikethrough,
19+
boolean superscript,
20+
Shader shader)
21+
{
22+
TextPaint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
23+
paint.density = density;
24+
paint.setTextSize(size);
25+
paint.setUnderlineText(underline);
26+
paint.setStrikeThruText(strikethrough);
27+
28+
if (shader != null) {
29+
paint.setShader(shader);
30+
}
31+
32+
if (superscript) {
33+
paint.baselineShift += (int)(paint.ascent() / 2);
34+
}
35+
36+
paint.setLetterSpacing(letterSpacing);
37+
paint.setTypeface(typeface);
38+
paint.setColor(color);
39+
40+
return paint;
41+
}
42+
}

src/Uno.UI/Controls/PaintPool.Android.cs

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ namespace Uno.UI.Controls
2020
/// </summary>
2121
internal class TextPaintPool
2222
{
23-
private static readonly Action LogCharacterSpacingNotSupported =
24-
Actions.CreateOnce(() => typeof(TextPaintPool).Log().Warn("CharacterSpacing is only supported on Android API Level 21+"));
25-
2623
private record Entry(
2724
FontWeight FontWeight,
2825
FontStyle FontStyle,
@@ -128,39 +125,21 @@ private static void TryScavenge()
128125

129126
private static TextPaint InnerBuildPaint(FontWeight fontWeight, FontStyle fontStyle, FontFamily fontFamily, double fontSize, double characterSpacing, Color foreground, Shader shader, BaseLineAlignment baselineAlignment, TextDecorations textDecorations)
130127
{
131-
var paint = new TextPaint(PaintFlags.AntiAlias);
132-
133128
var paintSpecs = BuildPaintValueSpecs(fontSize, characterSpacing);
134129

135-
paint.Density = paintSpecs.density;
136-
paint.TextSize = paintSpecs.textSize;
137-
paint.UnderlineText = (textDecorations & TextDecorations.Underline) == TextDecorations.Underline;
138-
paint.StrikeThruText = (textDecorations & TextDecorations.Strikethrough) == TextDecorations.Strikethrough;
139-
if (shader != null)
140-
{
141-
paint.SetShader(shader);
142-
}
143-
144-
if (baselineAlignment == BaseLineAlignment.Superscript)
145-
{
146-
paint.BaselineShift += (int)(paint.Ascent() / 2);
147-
}
148-
149-
if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Lollipop)
150-
{
151-
paint.LetterSpacing = paintSpecs.letterSpacing;
152-
}
153-
else
154-
{
155-
LogCharacterSpacingNotSupported();
156-
}
157-
158130
var typefaceStyle = TypefaceStyleHelper.GetTypefaceStyle(fontStyle, fontWeight);
159-
var typeface = FontHelper.FontFamilyToTypeFace(fontFamily, fontWeight, typefaceStyle);
160-
paint.SetTypeface(typeface);
161-
paint.Color = foreground;
162131

163-
return paint;
132+
return TextPaintPoolNative.BuildPaint(
133+
paintSpecs.density,
134+
paintSpecs.textSize,
135+
paintSpecs.letterSpacing,
136+
FontHelper.FontFamilyToTypeFace(fontFamily, fontWeight, typefaceStyle),
137+
(int)((Android.Graphics.Color)foreground),
138+
(textDecorations & TextDecorations.Underline) == TextDecorations.Underline,
139+
(textDecorations & TextDecorations.Strikethrough) == TextDecorations.Strikethrough,
140+
baselineAlignment == BaseLineAlignment.Superscript,
141+
shader
142+
);
164143
}
165144

166145
internal static (float density, float textSize, float letterSpacing) BuildPaintValueSpecs(double fontSize, double characterSpacing)

0 commit comments

Comments
 (0)