aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Core/OsHle/Objects/Vi/IHOSBinderDriver.cs
blob: cfd271e85e1f1095bebe6df1f623f3d158d4bbd7 (plain)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using ChocolArm64.Memory;
using Ryujinx.Core.OsHle.Handles;
using Ryujinx.Core.OsHle.Ipc;
using Ryujinx.Core.OsHle.Utilities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

using static Ryujinx.Core.OsHle.Objects.Android.Parcel;

namespace Ryujinx.Core.OsHle.Objects.Vi
{
    class IHOSBinderDriver : IIpcInterface
    {
        private delegate long ServiceProcessParcel(ServiceCtx Context, byte[] ParcelData);

        private Dictionary<int, ServiceProcessRequest> m_Commands;

        private Dictionary<(string, int), ServiceProcessParcel> m_Methods;

        public IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;       

        private class BufferObj
        {

        }

        private IdPoolWithObj BufferSlots;

        private byte[] Gbfr;

        public IHOSBinderDriver()
        {
            m_Commands = new Dictionary<int, ServiceProcessRequest>()
            {
                { 0, TransactParcel  },
                { 1, AdjustRefcount  },
                { 2, GetNativeHandle }
            };

            m_Methods = new Dictionary<(string, int), ServiceProcessParcel>()
            {
                { ("android.gui.IGraphicBufferProducer", 0x1), GraphicBufferProducerRequestBuffer },
                { ("android.gui.IGraphicBufferProducer", 0x3), GraphicBufferProducerDequeueBuffer },
                { ("android.gui.IGraphicBufferProducer", 0x7), GraphicBufferProducerQueueBuffer   },
                { ("android.gui.IGraphicBufferProducer", 0x8), GraphicBufferProducerCancelBuffer  },
                { ("android.gui.IGraphicBufferProducer", 0x9), GraphicBufferProducerQuery         },
                { ("android.gui.IGraphicBufferProducer", 0xa), GraphicBufferProducerConnect       },
                { ("android.gui.IGraphicBufferProducer", 0xe), GraphicBufferPreallocateBuffer     }
            };

            BufferSlots = new IdPoolWithObj();
        }

        public long TransactParcel(ServiceCtx Context)
        {
            int Id   = Context.RequestData.ReadInt32();
            int Code = Context.RequestData.ReadInt32();

            long DataPos  = Context.Request.SendBuff[0].Position;
            long DataSize = Context.Request.SendBuff[0].Size;

            byte[] Data = AMemoryHelper.ReadBytes(Context.Memory, DataPos, (int)DataSize);

            Data = GetParcelData(Data);

            using (MemoryStream MS = new MemoryStream(Data))
            {
                BinaryReader Reader = new BinaryReader(MS);

                MS.Seek(4, SeekOrigin.Current);

                int StrSize = Reader.ReadInt32();

                string InterfaceName = Encoding.Unicode.GetString(Data, 8, StrSize * 2);

                if (m_Methods.TryGetValue((InterfaceName, Code), out ServiceProcessParcel ProcReq))
                {
                    return ProcReq(Context, Data);
                }
                else
                {
                    throw new NotImplementedException($"{InterfaceName} {Code}");
                }
            }
        }

        private long GraphicBufferProducerRequestBuffer(ServiceCtx Context, byte[] ParcelData)
        {
            int GbfrSize = Gbfr?.Length ?? 0;

            byte[] Data = new byte[GbfrSize + 4];

            if (Gbfr != null)
            {
                Buffer.BlockCopy(Gbfr, 0, Data, 0, GbfrSize);
            }

            return MakeReplyParcel(Context, Data);
        }

        private long GraphicBufferProducerDequeueBuffer(ServiceCtx Context, byte[] ParcelData)
        {
            //Note: It seems that the maximum number of slots is 64, because if we return
            //a Slot number > 63, it seems to cause a buffer overrun and it reads garbage.
            //Note 2: The size of each object associated with the slot is 0x30.
            int Slot = BufferSlots.GenerateId(new BufferObj());

            return MakeReplyParcel(Context, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
        }

        private long GraphicBufferProducerQueueBuffer(ServiceCtx Context, byte[] ParcelData)
        {
            return MakeReplyParcel(Context, 1280, 720, 0, 0, 0);
        }

        private long GraphicBufferProducerCancelBuffer(ServiceCtx Context, byte[] ParcelData)
        {
            using (MemoryStream MS = new MemoryStream(ParcelData))
            {
                BinaryReader Reader = new BinaryReader(MS);

                MS.Seek(0x50, SeekOrigin.Begin);

                int Slot = Reader.ReadInt32();

                BufferSlots.Delete(Slot);

                return MakeReplyParcel(Context, 0);
            }
        }

        private long GraphicBufferProducerQuery(ServiceCtx Context, byte[] ParcelData)
        {
            return MakeReplyParcel(Context, 0, 0);
        }

        private long GraphicBufferProducerConnect(ServiceCtx Context, byte[] ParcelData)
        {
            return MakeReplyParcel(Context, 1280, 720, 0, 0, 0);
        }

        private long GraphicBufferPreallocateBuffer(ServiceCtx Context, byte[] ParcelData)
        {
            int GbfrSize = ParcelData.Length - 0x54;

            Gbfr = new byte[GbfrSize];

            Buffer.BlockCopy(ParcelData, 0x54, Gbfr, 0, GbfrSize);

            using (MemoryStream MS = new MemoryStream(ParcelData))
            {
                BinaryReader Reader = new BinaryReader(MS);

                MS.Seek(0xd4, SeekOrigin.Begin);

                int Handle = Reader.ReadInt32();

                HNvMap NvMap = Context.Ns.Os.Handles.GetData<HNvMap>(Handle);

                Context.Ns.Gpu.Renderer.FrameBufferPtr = NvMap.Address;
            }

            return MakeReplyParcel(Context, 0);
        }

        private long MakeReplyParcel(ServiceCtx Context, params int[] Ints)
        {
            using (MemoryStream MS = new MemoryStream())
            {
                BinaryWriter Writer = new BinaryWriter(MS);

                foreach (int Int in Ints)
                {
                    Writer.Write(Int);
                }

                return MakeReplyParcel(Context, MS.ToArray());
            }
        }

        private long MakeReplyParcel(ServiceCtx Context, byte[] Data)
        {
            long ReplyPos  = Context.Request.ReceiveBuff[0].Position;
            long ReplySize = Context.Request.ReceiveBuff[0].Position;

            byte[] Reply = MakeParcel(Data, new byte[0]);

            AMemoryHelper.WriteBytes(Context.Memory, ReplyPos, Reply);

            return 0;
        }

        public long AdjustRefcount(ServiceCtx Context)
        {
            int Id     = Context.RequestData.ReadInt32();
            int AddVal = Context.RequestData.ReadInt32();
            int Type   = Context.RequestData.ReadInt32();

            return 0;
        }

        public long GetNativeHandle(ServiceCtx Context)
        {
            int  Id  = Context.RequestData.ReadInt32();
            uint Unk = Context.RequestData.ReadUInt32();

            Context.Response.HandleDesc = IpcHandleDesc.MakeMove(0xbadcafe);

            return 0;
        }
    }
}