Surface.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 FrameField Field => FrameField.Progressive;
  16. public int Width { get; }
  17. public int Height { get; }
  18. public int AlignedWidth { get; }
  19. public int AlignedHeight { get; }
  20. public int Stride { get; }
  21. public int UvWidth { get; }
  22. public int UvHeight { get; }
  23. public int UvAlignedWidth { get; }
  24. public int UvAlignedHeight { get; }
  25. public int UvStride { get; }
  26. public bool HighBd => false;
  27. private readonly IntPtr _pointer;
  28. public Surface(int width, int height)
  29. {
  30. const int border = 32;
  31. const int ssX = 1;
  32. const int ssY = 1;
  33. const bool highbd = false;
  34. int alignedWidth = (width + 7) & ~7;
  35. int alignedHeight = (height + 7) & ~7;
  36. int yStride = ((alignedWidth + 2 * border) + 31) & ~31;
  37. int yplaneSize = (alignedHeight + 2 * border) * yStride;
  38. int uvWidth = alignedWidth >> ssX;
  39. int uvHeight = alignedHeight >> ssY;
  40. int uvStride = yStride >> ssX;
  41. int uvBorderW = border >> ssX;
  42. int uvBorderH = border >> ssY;
  43. int uvplaneSize = (uvHeight + 2 * uvBorderH) * uvStride;
  44. int frameSize = (highbd ? 2 : 1) * (yplaneSize + 2 * uvplaneSize);
  45. IntPtr pointer = Marshal.AllocHGlobal(frameSize);
  46. _pointer = pointer;
  47. Width = width;
  48. Height = height;
  49. AlignedWidth = alignedWidth;
  50. AlignedHeight = alignedHeight;
  51. Stride = yStride;
  52. UvWidth = (width + ssX) >> ssX;
  53. UvHeight = (height + ssY) >> ssY;
  54. UvAlignedWidth = uvWidth;
  55. UvAlignedHeight = uvHeight;
  56. UvStride = uvStride;
  57. ArrayPtr<byte> NewPlane(int start, int size, int border)
  58. {
  59. return new ArrayPtr<byte>(pointer + start + border, size - border);
  60. }
  61. YBuffer = NewPlane(0, yplaneSize, (border * yStride) + border);
  62. UBuffer = NewPlane(yplaneSize, uvplaneSize, (uvBorderH * uvStride) + uvBorderW);
  63. VBuffer = NewPlane(yplaneSize + uvplaneSize, uvplaneSize, (uvBorderH * uvStride) + uvBorderW);
  64. }
  65. public void Dispose()
  66. {
  67. Marshal.FreeHGlobal(_pointer);
  68. }
  69. }
  70. }