Surface.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using Ryujinx.Common.Memory;
  2. using Ryujinx.Graphics.Video;
  3. using System;
  4. using System.Runtime.InteropServices;
  5. namespace Ryujinx.Graphics.Nvdec.Vp9.Types
  6. {
  7. internal struct Surface : ISurface
  8. {
  9. public ArrayPtr<byte> YBuffer;
  10. public ArrayPtr<byte> UBuffer;
  11. public ArrayPtr<byte> VBuffer;
  12. public unsafe Plane YPlane => new Plane((IntPtr)YBuffer.ToPointer(), YBuffer.Length);
  13. public unsafe Plane UPlane => new Plane((IntPtr)UBuffer.ToPointer(), UBuffer.Length);
  14. public unsafe Plane VPlane => new Plane((IntPtr)VBuffer.ToPointer(), VBuffer.Length);
  15. public int Width { get; }
  16. public int Height { get; }
  17. public int AlignedWidth { get; }
  18. public int AlignedHeight { get; }
  19. public int Stride { get; }
  20. public int UvWidth { get; }
  21. public int UvHeight { get; }
  22. public int UvAlignedWidth { get; }
  23. public int UvAlignedHeight { get; }
  24. public int UvStride { get; }
  25. public bool HighBd => false;
  26. private readonly IntPtr _pointer;
  27. public Surface(int width, int height)
  28. {
  29. const int border = 32;
  30. const int ssX = 1;
  31. const int ssY = 1;
  32. const bool highbd = false;
  33. int alignedWidth = (width + 7) & ~7;
  34. int alignedHeight = (height + 7) & ~7;
  35. int yStride = ((alignedWidth + 2 * border) + 31) & ~31;
  36. int yplaneSize = (alignedHeight + 2 * border) * yStride;
  37. int uvWidth = alignedWidth >> ssX;
  38. int uvHeight = alignedHeight >> ssY;
  39. int uvStride = yStride >> ssX;
  40. int uvBorderW = border >> ssX;
  41. int uvBorderH = border >> ssY;
  42. int uvplaneSize = (uvHeight + 2 * uvBorderH) * uvStride;
  43. int frameSize = (highbd ? 2 : 1) * (yplaneSize + 2 * uvplaneSize);
  44. IntPtr pointer = Marshal.AllocHGlobal(frameSize);
  45. _pointer = pointer;
  46. Width = width;
  47. Height = height;
  48. AlignedWidth = alignedWidth;
  49. AlignedHeight = alignedHeight;
  50. Stride = yStride;
  51. UvWidth = (width + ssX) >> ssX;
  52. UvHeight = (height + ssY) >> ssY;
  53. UvAlignedWidth = uvWidth;
  54. UvAlignedHeight = uvHeight;
  55. UvStride = uvStride;
  56. ArrayPtr<byte> NewPlane(int start, int size, int border)
  57. {
  58. return new ArrayPtr<byte>(pointer + start + border, size - border);
  59. }
  60. YBuffer = NewPlane(0, yplaneSize, (border * yStride) + border);
  61. UBuffer = NewPlane(yplaneSize, uvplaneSize, (uvBorderH * uvStride) + uvBorderW);
  62. VBuffer = NewPlane(yplaneSize + uvplaneSize, uvplaneSize, (uvBorderH * uvStride) + uvBorderW);
  63. }
  64. public void Dispose()
  65. {
  66. Marshal.FreeHGlobal(_pointer);
  67. }
  68. }
  69. }