aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.HLE/HOS/Services/SurfaceFlinger/IGraphicBufferProducer.cs
blob: 3c01b92fbc2ede05f7de63f37883f8f0ebd23a19 (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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
using Ryujinx.Common.Logging;
using Ryujinx.HLE.HOS.Kernel.Threading;
using Ryujinx.HLE.HOS.Services.SurfaceFlinger.Types;
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace Ryujinx.HLE.HOS.Services.SurfaceFlinger
{
    abstract class IGraphicBufferProducer : IBinder
    {
        public string InterfaceToken => "android.gui.IGraphicBufferProducer";

        enum TransactionCode : uint
        {
            RequestBuffer = 1,
            SetBufferCount,
            DequeueBuffer,
            DetachBuffer,
            DetachNextBuffer,
            AttachBuffer,
            QueueBuffer,
            CancelBuffer,
            Query,
            Connect,
            Disconnect,
            SetSidebandStream,
            AllocateBuffers,
            SetPreallocatedBuffer,
            Reserved15,
            GetBufferInfo,
            GetBufferHistory,
        }

        [StructLayout(LayoutKind.Sequential, Pack = 1, Size = 0x54)]
        public struct QueueBufferInput : IFlattenable
        {
            public long Timestamp;
            public int IsAutoTimestamp;
            public Rect Crop;
            public NativeWindowScalingMode ScalingMode;
            public NativeWindowTransform Transform;
            public uint StickyTransform;
            public int Async;
            public int SwapInterval;
            public AndroidFence Fence;

            public void Flatten(Parcel parcel)
            {
                parcel.WriteUnmanagedType(ref this);
            }

            public readonly uint GetFdCount()
            {
                return 0;
            }

            public readonly uint GetFlattenedSize()
            {
                return (uint)Unsafe.SizeOf<QueueBufferInput>();
            }

            public void Unflatten(Parcel parcel)
            {
                this = parcel.ReadUnmanagedType<QueueBufferInput>();
            }
        }

        public struct QueueBufferOutput
        {
            public uint Width;
            public uint Height;
            public NativeWindowTransform TransformHint;
            public uint NumPendingBuffers;
            public ulong FrameNumber;

            public void WriteToParcel(Parcel parcel)
            {
                parcel.WriteUInt32(Width);
                parcel.WriteUInt32(Height);
                parcel.WriteUnmanagedType(ref TransformHint);
                parcel.WriteUInt32(NumPendingBuffers);

                if (TransformHint.HasFlag(NativeWindowTransform.ReturnFrameNumber))
                {
                    parcel.WriteUInt64(FrameNumber);
                }
            }
        }

        public ResultCode AdjustRefcount(int addVal, int type)
        {
            // TODO?
            return ResultCode.Success;
        }

        public void GetNativeHandle(uint typeId, out KReadableEvent readableEvent)
        {
            if (typeId == 0xF)
            {
                readableEvent = GetWaitBufferFreeEvent();
            }
            else
            {
                throw new NotImplementedException($"Unimplemented native event type {typeId}!");
            }
        }

        public void OnTransact(uint code, uint flags, Parcel inputParcel, Parcel outputParcel)
        {
            Status status = Status.Success;
            int slot;
            AndroidFence fence;
            QueueBufferInput queueInput;
            QueueBufferOutput queueOutput;
            NativeWindowApi api;

            AndroidStrongPointer<GraphicBuffer> graphicBuffer;
            AndroidStrongPointer<AndroidFence> strongFence;

            switch ((TransactionCode)code)
            {
                case TransactionCode.RequestBuffer:
                    slot = inputParcel.ReadInt32();

                    status = RequestBuffer(slot, out graphicBuffer);

                    outputParcel.WriteStrongPointer(ref graphicBuffer);

                    outputParcel.WriteStatus(status);

                    break;
                case TransactionCode.SetBufferCount:
                    int bufferCount = inputParcel.ReadInt32();

                    status = SetBufferCount(bufferCount);

                    outputParcel.WriteStatus(status);

                    break;
                case TransactionCode.DequeueBuffer:
                    bool async = inputParcel.ReadBoolean();
                    uint width = inputParcel.ReadUInt32();
                    uint height = inputParcel.ReadUInt32();
                    PixelFormat format = inputParcel.ReadUnmanagedType<PixelFormat>();
                    uint usage = inputParcel.ReadUInt32();

                    status = DequeueBuffer(out int dequeueSlot, out fence, async, width, height, format, usage);
                    strongFence = new AndroidStrongPointer<AndroidFence>(fence);

                    outputParcel.WriteInt32(dequeueSlot);
                    outputParcel.WriteStrongPointer(ref strongFence);

                    outputParcel.WriteStatus(status);

                    break;
                case TransactionCode.DetachBuffer:
                    slot = inputParcel.ReadInt32();

                    status = DetachBuffer(slot);

                    outputParcel.WriteStatus(status);

                    break;
                case TransactionCode.DetachNextBuffer:
                    status = DetachNextBuffer(out graphicBuffer, out fence);
                    strongFence = new AndroidStrongPointer<AndroidFence>(fence);

                    outputParcel.WriteStrongPointer(ref graphicBuffer);
                    outputParcel.WriteStrongPointer(ref strongFence);

                    outputParcel.WriteStatus(status);

                    break;
                case TransactionCode.AttachBuffer:
                    graphicBuffer = inputParcel.ReadStrongPointer<GraphicBuffer>();

                    status = AttachBuffer(out slot, graphicBuffer);

                    outputParcel.WriteInt32(slot);

                    outputParcel.WriteStatus(status);

                    break;
                case TransactionCode.QueueBuffer:
                    slot = inputParcel.ReadInt32();
                    queueInput = inputParcel.ReadFlattenable<QueueBufferInput>();

                    status = QueueBuffer(slot, ref queueInput, out queueOutput);

                    queueOutput.WriteToParcel(outputParcel);

                    outputParcel.WriteStatus(status);

                    break;
                case TransactionCode.CancelBuffer:
                    slot = inputParcel.ReadInt32();
                    fence = inputParcel.ReadFlattenable<AndroidFence>();

                    CancelBuffer(slot, ref fence);

                    outputParcel.WriteStatus(Status.Success);

                    break;
                case TransactionCode.Query:
                    NativeWindowAttribute what = inputParcel.ReadUnmanagedType<NativeWindowAttribute>();

                    status = Query(what, out int outValue);

                    outputParcel.WriteInt32(outValue);

                    outputParcel.WriteStatus(status);

                    break;
                case TransactionCode.Connect:
                    bool hasListener = inputParcel.ReadBoolean();

                    IProducerListener listener = null;

                    if (hasListener)
                    {
                        throw new NotImplementedException("Connect with a strong binder listener isn't implemented");
                    }

                    api = inputParcel.ReadUnmanagedType<NativeWindowApi>();

                    bool producerControlledByApp = inputParcel.ReadBoolean();

                    status = Connect(listener, api, producerControlledByApp, out queueOutput);

                    queueOutput.WriteToParcel(outputParcel);

                    outputParcel.WriteStatus(status);

                    break;
                case TransactionCode.Disconnect:
                    api = inputParcel.ReadUnmanagedType<NativeWindowApi>();

                    status = Disconnect(api);

                    outputParcel.WriteStatus(status);

                    break;
                case TransactionCode.SetPreallocatedBuffer:
                    slot = inputParcel.ReadInt32();

                    graphicBuffer = inputParcel.ReadStrongPointer<GraphicBuffer>();

                    status = SetPreallocatedBuffer(slot, graphicBuffer);

                    outputParcel.WriteStatus(status);

                    break;
                case TransactionCode.GetBufferHistory:
                    int bufferHistoryCount = inputParcel.ReadInt32();

                    status = GetBufferHistory(bufferHistoryCount, out Span<BufferInfo> bufferInfos);

                    outputParcel.WriteStatus(status);

                    outputParcel.WriteInt32(bufferInfos.Length);

                    outputParcel.WriteUnmanagedSpan<BufferInfo>(bufferInfos);

                    break;
                default:
                    throw new NotImplementedException($"Transaction {(TransactionCode)code} not implemented");
            }

            if (status != Status.Success)
            {
                Logger.Error?.Print(LogClass.SurfaceFlinger, $"Error returned by transaction {(TransactionCode)code}: {status}");
            }
        }

        protected abstract KReadableEvent GetWaitBufferFreeEvent();

        public abstract Status RequestBuffer(int slot, out AndroidStrongPointer<GraphicBuffer> graphicBuffer);

        public abstract Status SetBufferCount(int bufferCount);

        public abstract Status DequeueBuffer(out int slot, out AndroidFence fence, bool async, uint width, uint height, PixelFormat format, uint usage);

        public abstract Status DetachBuffer(int slot);

        public abstract Status DetachNextBuffer(out AndroidStrongPointer<GraphicBuffer> graphicBuffer, out AndroidFence fence);

        public abstract Status AttachBuffer(out int slot, AndroidStrongPointer<GraphicBuffer> graphicBuffer);

        public abstract Status QueueBuffer(int slot, ref QueueBufferInput input, out QueueBufferOutput output);

        public abstract void CancelBuffer(int slot, ref AndroidFence fence);

        public abstract Status Query(NativeWindowAttribute what, out int outValue);

        public abstract Status Connect(IProducerListener listener, NativeWindowApi api, bool producerControlledByApp, out QueueBufferOutput output);

        public abstract Status Disconnect(NativeWindowApi api);

        public abstract Status SetPreallocatedBuffer(int slot, AndroidStrongPointer<GraphicBuffer> graphicBuffer);

        public abstract Status GetBufferHistory(int bufferHistoryCount, out Span<BufferInfo> bufferInfos);
    }
}