SurfaceFramebuffer.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Avalonia.Controls.Platform.Surfaces;
  7. using Avalonia.Platform;
  8. using Cairo;
  9. using Gdk;
  10. namespace Avalonia.Gtk
  11. {
  12. class SurfaceFramebuffer : ILockedFramebuffer
  13. {
  14. private Drawable _drawable;
  15. private ImageSurface _surface;
  16. public SurfaceFramebuffer(int width, int height)
  17. {
  18. _surface = new ImageSurface(Cairo.Format.RGB24, width, height);
  19. }
  20. public void SetDrawable(Drawable drawable)
  21. {
  22. _drawable = drawable;
  23. _surface.Flush();
  24. }
  25. public void Deallocate()
  26. {
  27. _surface.Dispose();
  28. _surface = null;
  29. }
  30. public void Dispose()
  31. {
  32. using (var ctx = CairoHelper.Create(_drawable))
  33. {
  34. _surface.MarkDirty();
  35. ctx.SetSourceSurface(_surface, 0, 0);
  36. ctx.Paint();
  37. }
  38. _drawable = null;
  39. }
  40. public IntPtr Address => _surface.DataPtr;
  41. public int Width => _surface.Width;
  42. public int Height => _surface.Height;
  43. public int RowBytes => _surface.Stride;
  44. //TODO: Proper DPI detect
  45. public Size Dpi => new Size(96, 96);
  46. public PixelFormat Format => PixelFormat.Bgra8888;
  47. }
  48. }