Skip to content

Commit

Permalink
[Chore] Add Android LoadImageFromFont benchmark (#24033)
Browse files Browse the repository at this point in the history
  • Loading branch information
albyrock87 committed Aug 20, 2024
1 parent 639fcb0 commit ab55e36
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.microsoft.maui.glide;

import android.util.Log;

public class GlideLogging {
private static final String TAG = "Glide";
private static final boolean IS_VERBOSE_LOGGABLE = Log.isLoggable(TAG, Log.VERBOSE);

public static boolean isVerboseLoggable() {
return IS_VERBOSE_LOGGABLE;
}

public static void v(String message) {
if (IS_VERBOSE_LOGGABLE) {
Log.v(TAG, message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;

import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.Registry;
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.module.AppGlideModule;

import com.microsoft.maui.ImageLoaderCallback;
import com.microsoft.maui.glide.GlideLogging;
import com.microsoft.maui.glide.fallback.ImageLoaderCallbackModelLoaderFactory;
import com.microsoft.maui.glide.font.FontModel;
import com.microsoft.maui.glide.font.FontModelLoaderFactory;
Expand All @@ -33,4 +36,13 @@ public void registerComponents(Context context, Glide glide, Registry registry)
public boolean isManifestParsingEnabled() {
return false;
}
}

@Override
public void applyOptions(Context context, GlideBuilder builder) {
// Glide is checking for the log level only on some classes, so we have to do it ourselves here.
// Command: adb shell setprop log.tag.Glide VERBOSE
if (GlideLogging.isVerboseLoggable()) {
builder.setLogLevel(Log.VERBOSE);
}
}
}
2 changes: 1 addition & 1 deletion src/Core/src/Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<ProjectReference Include="..\..\Graphics\src\Graphics.Win2D\Graphics.Win2D.csproj" />
</ItemGroup>
<ItemGroup Condition="$(TargetFramework.Contains('-android'))">
<PackageReference Include="Xamarin.Android.Glide" Version="4.15.1.2" />
<PackageReference Include="Xamarin.Android.Glide" Version="$(_XamarinAndroidGlideVersion)" />
<PackageReference Include="Xamarin.AndroidX.Lifecycle.LiveData" Version="2.6.1.3" />
<PackageReference Include="Xamarin.Google.Android.Material" Version="1.9.0.2" />
<PackageReference Include="Xamarin.AndroidX.SwipeRefreshLayout" Version="1.1.0.14" />
Expand Down
2 changes: 1 addition & 1 deletion src/Core/tests/Benchmarks.Droid/Benchmarks.Droid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<Using Include="BenchmarkDotNet.Order" />
<Using Include="BenchmarkDotNet.Running" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.10" />
<PackageReference Include="Xamarin.Android.Glide" Version="4.14.2.1" />
<PackageReference Include="Xamarin.Android.Glide" Version="$(_XamarinAndroidGlideVersion)" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\Controls\src\Core\Controls.Core.csproj" />
Expand Down
25 changes: 23 additions & 2 deletions src/Core/tests/Benchmarks.Droid/ImageBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using Android.Content;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.OS;
using Bumptech.Glide;
using Bumptech.Glide.Request.Target;
using Bumptech.Glide.Request.Transition;
using Java.Lang;
using Microsoft.Maui.Storage;
using AImageView = Android.Widget.ImageView;
using Path = System.IO.Path;

namespace Benchmarks.Droid;

Expand All @@ -20,6 +20,7 @@ public class ImageBenchmark
Handler? handler;
Context? context;
string? imageFilename;
Typeface? defaultTypeface;

[GlobalSetup]
public void GlobalSetup()
Expand All @@ -29,6 +30,7 @@ public void GlobalSetup()
imageView = new AImageView(context);
glide = Glide.Get(context);
handler = new Handler(Looper.MainLooper!);
defaultTypeface = Typeface.Default;

var imageName = "dotnet_bot.png";
var cacheDir = FileSystem.CacheDirectory;
Expand Down Expand Up @@ -70,6 +72,25 @@ public async Task ImageHelperFromFile()
await callback.SuccessTask;
}

[Benchmark]
public async Task ImageHelperFromFont()
{
var callback = new Callback();

handler!.Post(() =>
{
Microsoft.Maui.PlatformInterop.LoadImageFromFont(
context,
Color.Aquamarine,
"A",
defaultTypeface,
24,
callback);
});

await callback.SuccessTask;
}

class Callback : Java.Lang.Object, Microsoft.Maui.IImageLoaderCallback
{
readonly TaskCompletionSource<Drawable?> tcsDrawable = new TaskCompletionSource<Drawable?>();
Expand Down

0 comments on commit ab55e36

Please sign in to comment.