From fec8e3489d5719d0621b34ddfb9992506d7cf0f1 Mon Sep 17 00:00:00 2001
From: jduncanator <1518948+jduncanator@users.noreply.github.com>
Date: Fri, 15 Feb 2019 15:47:40 +1100
Subject: [PATCH] Vi: Implement ConvertScalingMode (#581)

* Vi: Implement ConvertScalingMode

For now, it seems Nintendo just map 1:1 between the two enums.

* Resolve style nits

* Vi: Correct mapping order
---
 .../Services/Vi/IApplicationDisplayService.cs | 31 +++++++++++++++++++
 Ryujinx.HLE/HOS/Services/Vi/ScalingMode.cs    | 24 ++++++++++++++
 2 files changed, 55 insertions(+)
 create mode 100644 Ryujinx.HLE/HOS/Services/Vi/ScalingMode.cs

diff --git a/Ryujinx.HLE/HOS/Services/Vi/IApplicationDisplayService.cs b/Ryujinx.HLE/HOS/Services/Vi/IApplicationDisplayService.cs
index 039cc81f40..b272e0788d 100644
--- a/Ryujinx.HLE/HOS/Services/Vi/IApplicationDisplayService.cs
+++ b/Ryujinx.HLE/HOS/Services/Vi/IApplicationDisplayService.cs
@@ -6,6 +6,7 @@ using System;
 using System.IO;
 using System.Text;
 
+using static Ryujinx.HLE.HOS.ErrorCode;
 using static Ryujinx.HLE.HOS.Services.Android.Parcel;
 
 namespace Ryujinx.HLE.HOS.Services.Vi
@@ -35,6 +36,7 @@ namespace Ryujinx.HLE.HOS.Services.Vi
                 { 2030, CreateStrayLayer                     },
                 { 2031, DestroyStrayLayer                    },
                 { 2101, SetLayerScalingMode                  },
+                { 2102, ConvertScalingMode                   },
                 { 5202, GetDisplayVSyncEvent                 }
             };
 
@@ -176,6 +178,35 @@ namespace Ryujinx.HLE.HOS.Services.Vi
             return 0;
         }
 
+        public long ConvertScalingMode(ServiceCtx context)
+        {
+            SrcScalingMode  scalingMode     = (SrcScalingMode)context.RequestData.ReadInt32();
+            DstScalingMode? destScalingMode = ConvetScalingModeImpl(scalingMode);
+
+            if (!destScalingMode.HasValue)
+            {
+                return MakeError(ErrorModule.Vi, 1);
+            }
+
+            context.ResponseData.Write((ulong)destScalingMode);
+
+            return 0;
+        }
+
+        private DstScalingMode? ConvetScalingModeImpl(SrcScalingMode srcScalingMode)
+        {
+            switch (srcScalingMode)
+            {
+                case SrcScalingMode.None:                return DstScalingMode.None;
+                case SrcScalingMode.Freeze:              return DstScalingMode.Freeze;
+                case SrcScalingMode.ScaleAndCrop:        return DstScalingMode.ScaleAndCrop;
+                case SrcScalingMode.ScaleToWindow:       return DstScalingMode.ScaleToWindow;
+                case SrcScalingMode.PreserveAspectRatio: return DstScalingMode.PreserveAspectRatio;
+            }
+
+            return null;
+        }
+
         public long GetDisplayVSyncEvent(ServiceCtx context)
         {
             string name = GetDisplayName(context);
diff --git a/Ryujinx.HLE/HOS/Services/Vi/ScalingMode.cs b/Ryujinx.HLE/HOS/Services/Vi/ScalingMode.cs
new file mode 100644
index 0000000000..824a27b70a
--- /dev/null
+++ b/Ryujinx.HLE/HOS/Services/Vi/ScalingMode.cs
@@ -0,0 +1,24 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace Ryujinx.HLE.HOS.Services.Vi
+{
+    enum SrcScalingMode
+    {
+        Freeze = 0,
+        ScaleToWindow = 1,
+        ScaleAndCrop = 2,
+        None = 3,
+        PreserveAspectRatio = 4
+    }
+
+    enum DstScalingMode
+    {
+        None = 0,
+        Freeze = 1,
+        ScaleToWindow = 2,
+        ScaleAndCrop = 3,
+        PreserveAspectRatio = 4
+    }
+}