ARCoreSessionSubsystem.cs 18.8 KB
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 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
using AOT;
using System;
using System.Runtime.InteropServices;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine.Assertions;
using UnityEngine.Scripting;
using UnityEngine.XR.ARSubsystems;

namespace UnityEngine.XR.ARCore
{
    /// <summary>
    /// The ARCore implementation of the
    /// [XRSessionSubsystem](xref:UnityEngine.XR.ARSubsystems.XRSessionSubsystem).
    /// Do not create this directly. Use the
    /// [SubsystemManager](xref:UnityEngine.SubsystemManager)
    /// instead.
    /// </summary>
    [Preserve]
    public sealed class ARCoreSessionSubsystem : XRSessionSubsystem
    {
#if UNITY_2020_2_OR_NEWER
        protected override void OnCreate()
        {
            ((ARCoreProvider)provider).beforeSetConfiguration += ConfigurationChangedFromProvider;
        }
#endif

        void ConfigurationChangedFromProvider(ARCoreBeforeSetConfigurationEventArgs eventArgs) => beforeSetConfiguration?.Invoke(eventArgs);

        /// <summary>
        /// Notifies that there has been a change in the ARCore configuration object which triggers <see cref="beforeSetConfiguration"/>.
        /// </summary>
        public void SetConfigurationDirty()
        {
            NativeApi.UnityARCore_session_setConfigurationDirty();
        }

        /// <summary>
        /// Event that is triggered right before the configuration is set on the session. Allows changes to be made to the configuration before it is set.
        /// </summary>
        public event Action<ARCoreBeforeSetConfigurationEventArgs> beforeSetConfiguration;

#if !UNITY_2020_2_OR_NEWER
        /// <summary>
        /// Creates the provider interface.
        /// </summary>
        /// <returns>The provider interface for ARCore</returns>
        protected override Provider CreateProvider()
        {
            var provider = new ARCoreProvider(this);
            provider.beforeSetConfiguration += ConfigurationChangedFromProvider;
            return provider;
        }
#endif

        class ARCoreProvider : Provider
        {
            GCHandle m_ProviderHandle;
            Action<ArSession, ArConfig, IntPtr> m_SetConfigurationCallback = SetConfigurationCallback;

#if UNITY_2020_2_OR_NEWER
            public ARCoreProvider()
#else
            ARCoreSessionSubsystem m_Subsystem;
            public ARCoreProvider(ARCoreSessionSubsystem subsystem)
#endif
            {
#if !UNITY_2020_2_OR_NEWER
                m_Subsystem = subsystem;
#endif

                NativeApi.UnityARCore_session_construct(CameraPermissionRequestProvider);
                if (SystemInfo.graphicsMultiThreaded)
                {
                    m_RenderEventFunc = NativeApi.UnityARCore_session_getRenderEventFunc();
                }

                m_ProviderHandle = GCHandle.Alloc(this);
                NativeApi.UnityARCore_session_setConfigCallback(m_SetConfigurationCallback, GCHandle.ToIntPtr(m_ProviderHandle));
            }

#if UNITY_2020_2_OR_NEWER
            public override void Start()
#else
            public override void Resume()
#endif
            {
                // Texture *must* be created before ARCore session resume is called
                CreateTexture();
                NativeApi.UnityARCore_session_resume(Guid.NewGuid());
            }

#if UNITY_2020_2_OR_NEWER
            public override void Stop()
#else
            public override void Pause()
#endif
                => NativeApi.UnityARCore_session_pause();

            public override void Update(XRSessionUpdateParams updateParams, Configuration configuration)
            {
                NativeApi.UnityARCore_session_update(
                    updateParams.screenOrientation,
                    updateParams.screenDimensions,
                    configuration.descriptor.identifier,
                    configuration.features);
            }

            public override unsafe NativeArray<ConfigurationDescriptor> GetConfigurationDescriptors(Allocator allocator)
            {
                NativeApi.UnityARCore_session_getConfigurationDescriptors(out IntPtr ptr, out int count, out int stride);
                Assert.AreNotEqual(IntPtr.Zero, ptr, "Configuration descriptors pointer was null.");
                Assert.IsTrue(count > 0, "There are no configuration descriptors.");

                var descriptors = new NativeArray<ConfigurationDescriptor>(count, allocator);
                unsafe
                {
                    UnsafeUtility.MemCpyStride(
                        descriptors.GetUnsafePtr(),
                        UnsafeUtility.SizeOf<ConfigurationDescriptor>(),
                        (void*)ptr, stride, stride, count);
                }

                return descriptors;
            }

