ImageNode.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using Avalonia.Platform;
  2. using Avalonia.Utilities;
  3. using Avalonia.Media.Imaging;
  4. namespace Avalonia.Rendering.SceneGraph
  5. {
  6. /// <summary>
  7. /// A node in the scene graph which represents an image draw.
  8. /// </summary>
  9. internal class ImageNode : DrawOperationWithTransform
  10. {
  11. /// <summary>
  12. /// Initializes a new instance of the <see cref="ImageNode"/> class.
  13. /// </summary>
  14. /// <param name="transform">The transform.</param>
  15. /// <param name="source">The image to draw.</param>
  16. /// <param name="opacity">The draw opacity.</param>
  17. /// <param name="sourceRect">The source rect.</param>
  18. /// <param name="destRect">The destination rect.</param>
  19. /// <param name="bitmapInterpolationMode">The bitmap interpolation mode.</param>
  20. public ImageNode(Matrix transform, IRef<IBitmapImpl> source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode)
  21. : base(destRect, transform)
  22. {
  23. Source = source.Clone();
  24. Opacity = opacity;
  25. SourceRect = sourceRect;
  26. DestRect = destRect;
  27. BitmapInterpolationMode = bitmapInterpolationMode;
  28. SourceVersion = Source.Item.Version;
  29. }
  30. /// <summary>
  31. /// Gets the image to draw.
  32. /// </summary>
  33. public IRef<IBitmapImpl> Source { get; }
  34. /// <summary>
  35. /// Source bitmap Version
  36. /// </summary>
  37. public int SourceVersion { get; }
  38. /// <summary>
  39. /// Gets the draw opacity.
  40. /// </summary>
  41. public double Opacity { get; }
  42. /// <summary>
  43. /// Gets the source rect.
  44. /// </summary>
  45. public Rect SourceRect { get; }
  46. /// <summary>
  47. /// Gets the destination rect.
  48. /// </summary>
  49. public Rect DestRect { get; }
  50. /// <summary>
  51. /// Gets the bitmap interpolation mode.
  52. /// </summary>
  53. /// <value>
  54. /// The scaling mode.
  55. /// </value>
  56. public BitmapInterpolationMode BitmapInterpolationMode { get; }
  57. /// <summary>
  58. /// Determines if this draw operation equals another.
  59. /// </summary>
  60. /// <param name="transform">The transform of the other draw operation.</param>
  61. /// <param name="source">The image of the other draw operation.</param>
  62. /// <param name="opacity">The opacity of the other draw operation.</param>
  63. /// <param name="sourceRect">The source rect of the other draw operation.</param>
  64. /// <param name="destRect">The dest rect of the other draw operation.</param>
  65. /// <param name="bitmapInterpolationMode">The bitmap interpolation mode.</param>
  66. /// <returns>True if the draw operations are the same, otherwise false.</returns>
  67. /// <remarks>
  68. /// The properties of the other draw operation are passed in as arguments to prevent
  69. /// allocation of a not-yet-constructed draw operation object.
  70. /// </remarks>
  71. public bool Equals(Matrix transform, IRef<IBitmapImpl> source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode)
  72. {
  73. return transform == Transform &&
  74. Equals(source.Item, Source.Item) &&
  75. source.Item.Version == SourceVersion &&
  76. opacity == Opacity &&
  77. sourceRect == SourceRect &&
  78. destRect == DestRect &&
  79. bitmapInterpolationMode == BitmapInterpolationMode;
  80. }
  81. /// <inheritdoc/>
  82. public override void Render(IDrawingContextImpl context)
  83. {
  84. context.DrawBitmap(Source, Opacity, SourceRect, DestRect, BitmapInterpolationMode);
  85. }
  86. /// <inheritdoc/>
  87. public override bool HitTestTransformed(Point p) => DestRect.ContainsExclusive(p);
  88. public override void Dispose()
  89. {
  90. Source?.Dispose();
  91. }
  92. }
  93. }