CustomSkiaPage.cs 4.4 KB

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