            /// <summary>
            /// Event that is triggered right before the configuration is set on the session. Allows changes to be made to the configuration before it is set.
            /// </summary>
            public event Action<ARCoreBeforeSetConfigurationEventArgs> beforeSetConfiguration;

            [MonoPInvokeCallback(typeof(Action<ArSession, ArConfig, IntPtr>))]
            static void SetConfigurationCallback(ArSession session, ArConfig config, IntPtr context)
            {
                var providerHandle = (GCHandle)context;

                if (providerHandle.Target is ARCoreProvider provider)
                {
                    provider.beforeSetConfiguration?.Invoke(new ARCoreBeforeSetConfigurationEventArgs(session, config));
                }
            }

            public override void Destroy()
            {
                m_ProviderHandle.Free();
                DeleteTexture();
                NativeApi.UnityARCore_session_destroy();
            }

            public override void Reset()
            {
                NativeApi.UnityARCore_session_reset();
#if UNITY_2020_2_OR_NEWER
                if (running)
                    Start();
#else
                if (m_Subsystem.running)
                    Resume();
#endif
            }

            public override void OnApplicationPause() => NativeApi.UnityARCore_session_onApplicationPause();

            public override void OnApplicationResume() => NativeApi.UnityARCore_session_onApplicationResume();

            public override Promise<SessionAvailability> GetAvailabilityAsync()
            {
                return ExecuteAsync<SessionAvailability>((context) =>
                {
                    NativeApi.ArPresto_checkApkAvailability(OnCheckApkAvailability, context);
                });
            }

            public override Promise<SessionInstallationStatus> InstallAsync()
            {
                return ExecuteAsync<SessionInstallationStatus>((context) =>
                {
                    NativeApi.ArPresto_requestApkInstallation(true, OnApkInstallation, context);
                });
            }

            public override IntPtr nativePtr => NativeApi.UnityARCore_session_getNativePtr();

            public override TrackingState trackingState => NativeApi.UnityARCore_session_getTrackingState();

            public override NotTrackingReason notTrackingReason => NativeApi.UnityARCore_session_getNotTrackingReason();

            public override Feature requestedFeatures => Api.GetRequestedFeatures();

            public override Feature requestedTrackingMode
            {
                get => Api.GetRequestedFeatures();
                set
                {
                    Api.SetFeatureRequested(Feature.AnyTrackingMode, false);
                    Api.SetFeatureRequested(value, true);
                }
            }

            public override Feature currentTrackingMode => NativeApi.GetCurrentTrackingMode();

            public override bool matchFrameRateEnabled => NativeApi.UnityARCore_session_getMatchFrameRateEnabled();

            public override bool matchFrameRateRequested
            {
                get => NativeApi.UnityARCore_session_getMatchFrameRateRequested();
                set => NativeApi.UnityARCore_session_setMatchFrameRateRequested(value);
            }

            public override int frameRate
            {
                get
                {
                    if (ARCoreCameraSubsystem.TryGetCurrentConfiguration(out XRCameraConfiguration configuration) && configuration.framerate.HasValue)
                    {
                        return configuration.framerate.Value;
                    }

                    return 30;
                }
            }

            static Promise<T> ExecuteAsync<T>(Action<IntPtr> apiMethod)
            {
                var promise = new ARCorePromise<T>();
                GCHandle gch = GCHandle.Alloc(promise);
                apiMethod(GCHandle.ToIntPtr(gch));
                return promise;
            }

