CustomSkiaPage.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.Diagnostics;
  3. using System.Globalization;
  4. using System.Linq;
  5. using Avalonia;
  6. using Avalonia.Controls;
  7. using Avalonia.Media;
  8. using Avalonia.Platform;
  9. using Avalonia.Rendering.SceneGraph;
  10. using Avalonia.Skia;
  11. using Avalonia.Threading;
  12. using Avalonia.Utilities;
  13. using SkiaSharp;
  14. namespace RenderDemo.Pages
  15. {
  16. public class CustomSkiaPage : Control
  17. {
  18. private readonly GlyphRun _noSkia;
  19. public CustomSkiaPage()
  20. {
  21. ClipToBounds = true;
  22. var text = "Current rendering API is not Skia";
  23. var glyphs = text.Select(ch => Typeface.Default.GlyphTypeface.GetGlyph(ch)).ToArray();
  24. _noSkia = new GlyphRun(Typeface.Default.GlyphTypeface, 12, text.AsMemory(), glyphs);
  25. }
  26. class CustomDrawOp : ICustomDrawOperation
  27. {
  28. private readonly IImmutableGlyphRunReference _noSkia;
  29. public CustomDrawOp(Rect bounds, GlyphRun noSkia)
  30. {
  31. _noSkia = noSkia.TryCreateImmutableGlyphRunReference();
  32. Bounds = bounds;
  33. }
  34. public void Dispose()
  35. {
  36. // No-op
  37. }
  38. public Rect Bounds { get; }
  39. public bool HitTest(Point p) => false;
  40. public bool Equals(ICustomDrawOperation other) => false;
  41. static Stopwatch St = Stopwatch.StartNew();
  42. public void Render(ImmediateDrawingContext context)
  43. {
  44. var leaseFeature = context.TryGetFeature<ISkiaSharpApiLeaseFeature>();
  45. if (leaseFeature == null)
  46. context.DrawGlyphRun(Brushes.Black, _noSkia);
  47. else
  48. {
  49. using var lease = leaseFeature.Lease();
  50. var canvas = lease.SkCanvas;
  51. canvas.Save();
  52. // create the first shader
  53. var colors = new SKColor[] {
  54. new SKColor(0, 255, 255),
  55. new SKColor(255, 0, 255),
  56. new SKColor(255, 255, 0),
  57. new SKColor(0, 255, 255)
  58. };
  59. var sx = Animate(100, 2, 10);
  60. var sy = Animate(1000, 5, 15);
  61. var lightPosition = new SKPoint(
  62. (float)(Bounds.Width / 2 + Math.Cos(St.Elapsed.TotalSeconds) * Bounds.Width / 4),
  63. (float)(Bounds.Height / 2 + Math.Sin(St.Elapsed.TotalSeconds) * Bounds.Height / 4));
  64. using (var sweep =
  65. SKShader.CreateSweepGradient(new SKPoint((int)Bounds.Width / 2, (int)Bounds.Height / 2), colors,
  66. null))
  67. using(var turbulence = SKShader.CreatePerlinNoiseFractalNoise(0.05f, 0.05f, 4, 0))
  68. using(var shader = SKShader.CreateCompose(sweep, turbulence, SKBlendMode.SrcATop))
  69. using(var blur = SKImageFilter.CreateBlur(Animate(100, 2, 10), Animate(100, 5, 15)))
  70. using (var paint = new SKPaint
  71. {
  72. Shader = shader,
  73. ImageFilter = blur
  74. })
  75. canvas.DrawPaint(paint);
  76. using (var pseudoLight = SKShader.CreateRadialGradient(
  77. lightPosition,
  78. (float) (Bounds.Width/3),
  79. new [] {
  80. new SKColor(255, 200, 200, 100),
  81. SKColors.Transparent,
  82. new SKColor(40,40,40, 220),
  83. new SKColor(20,20,20, (byte)Animate(100, 200,220)) },
  84. new float[] { 0.3f, 0.3f, 0.8f, 1 },
  85. SKShaderTileMode.Clamp))
  86. using (var paint = new SKPaint
  87. {
  88. Shader = pseudoLight
  89. })
  90. canvas.DrawPaint(paint);
  91. canvas.Restore();
  92. }
  93. }
  94. static int Animate(int d, int from, int to)
  95. {
  96. var ms = (int)(St.ElapsedMilliseconds / d);
  97. var diff = to - from;
  98. var range = diff * 2;
  99. var v = ms % range;
  100. if (v > diff)
  101. v = range - v;
  102. var rv = v + from;
  103. if (rv < from || rv > to)
  104. throw new Exception("WTF");
  105. return rv;
  106. }
  107. }
  108. public override void Render(DrawingContext context)
  109. {
  110. context.Custom(new CustomDrawOp(new Rect(0, 0, Bounds.Width, Bounds.Height), _noSkia));
  111. Dispatcher.UIThread.InvokeAsync(InvalidateVisual, DispatcherPriority.Background);
  112. }
  113. }
  114. }