SpoutSDK.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. SpoutSDK.h
  3. The main SDK include file
  4. Copyright (c) 2014-2017, Lynn Jarvis. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without modification,
  6. are permitted provided that the following conditions are met:
  7. 1. Redistributions of source code must retain the above copyright notice,
  8. this list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above copyright notice,
  10. this list of conditions and the following disclaimer in the documentation
  11. and/or other materials provided with the distribution.
  12. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  13. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  14. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  15. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  16. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  17. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  18. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  19. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  20. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21. */
  22. #pragma once
  23. #ifndef __SpoutSDK__
  24. #define __SpoutSDK__
  25. #include <windows.h>
  26. #include <set>
  27. #include <string>
  28. #include <iostream>
  29. #include <fstream>
  30. #include <Mmsystem.h> // for timegettime
  31. #include <direct.h> // for _getcwd
  32. #include <shlwapi.h> // for path functions
  33. #include "Shellapi.h" // for shellexecute
  34. #pragma comment(lib, "shlwapi.lib") // for path functions
  35. #pragma comment(lib, "Shell32.lib") // for shellexecute
  36. #pragma comment(lib, "Advapi32.lib") // for registry functions
  37. #pragma comment(lib, "Version.lib") // for VersionInfo API
  38. #include "SpoutCommon.h"
  39. #include "spoutMemoryShare.h"
  40. #include "SpoutSenderNames.h"
  41. #include "SpoutGLDXinterop.h"
  42. // Compile flag only - not currently used
  43. #if defined(__x86_64__) || defined(_M_X64)
  44. #define is64bit
  45. // #elif defined(__i386) || defined(_M_IX86)
  46. // x86 32-bit
  47. #endif
  48. class SPOUT_DLLEXP Spout {
  49. public:
  50. Spout();
  51. ~Spout();
  52. spoutGLDXinterop interop; // Opengl/directx interop texture sharing object
  53. // ================== //
  54. // PUBLIC FUNCTIONS //
  55. // ================== //
  56. // Sender
  57. bool CreateSender (const char *Sendername, unsigned int width, unsigned int height, DWORD dwFormat = 0);
  58. bool UpdateSender (const char* Sendername, unsigned int width, unsigned int height);
  59. void ReleaseSender (DWORD dwMsec = 0);
  60. // Receiver
  61. bool CreateReceiver (char* Sendername, unsigned int &width, unsigned int &height, bool bUseActive = false);
  62. void ReleaseReceiver();
  63. bool CheckReceiver (char* Sendername, unsigned int &width, unsigned int &height, bool &bConnected);
  64. bool GetImageSize (char* sendername, unsigned int &width, unsigned int &height, bool &mMemoryMode);
  65. // Texture functions
  66. bool SendTexture (GLuint TextureID, GLuint TextureTarget, unsigned int width, unsigned int height, bool bInvert=true, GLuint HostFBO=0);
  67. bool SendImage (const unsigned char* pixels, unsigned int width, unsigned int height, GLenum glFormat = GL_RGBA, bool bInvert=true, GLuint HostFBO = 0);
  68. bool ReceiveTexture (char* Sendername, unsigned int &width, unsigned int &height, GLuint TextureID = 0, GLuint TextureTarget = 0, bool bInvert = false, GLuint HostFBO=0);
  69. bool ReceiveImage (char* Sendername, unsigned int &width, unsigned int &height, unsigned char* pixels, GLenum glFormat = GL_RGBA, bool bInvert = false, GLuint HostFBO=0);
  70. bool DrawSharedTexture(float max_x = 1.0, float max_y = 1.0, float aspect = 1.0, bool bInvert = true, GLuint HostFBO = 0);
  71. bool DrawToSharedTexture(GLuint TextureID, GLuint TextureTarget, unsigned int width, unsigned int height, float max_x = 1.0, float max_y = 1.0, float aspect = 1.0, bool bInvert = false, GLuint HostFBO = 0);
  72. bool BindSharedTexture();
  73. bool UnBindSharedTexture();
  74. int GetSenderCount ();
  75. bool GetSenderName (int index, char* sendername, int MaxSize = 256);
  76. bool GetSenderInfo (const char* sendername, unsigned int &width, unsigned int &height, HANDLE &dxShareHandle, DWORD &dwFormat);
  77. bool GetActiveSender(char* Sendername);
  78. bool SetActiveSender(const char* Sendername);
  79. // Utilities
  80. bool SetDX9(bool bDX9 = true); // User request to use DirectX 9 (default is DirectX 11)
  81. bool GetDX9(); // Return the flag that has been set
  82. bool SetMemoryShareMode(bool bMem = true);
  83. bool GetMemoryShareMode();
  84. bool SetCPUmode(bool bCPU = true);
  85. bool GetCPUmode();
  86. int GetShareMode();
  87. bool SetShareMode(int mode);
  88. int GetMaxSenders(); // Get maximum senders allowed
  89. void SetMaxSenders(int maxSenders); // Set maximum senders allowed
  90. // Access to globals
  91. bool GetSpoutSenderName(char * sendername, int maxchars); // get the global sender name
  92. bool IsSpoutInitialized(); // has the class been initialized
  93. bool IsBGRAavailable(); // Are bgra extensions supported (in interop class)
  94. bool IsPBOavailable(); // Are pbo extensions supported (in interop class)
  95. void SetBufferMode(bool bActive); // Set the pbo availability on or off
  96. bool GetBufferMode();
  97. // Adapter functions
  98. int GetNumAdapters(); // Get the number of graphics adapters in the system
  99. bool GetAdapterName(int index, char *adaptername, int maxchars); // Get an adapter name
  100. bool SetAdapter(int index = 0); // Set required graphics adapter for output
  101. int GetAdapter(); // Get the SpoutDirectX global adapter index
  102. bool GetHostPath(const char *sendername, char *hostpath, int maxchars); // The path of the host that produced the sender
  103. int GetVerticalSync();
  104. bool SetVerticalSync(bool bSync = true);
  105. bool SelectSenderPanel(const char* message = NULL);
  106. bool CheckSpoutPanel(); // Public for debugging
  107. bool OpenSpout(); // Public for debugging
  108. // Registry read/write
  109. bool WritePathToRegistry (const char *filepath, const char *subkey, const char *valuename);
  110. bool ReadPathFromRegistry(char *filepath, const char *subkey, const char *valuename);
  111. bool RemovePathFromRegistry(const char *subkey, const char *valuename);
  112. // Public for debugging only
  113. void UseAccessLocks(bool bUseLocks); // to disable/enable texture access locks in SpoutDirectX.cpp
  114. void SpoutCleanUp(bool bExit = false);
  115. void CleanSenders();
  116. int ReportMemory();
  117. /*
  118. //
  119. //
  120. // http://msdn.microsoft.com/en-us/library/windows/desktop/bb172558%28v=vs.85%29.aspx
  121. //
  122. // Compatible DX11/DX9 format for Texture2D
  123. // http://msdn.microsoft.com/en-us/library/windows/desktop/ff471324%28v=vs.85%29.aspx
  124. //
  125. // DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, DXGI_FORMAT_B8G8R8A8_UNORM
  126. // are compatible with DX9 - D3DFMT_A8B8G8R8
  127. //
  128. // Noted that DX11 -> DX9 only works if the DX11 format is set to DXGI_FORMAT_B8G8R8A8_UNORM
  129. // if the DX9 format is set to D3DFMT_A8B8G8R8
  130. DXGI_FORMAT_R8G8B8A8_TYPELESS = 27,
  131. DXGI_FORMAT_R8G8B8A8_UNORM = 28,
  132. DXGI_FORMAT_R8G8B8A8_UNORM_SRGB = 29,
  133. DXGI_FORMAT_R8G8B8A8_UINT = 30,
  134. DXGI_FORMAT_R8G8B8A8_SNORM = 31,
  135. DXGI_FORMAT_R8G8B8A8_SINT = 32,
  136. DXGI_FORMAT_B8G8R8A8_UNORM = 87,
  137. DXGI_FORMAT_B8G8R8X8_UNORM = 88,
  138. DXGI_FORMAT_B8G8R8A8_TYPELESS = 90,
  139. DXGI_FORMAT_B8G8R8A8_UNORM_SRGB = 91,
  140. DXGI_FORMAT_B8G8R8X8_TYPELESS = 92,
  141. DXGI_FORMAT_B8G8R8X8_UNORM_SRGB = 93,
  142. */
  143. protected :
  144. // ================================= //
  145. // PRIVATE VARIABLES AND FUNCTIONS //
  146. // ================================= //
  147. char g_SharedMemoryName[256];
  148. char UserSenderName[256]; // used for the sender selection dialog
  149. unsigned int g_Width;
  150. unsigned int g_Height;
  151. HANDLE g_ShareHandle;
  152. DWORD g_Format;
  153. GLuint g_TexID;
  154. HWND g_hWnd;
  155. bool bGLDXcompatible;
  156. bool bMemoryShareInitOK;
  157. bool bDxInitOK;
  158. bool bUseCPU;
  159. bool bMemory; // force memoryshare flag
  160. bool bInitialized;
  161. bool bIsSending;
  162. bool bIsReceiving;
  163. bool bChangeRequested;
  164. bool bSpoutPanelOpened;
  165. bool bSpoutPanelActive;
  166. bool bUseActive; // Use the active sender for CreateReceiver
  167. SHELLEXECUTEINFOA m_ShExecInfo;
  168. bool GLDXcompatible();
  169. bool OpenReceiver (char *name, unsigned int& width, unsigned int& height);
  170. bool InitReceiver (HWND hwnd, char* sendername, unsigned int width, unsigned int height, bool bMemoryMode);
  171. bool InitSender (HWND hwnd, const char* sendername, unsigned int width, unsigned int height, DWORD dwFormat, bool bMemoryMode);
  172. bool InitMemoryShare(bool bReceiver);
  173. bool ReleaseMemoryShare();
  174. // Find a file version
  175. bool FindFileVersion(const char *filepath, DWORD &versMS, DWORD &versLS);
  176. };
  177. #endif