            [MonoPInvokeCallback(typeof(Action<NativeApi.ArPrestoApkInstallStatus, IntPtr>))]
            static void OnApkInstallation(NativeApi.ArPrestoApkInstallStatus status, IntPtr context)
            {
                var sessionInstallation = SessionInstallationStatus.None;
                switch (status)
                {
                    case NativeApi.ArPrestoApkInstallStatus.ErrorDeviceNotCompatible:
                        sessionInstallation = SessionInstallationStatus.ErrorDeviceNotCompatible;
                        break;

                    case NativeApi.ArPrestoApkInstallStatus.ErrorUserDeclined:
                        sessionInstallation = SessionInstallationStatus.ErrorUserDeclined;
                        break;

                    case NativeApi.ArPrestoApkInstallStatus.Requested:
                        // This shouldn't happen
                        sessionInstallation = SessionInstallationStatus.Error;
                        break;

                    case NativeApi.ArPrestoApkInstallStatus.Success:
                        sessionInstallation = SessionInstallationStatus.Success;
                        break;

                    case NativeApi.ArPrestoApkInstallStatus.Error:
                    default:
                        sessionInstallation = SessionInstallationStatus.Error;
                        break;
                }

                ResolvePromise(context, sessionInstallation);
            }

            [MonoPInvokeCallback(typeof(Action<NativeApi.ArAvailability, IntPtr>))]
            static void OnCheckApkAvailability(NativeApi.ArAvailability availability, IntPtr context)
            {
                var sessionAvailability = SessionAvailability.None;
                switch (availability)
                {
                    case NativeApi.ArAvailability.SupportedNotInstalled:
                    case NativeApi.ArAvailability.SupportedApkTooOld:
                        sessionAvailability = SessionAvailability.Supported;
                        break;

                    case NativeApi.ArAvailability.SupportedInstalled:
                        sessionAvailability = SessionAvailability.Supported | SessionAvailability.Installed;
                        break;

                    default:
                        sessionAvailability = SessionAvailability.None;
                        break;
                }

                ResolvePromise(context, sessionAvailability);
            }

            [MonoPInvokeCallback(typeof(NativeApi.CameraPermissionRequestProviderDelegate))]
            static void CameraPermissionRequestProvider(NativeApi.CameraPermissionsResultCallbackDelegate callback, IntPtr context)
            {
                ARCorePermissionManager.RequestPermission(k_CameraPermissionName, (permissinName, granted) =>
                {
                    callback(granted, context);
                });
            }

            static void ResolvePromise<T>(IntPtr context, T arg) where T : struct
            {
                GCHandle gch = GCHandle.FromIntPtr(context);
                var promise = (ARCorePromise<T>)gch.Target;
                if (promise != null)
                    promise.Resolve(arg);
                gch.Free();
            }

            void IssueRenderEventAndWaitForCompletion(NativeApi.RenderEvent renderEvent)
            {
                // NB: If m_RenderEventFunc is zero, it means
                //     1. We are running in the Editor.
                //     2. The UnityARCore library could not be loaded or similar catastrophic failure.
                if (m_RenderEventFunc != IntPtr.Zero)
                {
                    NativeApi.UnityARCore_session_setRenderEventPending();
                    GL.IssuePluginEvent(m_RenderEventFunc, (int)renderEvent);
                    NativeApi.UnityARCore_session_waitForRenderEvent();
                }
            }

            // Safe to call multiple times; does nothing if already created.
            void CreateTexture()
            {
                if (SystemInfo.graphicsMultiThreaded)
                {
                    IssueRenderEventAndWaitForCompletion(NativeApi.RenderEvent.CreateTexture);
                }
                else
                {
                    NativeApi.UnityARCore_session_createTextureMainThread();
                }
            }

            // Safe to call multiple times; does nothing if already destroyed.
            void DeleteTexture()
            {
                if (SystemInfo.graphicsMultiThreaded)
                {
                    IssueRenderEventAndWaitForCompletion(NativeApi.RenderEvent.DeleteTexture);
                }
                else
                {
                    NativeApi.UnityARCore_session_deleteTextureMainThread();
                }
            }

            const string k_CameraPermissionName = "android.permission.CAMERA";

            IntPtr m_RenderEventFunc;
        }

