GraphicsDeviceService.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // GraphicsDeviceService.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Threading;
  12. using Microsoft.Xna.Framework.Graphics;
  13. #endregion
  14. // The IGraphicsDeviceService interface requires a DeviceCreated event, but we
  15. // always just create the device inside our constructor, so we have no place to
  16. // raise that event. The C# compiler warns us that the event is never used, but
  17. // we don't care so we just disable this warning.
  18. #pragma warning disable 67
  19. namespace WinFormsGraphicsDevice
  20. {
  21. /// <summary>
  22. /// Helper class responsible for creating and managing the GraphicsDevice.
  23. /// All GraphicsDeviceControl instances share the same GraphicsDeviceService,
  24. /// so even though there can be many controls, there will only ever be a single
  25. /// underlying GraphicsDevice. This implements the standard IGraphicsDeviceService
  26. /// interface, which provides notification events for when the device is reset
  27. /// or disposed.
  28. /// </summary>
  29. class GraphicsDeviceService : IGraphicsDeviceService
  30. {
  31. #region Fields
  32. // Singleton device service instance.
  33. static GraphicsDeviceService singletonInstance;
  34. // Keep track of how many controls are sharing the singletonInstance.
  35. static int referenceCount;
  36. #endregion
  37. /// <summary>
  38. /// Constructor is private, because this is a singleton class:
  39. /// client controls should use the public AddRef method instead.
  40. /// </summary>
  41. GraphicsDeviceService(IntPtr windowHandle, int width, int height)
  42. {
  43. parameters = new PresentationParameters();
  44. parameters.BackBufferWidth = Math.Max(width, 1);
  45. parameters.BackBufferHeight = Math.Max(height, 1);
  46. parameters.BackBufferFormat = SurfaceFormat.Color;
  47. parameters.DepthStencilFormat = DepthFormat.Depth24;
  48. parameters.DeviceWindowHandle = windowHandle;
  49. parameters.PresentationInterval = PresentInterval.Immediate;
  50. parameters.IsFullScreen = false;
  51. graphicsDevice = new GraphicsDevice(GraphicsAdapter.DefaultAdapter,
  52. GraphicsProfile.Reach,
  53. parameters);
  54. }
  55. /// <summary>
  56. /// Gets a reference to the singleton instance.
  57. /// </summary>
  58. public static GraphicsDeviceService AddRef(IntPtr windowHandle,
  59. int width, int height)
  60. {
  61. // Increment the "how many controls sharing the device" reference count.
  62. if (Interlocked.Increment(ref referenceCount) == 1)
  63. {
  64. // If this is the first control to start using the
  65. // device, we must create the singleton instance.
  66. singletonInstance = new GraphicsDeviceService(windowHandle,
  67. width, height);
  68. }
  69. return singletonInstance;
  70. }
  71. /// <summary>
  72. /// Releases a reference to the singleton instance.
  73. /// </summary>
  74. public void Release(bool disposing)
  75. {
  76. // Decrement the "how many controls sharing the device" reference count.
  77. if (Interlocked.Decrement(ref referenceCount) == 0)
  78. {
  79. // If this is the last control to finish using the
  80. // device, we should dispose the singleton instance.
  81. if (disposing)
  82. {
  83. if (DeviceDisposing != null)
  84. DeviceDisposing(this, EventArgs.Empty);
  85. graphicsDevice.Dispose();
  86. }
  87. graphicsDevice = null;
  88. }
  89. }
  90. /// <summary>
  91. /// Resets the graphics device to whichever is bigger out of the specified
  92. /// resolution or its current size. This behavior means the device will
  93. /// demand-grow to the largest of all its GraphicsDeviceControl clients.
  94. /// </summary>
  95. public void ResetDevice(int width, int height)
  96. {
  97. if (DeviceResetting != null)
  98. DeviceResetting(this, EventArgs.Empty);
  99. parameters.BackBufferWidth = Math.Max(parameters.BackBufferWidth, width);
  100. parameters.BackBufferHeight = Math.Max(parameters.BackBufferHeight, height);
  101. graphicsDevice.Reset(parameters);
  102. if (DeviceReset != null)
  103. DeviceReset(this, EventArgs.Empty);
  104. }
  105. /// <summary>
  106. /// Gets the current graphics device.
  107. /// </summary>
  108. public GraphicsDevice GraphicsDevice
  109. {
  110. get { return graphicsDevice; }
  111. }
  112. GraphicsDevice graphicsDevice;
  113. // Store the current device settings.
  114. PresentationParameters parameters;
  115. // IGraphicsDeviceService events.
  116. public event EventHandler<EventArgs> DeviceCreated;
  117. public event EventHandler<EventArgs> DeviceDisposing;
  118. public event EventHandler<EventArgs> DeviceReset;
  119. public event EventHandler<EventArgs> DeviceResetting;
  120. }
  121. }