OVRCommon.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. /************************************************************************************
  2. Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
  3. Licensed under the Oculus Utilities SDK License Version 1.31 (the "License"); you may not use
  4. the Utilities SDK except in compliance with the License, which is provided at the time of installation
  5. or download, or which otherwise accompanies this software in either electronic or hard copy form.
  6. You may obtain a copy of the License at
  7. https://developer.oculus.com/licenses/utilities-1.31
  8. Unless required by applicable law or agreed to in writing, the Utilities SDK distributed
  9. under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  10. ANY KIND, either express or implied. See the License for the specific language governing
  11. permissions and limitations under the License.
  12. ************************************************************************************/
  13. #if USING_XR_MANAGEMENT && USING_XR_SDK_OCULUS
  14. #define USING_XR_SDK
  15. #endif
  16. using UnityEngine;
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Runtime.InteropServices;
  20. #if USING_XR_SDK
  21. using UnityEngine.XR;
  22. using UnityEngine.Experimental.XR;
  23. #endif
  24. #if UNITY_2017_2_OR_NEWER
  25. using InputTracking = UnityEngine.XR.InputTracking;
  26. using Node = UnityEngine.XR.XRNode;
  27. using NodeState = UnityEngine.XR.XRNodeState;
  28. using Device = UnityEngine.XR.XRDevice;
  29. #elif UNITY_2017_1_OR_NEWER
  30. using InputTracking = UnityEngine.VR.InputTracking;
  31. using Node = UnityEngine.VR.VRNode;
  32. using NodeState = UnityEngine.VR.VRNodeState;
  33. using Device = UnityEngine.VR.VRDevice;
  34. #else
  35. using InputTracking = UnityEngine.VR.InputTracking;
  36. using Node = UnityEngine.VR.VRNode;
  37. using Device = UnityEngine.VR.VRDevice;
  38. #endif
  39. /// <summary>
  40. /// Miscellaneous extension methods that any script can use.
  41. /// </summary>
  42. public static class OVRExtensions
  43. {
  44. /// <summary>
  45. /// Converts the given world-space transform to an OVRPose in tracking space.
  46. /// </summary>
  47. public static OVRPose ToTrackingSpacePose(this Transform transform, Camera camera)
  48. {
  49. //Initializing to identity, but for all Oculus headsets, down below the pose will be initialized to the runtime's pose value, so identity will never be returned.
  50. OVRPose headPose = OVRPose.identity;
  51. Vector3 pos;
  52. Quaternion rot;
  53. if (OVRNodeStateProperties.GetNodeStatePropertyVector3(Node.Head, NodeStatePropertyType.Position, OVRPlugin.Node.Head, OVRPlugin.Step.Render, out pos))
  54. headPose.position = pos;
  55. if (OVRNodeStateProperties.GetNodeStatePropertyQuaternion(Node.Head, NodeStatePropertyType.Orientation, OVRPlugin.Node.Head, OVRPlugin.Step.Render, out rot))
  56. headPose.orientation = rot;
  57. var ret = headPose * transform.ToHeadSpacePose(camera);
  58. return ret;
  59. }
  60. /// <summary>
  61. /// Converts the given pose from tracking-space to world-space.
  62. /// </summary>
  63. public static OVRPose ToWorldSpacePose(OVRPose trackingSpacePose)
  64. {
  65. OVRPose headPose = OVRPose.identity;
  66. Vector3 pos;
  67. Quaternion rot;
  68. if (OVRNodeStateProperties.GetNodeStatePropertyVector3(Node.Head, NodeStatePropertyType.Position, OVRPlugin.Node.Head, OVRPlugin.Step.Render, out pos))
  69. headPose.position = pos;
  70. if (OVRNodeStateProperties.GetNodeStatePropertyQuaternion(Node.Head, NodeStatePropertyType.Orientation, OVRPlugin.Node.Head, OVRPlugin.Step.Render, out rot))
  71. headPose.orientation = rot;
  72. // Transform from tracking-Space to head-Space
  73. OVRPose poseInHeadSpace = headPose.Inverse() * trackingSpacePose;
  74. // Transform from head space to world space
  75. OVRPose ret = Camera.main.transform.ToOVRPose() * poseInHeadSpace;
  76. return ret;
  77. }
  78. /// <summary>
  79. /// Converts the given world-space transform to an OVRPose in head space.
  80. /// </summary>
  81. public static OVRPose ToHeadSpacePose(this Transform transform, Camera camera)
  82. {
  83. return camera.transform.ToOVRPose().Inverse() * transform.ToOVRPose();
  84. }
  85. public static OVRPose ToOVRPose(this Transform t, bool isLocal = false)
  86. {
  87. OVRPose pose;
  88. pose.orientation = (isLocal) ? t.localRotation : t.rotation;
  89. pose.position = (isLocal) ? t.localPosition : t.position;
  90. return pose;
  91. }
  92. public static void FromOVRPose(this Transform t, OVRPose pose, bool isLocal = false)
  93. {
  94. if (isLocal)
  95. {
  96. t.localRotation = pose.orientation;
  97. t.localPosition = pose.position;
  98. }
  99. else
  100. {
  101. t.rotation = pose.orientation;
  102. t.position = pose.position;
  103. }
  104. }
  105. public static OVRPose ToOVRPose(this OVRPlugin.Posef p)
  106. {
  107. return new OVRPose()
  108. {
  109. position = new Vector3(p.Position.x, p.Position.y, -p.Position.z),
  110. orientation = new Quaternion(-p.Orientation.x, -p.Orientation.y, p.Orientation.z, p.Orientation.w)
  111. };
  112. }
  113. public static OVRTracker.Frustum ToFrustum(this OVRPlugin.Frustumf f)
  114. {
  115. return new OVRTracker.Frustum()
  116. {
  117. nearZ = f.zNear,
  118. farZ = f.zFar,
  119. fov = new Vector2()
  120. {
  121. x = Mathf.Rad2Deg * f.fovX,
  122. y = Mathf.Rad2Deg * f.fovY
  123. }
  124. };
  125. }
  126. public static Color FromColorf(this OVRPlugin.Colorf c)
  127. {
  128. return new Color() { r = c.r, g = c.g, b = c.b, a = c.a };
  129. }
  130. public static OVRPlugin.Colorf ToColorf(this Color c)
  131. {
  132. return new OVRPlugin.Colorf() { r = c.r, g = c.g, b = c.b, a = c.a };
  133. }
  134. public static Vector3 FromVector3f(this OVRPlugin.Vector3f v)
  135. {
  136. return new Vector3() { x = v.x, y = v.y, z = v.z };
  137. }
  138. public static Vector3 FromFlippedZVector3f(this OVRPlugin.Vector3f v)
  139. {
  140. return new Vector3() { x = v.x, y = v.y, z = -v.z };
  141. }
  142. public static OVRPlugin.Vector3f ToVector3f(this Vector3 v)
  143. {
  144. return new OVRPlugin.Vector3f() { x = v.x, y = v.y, z = v.z };
  145. }
  146. public static OVRPlugin.Vector3f ToFlippedZVector3f(this Vector3 v)
  147. {
  148. return new OVRPlugin.Vector3f() { x = v.x, y = v.y, z = -v.z };
  149. }
  150. public static Quaternion FromQuatf(this OVRPlugin.Quatf q)
  151. {
  152. return new Quaternion() { x = q.x, y = q.y, z = q.z, w = q.w };
  153. }
  154. public static Quaternion FromFlippedZQuatf(this OVRPlugin.Quatf q)
  155. {
  156. return new Quaternion() { x = -q.x, y = -q.y, z = q.z, w = q.w };
  157. }
  158. public static OVRPlugin.Quatf ToQuatf(this Quaternion q)
  159. {
  160. return new OVRPlugin.Quatf() { x = q.x, y = q.y, z = q.z, w = q.w };
  161. }
  162. public static OVRPlugin.Quatf ToFlippedZQuatf(this Quaternion q)
  163. {
  164. return new OVRPlugin.Quatf() { x = -q.x, y = -q.y, z = q.z, w = q.w };
  165. }
  166. public static OVR.OpenVR.HmdMatrix34_t ConvertToHMDMatrix34(this Matrix4x4 m)
  167. {
  168. OVR.OpenVR.HmdMatrix34_t pose = new OVR.OpenVR.HmdMatrix34_t();
  169. pose.m0 = m[0, 0];
  170. pose.m1 = m[0, 1];
  171. pose.m2 = -m[0, 2];
  172. pose.m3 = m[0, 3];
  173. pose.m4 = m[1, 0];
  174. pose.m5 = m[1, 1];
  175. pose.m6 = -m[1, 2];
  176. pose.m7 = m[1, 3];
  177. pose.m8 = -m[2, 0];
  178. pose.m9 = -m[2, 1];
  179. pose.m10 = m[2, 2];
  180. pose.m11 = -m[2, 3];
  181. return pose;
  182. }
  183. }
  184. //4 types of node state properties that can be queried with UnityEngine.XR
  185. public enum NodeStatePropertyType
  186. {
  187. Acceleration,
  188. AngularAcceleration,
  189. Velocity,
  190. AngularVelocity,
  191. Position,
  192. Orientation
  193. }
  194. public static class OVRNodeStateProperties
  195. {
  196. #if UNITY_2017_1_OR_NEWER
  197. private static List<NodeState> nodeStateList = new List<NodeState>();
  198. #endif
  199. public static bool IsHmdPresent()
  200. {
  201. if (OVRManager.OVRManagerinitialized && OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus)
  202. return OVRPlugin.hmdPresent;
  203. #if USING_XR_SDK
  204. XRDisplaySubsystem currentDisplaySubsystem = OVRManager.GetCurrentDisplaySubsystem();
  205. if (currentDisplaySubsystem != null)
  206. return currentDisplaySubsystem.running; //In 2019.3, this should be changed to currentDisplaySubsystem.isConnected, but this is a fine placeholder for now.
  207. return false;
  208. #else
  209. return Device.isPresent;
  210. #endif
  211. }
  212. public static bool GetNodeStatePropertyVector3(Node nodeType, NodeStatePropertyType propertyType, OVRPlugin.Node ovrpNodeType, OVRPlugin.Step stepType, out Vector3 retVec)
  213. {
  214. retVec = Vector3.zero;
  215. switch (propertyType)
  216. {
  217. case NodeStatePropertyType.Acceleration:
  218. if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus)
  219. {
  220. retVec = OVRPlugin.GetNodeAcceleration(ovrpNodeType, stepType).FromFlippedZVector3f();
  221. return true;
  222. }
  223. #if UNITY_2017_1_OR_NEWER
  224. if (GetUnityXRNodeStateVector3(nodeType, NodeStatePropertyType.Acceleration, out retVec))
  225. return true;
  226. #endif
  227. break;
  228. case NodeStatePropertyType.AngularAcceleration:
  229. if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus)
  230. {
  231. retVec = OVRPlugin.GetNodeAngularAcceleration(ovrpNodeType, stepType).FromFlippedZVector3f();
  232. return true;
  233. }
  234. #if UNITY_2017_2_OR_NEWER
  235. if (GetUnityXRNodeStateVector3(nodeType, NodeStatePropertyType.AngularAcceleration, out retVec))
  236. return true;
  237. #endif
  238. break;
  239. case NodeStatePropertyType.Velocity:
  240. if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus)
  241. {
  242. retVec = OVRPlugin.GetNodeVelocity(ovrpNodeType, stepType).FromFlippedZVector3f();
  243. return true;
  244. }
  245. #if UNITY_2017_1_OR_NEWER
  246. if (GetUnityXRNodeStateVector3(nodeType, NodeStatePropertyType.Velocity, out retVec))
  247. return true;
  248. #endif
  249. break;
  250. case NodeStatePropertyType.AngularVelocity:
  251. if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus)
  252. {
  253. retVec = OVRPlugin.GetNodeAngularVelocity(ovrpNodeType, stepType).FromFlippedZVector3f();
  254. return true;
  255. }
  256. #if UNITY_2017_2_OR_NEWER
  257. if (GetUnityXRNodeStateVector3(nodeType, NodeStatePropertyType.AngularVelocity, out retVec))
  258. return true;
  259. #endif
  260. break;
  261. case NodeStatePropertyType.Position:
  262. if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus)
  263. {
  264. retVec = OVRPlugin.GetNodePose(ovrpNodeType, stepType).ToOVRPose().position;
  265. return true;
  266. }
  267. #if UNITY_2017_1_OR_NEWER
  268. if (GetUnityXRNodeStateVector3(nodeType, NodeStatePropertyType.Position, out retVec))
  269. return true;
  270. #endif
  271. break;
  272. }
  273. return false;
  274. }
  275. public static bool GetNodeStatePropertyQuaternion(Node nodeType, NodeStatePropertyType propertyType, OVRPlugin.Node ovrpNodeType, OVRPlugin.Step stepType, out Quaternion retQuat)
  276. {
  277. retQuat = Quaternion.identity;
  278. switch (propertyType)
  279. {
  280. case NodeStatePropertyType.Orientation:
  281. if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus)
  282. {
  283. retQuat = OVRPlugin.GetNodePose(ovrpNodeType, stepType).ToOVRPose().orientation;
  284. return true;
  285. }
  286. #if UNITY_2017_1_OR_NEWER
  287. if (GetUnityXRNodeStateQuaternion(nodeType, NodeStatePropertyType.Orientation, out retQuat))
  288. return true;
  289. #endif
  290. break;
  291. }
  292. return false;
  293. }
  294. #if UNITY_2017_1_OR_NEWER
  295. private static bool ValidateProperty(Node nodeType, ref NodeState requestedNodeState)
  296. {
  297. InputTracking.GetNodeStates(nodeStateList);
  298. if (nodeStateList.Count == 0)
  299. return false;
  300. bool nodeStateFound = false;
  301. requestedNodeState = nodeStateList[0];
  302. for (int i = 0; i < nodeStateList.Count; i++)
  303. {
  304. if (nodeStateList[i].nodeType == nodeType)
  305. {
  306. requestedNodeState = nodeStateList[i];
  307. nodeStateFound = true;
  308. break;
  309. }
  310. }
  311. return nodeStateFound;
  312. }
  313. #endif
  314. #if UNITY_2017_1_OR_NEWER
  315. private static bool GetUnityXRNodeStateVector3(Node nodeType, NodeStatePropertyType propertyType, out Vector3 retVec)
  316. {
  317. retVec = Vector3.zero;
  318. NodeState requestedNodeState = default(NodeState);
  319. if (!ValidateProperty(nodeType, ref requestedNodeState))
  320. return false;
  321. if (propertyType == NodeStatePropertyType.Acceleration)
  322. {
  323. if (requestedNodeState.TryGetAcceleration(out retVec))
  324. {
  325. return true;
  326. }
  327. }
  328. else if (propertyType == NodeStatePropertyType.AngularAcceleration)
  329. {
  330. #if UNITY_2017_2_OR_NEWER
  331. if (requestedNodeState.TryGetAngularAcceleration(out retVec))
  332. {
  333. return true;
  334. }
  335. #endif
  336. }
  337. else if (propertyType == NodeStatePropertyType.Velocity)
  338. {
  339. if (requestedNodeState.TryGetVelocity(out retVec))
  340. {
  341. return true;
  342. }
  343. }
  344. else if (propertyType == NodeStatePropertyType.AngularVelocity)
  345. {
  346. #if UNITY_2017_2_OR_NEWER
  347. if (requestedNodeState.TryGetAngularVelocity(out retVec))
  348. {
  349. return true;
  350. }
  351. #endif
  352. }
  353. else if (propertyType == NodeStatePropertyType.Position)
  354. {
  355. if (requestedNodeState.TryGetPosition(out retVec))
  356. {
  357. return true;
  358. }
  359. }
  360. return false;
  361. }
  362. #endif
  363. #if UNITY_2017_1_OR_NEWER
  364. private static bool GetUnityXRNodeStateQuaternion(Node nodeType, NodeStatePropertyType propertyType, out Quaternion retQuat)
  365. {
  366. retQuat = Quaternion.identity;
  367. NodeState requestedNodeState = default(NodeState);
  368. if (!ValidateProperty(nodeType, ref requestedNodeState))
  369. return false;
  370. if (propertyType == NodeStatePropertyType.Orientation)
  371. {
  372. if (requestedNodeState.TryGetRotation(out retQuat))
  373. {
  374. return true;
  375. }
  376. }
  377. return false;
  378. }
  379. #endif
  380. }
  381. /// <summary>
  382. /// An affine transformation built from a Unity position and orientation.
  383. /// </summary>
  384. [System.Serializable]
  385. public struct OVRPose
  386. {
  387. /// <summary>
  388. /// A pose with no translation or rotation.
  389. /// </summary>
  390. public static OVRPose identity
  391. {
  392. get {
  393. return new OVRPose()
  394. {
  395. position = Vector3.zero,
  396. orientation = Quaternion.identity
  397. };
  398. }
  399. }
  400. public override bool Equals(System.Object obj)
  401. {
  402. return obj is OVRPose && this == (OVRPose)obj;
  403. }
  404. public override int GetHashCode()
  405. {
  406. return position.GetHashCode() ^ orientation.GetHashCode();
  407. }
  408. public static bool operator ==(OVRPose x, OVRPose y)
  409. {
  410. return x.position == y.position && x.orientation == y.orientation;
  411. }
  412. public static bool operator !=(OVRPose x, OVRPose y)
  413. {
  414. return !(x == y);
  415. }
  416. /// <summary>
  417. /// The position.
  418. /// </summary>
  419. public Vector3 position;
  420. /// <summary>
  421. /// The orientation.
  422. /// </summary>
  423. public Quaternion orientation;
  424. /// <summary>
  425. /// Multiplies two poses.
  426. /// </summary>
  427. public static OVRPose operator*(OVRPose lhs, OVRPose rhs)
  428. {
  429. var ret = new OVRPose();
  430. ret.position = lhs.position + lhs.orientation * rhs.position;
  431. ret.orientation = lhs.orientation * rhs.orientation;
  432. return ret;
  433. }
  434. /// <summary>
  435. /// Computes the inverse of the given pose.
  436. /// </summary>
  437. public OVRPose Inverse()
  438. {
  439. OVRPose ret;
  440. ret.orientation = Quaternion.Inverse(orientation);
  441. ret.position = ret.orientation * -position;
  442. return ret;
  443. }
  444. /// <summary>
  445. /// Converts the pose from left- to right-handed or vice-versa.
  446. /// </summary>
  447. public OVRPose flipZ()
  448. {
  449. var ret = this;
  450. ret.position.z = -ret.position.z;
  451. ret.orientation.z = -ret.orientation.z;
  452. ret.orientation.w = -ret.orientation.w;
  453. return ret;
  454. }
  455. // Warning: this function is not a strict reverse of OVRPlugin.Posef.ToOVRPose(), even after flipZ()
  456. public OVRPlugin.Posef ToPosef_Legacy()
  457. {
  458. return new OVRPlugin.Posef()
  459. {
  460. Position = position.ToVector3f(),
  461. Orientation = orientation.ToQuatf()
  462. };
  463. }
  464. public OVRPlugin.Posef ToPosef()
  465. {
  466. OVRPlugin.Posef result = new OVRPlugin.Posef();
  467. result.Position.x = position.x;
  468. result.Position.y = position.y;
  469. result.Position.z = -position.z;
  470. result.Orientation.x = -orientation.x;
  471. result.Orientation.y = -orientation.y;
  472. result.Orientation.z = orientation.z;
  473. result.Orientation.w = orientation.w;
  474. return result;
  475. }
  476. }
  477. /// <summary>
  478. /// Encapsulates an 8-byte-aligned of unmanaged memory.
  479. /// </summary>
  480. public class OVRNativeBuffer : IDisposable
  481. {
  482. private bool disposed = false;
  483. private int m_numBytes = 0;
  484. private IntPtr m_ptr = IntPtr.Zero;
  485. /// <summary>
  486. /// Creates a buffer of the specified size.
  487. /// </summary>
  488. public OVRNativeBuffer(int numBytes)
  489. {
  490. Reallocate(numBytes);
  491. }
  492. /// <summary>
  493. /// Releases unmanaged resources and performs other cleanup operations before the <see cref="OVRNativeBuffer"/> is
  494. /// reclaimed by garbage collection.
  495. /// </summary>
  496. ~OVRNativeBuffer()
  497. {
  498. Dispose(false);
  499. }
  500. /// <summary>
  501. /// Reallocates the buffer with the specified new size.
  502. /// </summary>
  503. public void Reset(int numBytes)
  504. {
  505. Reallocate(numBytes);
  506. }
  507. /// <summary>
  508. /// The current number of bytes in the buffer.
  509. /// </summary>
  510. public int GetCapacity()
  511. {
  512. return m_numBytes;
  513. }
  514. /// <summary>
  515. /// A pointer to the unmanaged memory in the buffer, starting at the given offset in bytes.
  516. /// </summary>
  517. public IntPtr GetPointer(int byteOffset = 0)
  518. {
  519. if (byteOffset < 0 || byteOffset >= m_numBytes)
  520. return IntPtr.Zero;
  521. return (byteOffset == 0) ? m_ptr : new IntPtr(m_ptr.ToInt64() + byteOffset);
  522. }
  523. /// <summary>
  524. /// Releases all resource used by the <see cref="OVRNativeBuffer"/> object.
  525. /// </summary>
  526. /// <remarks>Call <see cref="Dispose"/> when you are finished using the <see cref="OVRNativeBuffer"/>. The <see cref="Dispose"/>
  527. /// method leaves the <see cref="OVRNativeBuffer"/> in an unusable state. After calling <see cref="Dispose"/>, you must
  528. /// release all references to the <see cref="OVRNativeBuffer"/> so the garbage collector can reclaim the memory that
  529. /// the <see cref="OVRNativeBuffer"/> was occupying.</remarks>
  530. public void Dispose()
  531. {
  532. Dispose(true);
  533. GC.SuppressFinalize(this);
  534. }
  535. private void Dispose(bool disposing)
  536. {
  537. if (disposed)
  538. return;
  539. if (disposing)
  540. {
  541. // dispose managed resources
  542. }
  543. // dispose unmanaged resources
  544. Release();
  545. disposed = true;
  546. }
  547. private void Reallocate(int numBytes)
  548. {
  549. Release();
  550. if (numBytes > 0)
  551. {
  552. m_ptr = Marshal.AllocHGlobal(numBytes);
  553. m_numBytes = numBytes;
  554. }
  555. else
  556. {
  557. m_ptr = IntPtr.Zero;
  558. m_numBytes = 0;
  559. }
  560. }
  561. private void Release()
  562. {
  563. if (m_ptr != IntPtr.Zero)
  564. {
  565. Marshal.FreeHGlobal(m_ptr);
  566. m_ptr = IntPtr.Zero;
  567. m_numBytes = 0;
  568. }
  569. }
  570. }