OpenGlTextureBitmap.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System;
  2. using Avalonia.Media;
  3. using Avalonia.Media.Imaging;
  4. using Avalonia.Platform;
  5. using Avalonia.Threading;
  6. namespace Avalonia.OpenGL.Imaging
  7. {
  8. public class OpenGlTextureBitmap : Bitmap, IAffectsRender
  9. {
  10. private IOpenGlTextureBitmapImpl _impl;
  11. static IOpenGlTextureBitmapImpl CreateOrThrow()
  12. {
  13. if (!(AvaloniaLocator.Current.GetService<IPlatformRenderInterface>() is IOpenGlAwarePlatformRenderInterface
  14. glAware))
  15. throw new PlatformNotSupportedException("Rendering platform does not support OpenGL integration");
  16. return glAware.CreateOpenGlTextureBitmap();
  17. }
  18. public OpenGlTextureBitmap()
  19. : base(CreateOrThrow())
  20. {
  21. _impl = (IOpenGlTextureBitmapImpl)PlatformImpl.Item;
  22. }
  23. public IDisposable Lock() => _impl.Lock();
  24. public void SetTexture(int textureId, int internalFormat, PixelSize size, double dpiScaling)
  25. {
  26. _impl.SetBackBuffer(textureId, internalFormat, size, dpiScaling);
  27. SetIsDirty();
  28. }
  29. public void SetIsDirty()
  30. {
  31. if (Dispatcher.UIThread.CheckAccess())
  32. CallInvalidated();
  33. else
  34. Dispatcher.UIThread.Post(CallInvalidated);
  35. }
  36. private void CallInvalidated() => Invalidated?.Invoke(this, EventArgs.Empty);
  37. public event EventHandler Invalidated;
  38. }
  39. }