aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.HLE/HOS/Services/SurfaceFlinger/HOSBinderDriverServer.cs
blob: bc7bffcb6376a9ab19c40f9c641022209b309510 (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
using Ryujinx.Common.Logging;
using Ryujinx.HLE.HOS.Kernel.Threading;
using System;
using System.Collections.Generic;

namespace Ryujinx.HLE.HOS.Services.SurfaceFlinger
{
    class HOSBinderDriverServer : IHOSBinderDriver
    {
        private static readonly Dictionary<int, IBinder> _registeredBinderObjects = new();

        private static int _lastBinderId = 0;

        private static readonly object _lock = new();

        public static int RegisterBinderObject(IBinder binder)
        {
            lock (_lock)
            {
                _lastBinderId++;

                _registeredBinderObjects.Add(_lastBinderId, binder);

                return _lastBinderId;
            }
        }

        public static void UnregisterBinderObject(int binderId)
        {
            lock (_lock)
            {
                _registeredBinderObjects.Remove(binderId);
            }
        }

        public static int GetBinderId(IBinder binder)
        {
            lock (_lock)
            {
                foreach (KeyValuePair<int, IBinder> pair in _registeredBinderObjects)
                {
                    if (ReferenceEquals(binder, pair.Value))
                    {
                        return pair.Key;
                    }
                }

                return -1;
            }
        }

        private static IBinder GetBinderObjectById(int binderId)
        {
            lock (_lock)
            {
                if (_registeredBinderObjects.TryGetValue(binderId, out IBinder binder))
                {
                    return binder;
                }

                return null;
            }
        }

        protected override ResultCode AdjustRefcount(int binderId, int addVal, int type)
        {
            IBinder binder = GetBinderObjectById(binderId);

            if (binder == null)
            {
                Logger.Error?.Print(LogClass.SurfaceFlinger, $"Invalid binder id {binderId}");

                return ResultCode.Success;
            }

            return binder.AdjustRefcount(addVal, type);
        }

        protected override void GetNativeHandle(int binderId, uint typeId, out KReadableEvent readableEvent)
        {
            IBinder binder = GetBinderObjectById(binderId);

            if (binder == null)
            {
                readableEvent = null;

                Logger.Error?.Print(LogClass.SurfaceFlinger, $"Invalid binder id {binderId}");

                return;
            }

            binder.GetNativeHandle(typeId, out readableEvent);
        }

        protected override ResultCode OnTransact(int binderId, uint code, uint flags, ReadOnlySpan<byte> inputParcel, Span<byte> outputParcel)
        {
            IBinder binder = GetBinderObjectById(binderId);

            if (binder == null)
            {
                Logger.Error?.Print(LogClass.SurfaceFlinger, $"Invalid binder id {binderId}");

                return ResultCode.Success;
            }

            return binder.OnTransact(code, flags, inputParcel, outputParcel);
        }
    }
}