        [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
        static void RegisterDescriptor()
        {
            if (!Api.platformAndroid || !Api.loaderPresent)
                return;

            XRSessionSubsystemDescriptor.RegisterDescriptor(new XRSessionSubsystemDescriptor.Cinfo
            {
                id = "ARCore-Session",
#if UNITY_2020_2_OR_NEWER
                providerType = typeof(ARCoreProvider),
                subsystemTypeOverride = typeof(ARCoreSessionSubsystem),
#else
                subsystemImplementationType = typeof(ARCoreSessionSubsystem),
#endif
                supportsInstall = true,
                supportsMatchFrameRate = true
            });
        }

        internal static class NativeApi
        {
            public enum ArPrestoApkInstallStatus
            {
                Uninitialized = 0,
                Requested = 1,
                Success = 100,
                Error = 200,
                ErrorDeviceNotCompatible = 201,
                ErrorUserDeclined = 203,
            }

            public enum ArAvailability
            {
                UnknownError = 0,
                UnknownChecking = 1,
                UnknownTimedOut = 2,
                UnsupportedDeviceNotCapable = 100,
                SupportedNotInstalled = 201,
                SupportedApkTooOld = 202,
                SupportedInstalled = 203
            }

            public enum RenderEvent
            {
                CreateTexture,
                DeleteTexture
            }

            public delegate void CameraPermissionRequestProviderDelegate(
                CameraPermissionsResultCallbackDelegate resultCallback,
                IntPtr context);

            public delegate void CameraPermissionsResultCallbackDelegate(
                bool granted,
                IntPtr context);

            [DllImport("UnityARCore")]
            public static extern IntPtr UnityARCore_session_getNativePtr();

            [DllImport("UnityARCore")]
            public static extern void ArPresto_checkApkAvailability(
                Action<ArAvailability, IntPtr> onResult, IntPtr context);

            [DllImport("UnityARCore")]
            public static extern void ArPresto_requestApkInstallation(
                bool userRequested, Action<ArPrestoApkInstallStatus, IntPtr> onResult, IntPtr context);

            [DllImport("UnityARCore")]
            public static extern void UnityARCore_session_update(
                ScreenOrientation orientation,
                Vector2Int screenDimensions,
                IntPtr configId,
                Feature features);

            [DllImport("UnityARCore")]
            public static extern void UnityARCore_session_getConfigurationDescriptors(
                out IntPtr ptr, out int count, out int stride);

            [DllImport("UnityARCore")]
            public static extern void UnityARCore_session_construct(
                CameraPermissionRequestProviderDelegate cameraPermissionRequestProvider);

            [DllImport("UnityARCore")]
            public static extern void UnityARCore_session_destroy();

            [DllImport("UnityARCore")]
            public static extern void UnityARCore_session_resume(Guid guid);

            [DllImport("UnityARCore")]
            public static extern void UnityARCore_session_pause();

            [DllImport("UnityARCore")]
            public static extern void UnityARCore_session_onApplicationResume();

            [DllImport("UnityARCore")]
            public static extern void UnityARCore_session_onApplicationPause();

            [DllImport("UnityARCore")]
            public static extern void UnityARCore_session_reset();

            [DllImport("UnityARCore")]
            public static extern TrackingState UnityARCore_session_getTrackingState();

            [DllImport("UnityARCore")]
            public static extern NotTrackingReason UnityARCore_session_getNotTrackingReason();

            [DllImport("UnityARCore")]
            public static extern IntPtr UnityARCore_session_getRenderEventFunc();

            [DllImport("UnityARCore")]
            public static extern void UnityARCore_session_setRenderEventPending();

            [DllImport("UnityARCore")]
            public static extern void UnityARCore_session_waitForRenderEvent();

            [DllImport("UnityARCore")]
            public static extern void UnityARCore_session_createTextureMainThread();

            [DllImport("UnityARCore")]
            public static extern void UnityARCore_session_deleteTextureMainThread();

            [DllImport("UnityARCore")]
            public static extern bool UnityARCore_session_getMatchFrameRateEnabled();

            [DllImport("UnityARCore")]
            public static extern bool UnityARCore_session_getMatchFrameRateRequested();

            [DllImport("UnityARCore")]
            public static extern void UnityARCore_session_setMatchFrameRateRequested(bool value);

            [DllImport("UnityARCore", EntryPoint="UnityARCore_session_getCurrentTrackingMode")]
            public static extern Feature GetCurrentTrackingMode();

            [DllImport("UnityARCore")]
            public static extern void UnityARCore_session_setConfigurationDirty();

            [DllImport("UnityARCore")]
            public static extern void UnityARCore_session_setConfigCallback(Action<ArSession, ArConfig, IntPtr> callback, IntPtr context);
        }
    }
}