aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/HOS/Services/Vi/GbpBuffer.cs
blob: eb1adc7759e4674c67bb8e0587ff096b64728efe (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using Ryujinx.Common;
using System;
using System.IO;
using System.Runtime.InteropServices;

namespace Ryujinx.HLE.HOS.Services.Android
{
    [StructLayout(LayoutKind.Sequential, Size = 0x28)]
    struct GraphicBufferHeader
    {
        public int Magic;
        public int Width;
        public int Height;
        public int Stride;
        public int Format;
        public int Usage;

        public int Pid;
        public int RefCount;

        public int FdsCount;
        public int IntsCount;
    }

    [StructLayout(LayoutKind.Explicit, Size = 0x58)]
    struct NvGraphicBufferSurface
    {
        [FieldOffset(0)]
        public uint Width;

        [FieldOffset(0x4)]
        public uint Height;

        [FieldOffset(0x8)]
        public ColorFormat ColorFormat;

        [FieldOffset(0x10)]
        public int Layout;

        [FieldOffset(0x14)]
        public int Pitch;

        [FieldOffset(0x18)]
        public int NvMapHandle;

        [FieldOffset(0x1C)]
        public int Offset;

        [FieldOffset(0x20)]
        public int Kind;

        [FieldOffset(0x24)]
        public int BlockHeightLog2;

        [FieldOffset(0x28)]
        public int ScanFormat;

        [FieldOffset(0x30)]
        public long Flags;

        [FieldOffset(0x38)]
        public long Size;
    }

    [StructLayout(LayoutKind.Explicit)]
    struct NvGraphicBufferSurfaceArray
    {
        [FieldOffset(0x0)]
        private NvGraphicBufferSurface Surface0;

        [FieldOffset(0x58)]
        private NvGraphicBufferSurface Surface1;

        [FieldOffset(0xb0)]
        private NvGraphicBufferSurface Surface2;

        public NvGraphicBufferSurface this[int index]
        {
            get
            {
                if (index == 0)
                {
                    return Surface0;
                }
                else if (index == 1)
                {
                    return Surface1;
                }
                else if (index == 2)
                {
                    return Surface2;
                }

                throw new IndexOutOfRangeException();
            }
        }
    }

    [StructLayout(LayoutKind.Explicit, Size = 0x144)]
    struct NvGraphicBuffer
    {
        [FieldOffset(0x4)]
        public int NvMapId;

        [FieldOffset(0xC)]
        public int Magic;

        [FieldOffset(0x10)]
        public int Pid;

        [FieldOffset(0x14)]
        public int Type;

        [FieldOffset(0x18)]
        public int Usage;

        [FieldOffset(0x1C)]
        public int PixelFormat;

        [FieldOffset(0x20)]
        public int ExternalPixelFormat;

        [FieldOffset(0x24)]
        public int Stride;

        [FieldOffset(0x28)]
        public int FrameBufferSize;

        [FieldOffset(0x2C)]
        public int PlanesCount;

        [FieldOffset(0x34)]
        public NvGraphicBufferSurfaceArray Surfaces;
    }

    struct GbpBuffer
    {
        public GraphicBufferHeader Header { get; private set; }
        public NvGraphicBuffer     Buffer { get; private set; }

        public int Size => Marshal.SizeOf<NvGraphicBuffer>() + Marshal.SizeOf<GraphicBufferHeader>();

        public GbpBuffer(BinaryReader reader)
        {
            Header = reader.ReadStruct<GraphicBufferHeader>();

            // ignore fds
            // TODO: check if that is used in official implementation
            reader.BaseStream.Position += Header.FdsCount * 4;

            if (Header.IntsCount != 0x51)
            {
                throw new System.NotImplementedException($"Unexpected Graphic Buffer ints count (expected 0x51, found 0x{Header.IntsCount:x}");
            }

            Buffer = reader.ReadStruct<NvGraphicBuffer>();
        }

        public void Write(BinaryWriter writer)
        {
            writer.WriteStruct(Header);
            writer.WriteStruct(Buffer);
        }
    }
}