videoCaptureD3D9.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "videoCaptureD3D9.h"
  23. #include "gfx/D3D9/gfxD3D9Device.h"
  24. #include "platform/tmm_off.h"
  25. #include <d3d9.h>
  26. #include <d3dx9core.h>
  27. #include <d3dx9tex.h>
  28. VideoFrameGrabberD3D9::VideoFrameGrabberD3D9()
  29. {
  30. GFXDevice::getDeviceEventSignal().notify( this, &VideoFrameGrabberD3D9::_handleGFXEvent );
  31. mCurrentCapture = 0;
  32. }
  33. VideoFrameGrabberD3D9::~VideoFrameGrabberD3D9()
  34. {
  35. GFXDevice::getDeviceEventSignal().remove( this, &VideoFrameGrabberD3D9::_handleGFXEvent );
  36. }
  37. void VideoFrameGrabberD3D9::captureBackBuffer()
  38. {
  39. AssertFatal( mCapture[mCurrentCapture].stage != eInSystemMemory, "Error! Trying to override a capture resource in \"SystemMemory\" stage!" );
  40. #ifndef TORQUE_OS_XENON
  41. LPDIRECT3DDEVICE9 D3DDevice = dynamic_cast<GFXD3D9Device *>(GFX)->getDevice();
  42. IDirect3DSurface9 * backBuffer;
  43. D3DDevice->GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, &backBuffer );
  44. GFXTexHandle &vidMemTex = mCapture[mCurrentCapture].vidMemTex;
  45. // Re-init video memory texture if needed
  46. if (vidMemTex.isNull() || vidMemTex.getWidthHeight() != mResolution)
  47. vidMemTex.set(mResolution.x, mResolution.y,GFXFormatR8G8B8X8, &GFXDefaultRenderTargetProfile, avar("%s() - mVidMemTex (line %d)", __FUNCTION__, __LINE__) );
  48. // set up the copy surface
  49. IDirect3DSurface9 *surface;
  50. // grab the top level surface of tex 0
  51. GFXD3D9TextureObject *to = (GFXD3D9TextureObject *) &(*vidMemTex);
  52. D3D9Assert( to->get2DTex()->GetSurfaceLevel( 0, &surface ), NULL );
  53. // use StretchRect because it allows a copy from a multisample surface
  54. // to a normal rendertarget surface
  55. D3DDevice->StretchRect( backBuffer, NULL, surface, NULL, D3DTEXF_LINEAR );
  56. // Reelase surfaces
  57. backBuffer->Release();
  58. surface->Release();
  59. // Update the stage
  60. mCapture[mCurrentCapture].stage = eInVideoMemory;
  61. #endif
  62. }
  63. void VideoFrameGrabberD3D9::makeBitmap()
  64. {
  65. // Advance the stages for all resources, except the one used for the last capture
  66. for (U32 i=0; i<eNumStages; i++)
  67. {
  68. if (i == mCurrentCapture)
  69. continue;
  70. switch (mCapture[i].stage)
  71. {
  72. case eInVideoMemory:
  73. copyToSystemMemory(mCapture[i]);
  74. break;
  75. case eInSystemMemory:
  76. copyToBitmap(mCapture[i]);
  77. break;
  78. }
  79. }
  80. // Change the resource being used for backbuffer captures
  81. mCurrentCapture = (mCurrentCapture + 1) % eNumStages;
  82. AssertFatal( mCapture[mCurrentCapture].stage != eInSystemMemory, "Error! A capture resource with an invalid state was picked for making captures!" );
  83. }
  84. void VideoFrameGrabberD3D9::releaseTextures()
  85. {
  86. for (U32 i=0; i<eNumStages; i++)
  87. {
  88. mCapture[i].sysMemTex.free();
  89. mCapture[i].vidMemTex.free();
  90. mCapture[i].stage = eReadyToCapture;
  91. }
  92. }
  93. void VideoFrameGrabberD3D9::copyToSystemMemory(CaptureResource &capture)
  94. {
  95. AssertFatal( capture.stage == eInVideoMemory, "Error! copyToSystemMemory() can only work in resources in 'eInVideoMemory' stage!" );
  96. #ifndef TORQUE_OS_XENON
  97. GFXTexHandle &vidMemTex = capture.vidMemTex;
  98. GFXTexHandle &sysMemTex = capture.sysMemTex;
  99. // Initialize system memory texture if necessary
  100. Point2I size = vidMemTex.getWidthHeight();
  101. if (sysMemTex.isNull() || sysMemTex.getWidthHeight() != size)
  102. sysMemTex.set( size.x, size.y, GFXFormatR8G8B8X8, &GFXSystemMemProfile, avar("%s() - tex (line %d)", __FUNCTION__, __LINE__) );
  103. // set up the 2 copy surfaces
  104. IDirect3DSurface9 *surface[2];
  105. // grab the top level surface of tex 0
  106. GFXD3D9TextureObject *to = (GFXD3D9TextureObject *) &(*vidMemTex);
  107. D3D9Assert( to->get2DTex()->GetSurfaceLevel( 0, &surface[0] ), NULL );
  108. // grab the top level surface of tex 1
  109. to = (GFXD3D9TextureObject *) &(*sysMemTex);
  110. D3D9Assert( to->get2DTex()->GetSurfaceLevel( 0, &surface[1] ), NULL );
  111. // copy the data from the render target to the system memory texture
  112. LPDIRECT3DDEVICE9 D3DDevice = dynamic_cast<GFXD3D9Device *>(GFX)->getDevice();
  113. D3DDevice->GetRenderTargetData( surface[0], surface[1] );
  114. // celease surfaces
  115. surface[0]->Release();
  116. surface[1]->Release();
  117. // Change the resource state
  118. capture.stage = eInSystemMemory;
  119. #endif
  120. }
  121. void VideoFrameGrabberD3D9::copyToBitmap(CaptureResource &capture)
  122. {
  123. AssertFatal( capture.stage == eInSystemMemory, "Error! copyToBitmap() can only work in resources in 'eInSystemMemory' stage!" );
  124. GFXTexHandle &sysMemTex = capture.sysMemTex;
  125. Point2I size = sysMemTex.getWidthHeight();
  126. // Setup a surface
  127. IDirect3DSurface9 *surface;
  128. GFXD3D9TextureObject *to = (GFXD3D9TextureObject *) &(*sysMemTex);
  129. D3D9Assert( to->get2DTex()->GetSurfaceLevel( 0, &surface ), NULL );
  130. // Lock the system memory surface
  131. D3DLOCKED_RECT r;
  132. D3DSURFACE_DESC d;
  133. surface->GetDesc(&d);
  134. surface->LockRect( &r, NULL, D3DLOCK_READONLY);
  135. // Allocate a GBitmap and copy into it.
  136. GBitmap *gb = new GBitmap(size.x, size.y);
  137. // We've got the X8 in there so we have to manually copy stuff.
  138. const U32* src = (const U32*)r.pBits;
  139. U8* dst = gb->getWritableBits();
  140. S32 pixels = size.x*size.y;
  141. for(S32 i=0; i<pixels; i++)
  142. {
  143. U32 px = *src++;
  144. *dst++ = px >> 16;
  145. *dst++ = px >> 8;
  146. *dst++ = px;
  147. }
  148. surface->UnlockRect();
  149. // celease surfaces
  150. surface->Release();
  151. // Push this new bitmap
  152. pushNewBitmap(gb);
  153. // Change the resource state
  154. capture.stage = eReadyToCapture;
  155. }
  156. bool VideoFrameGrabberD3D9::_handleGFXEvent( GFXDevice::GFXDeviceEventType event_ )
  157. {
  158. switch ( event_ )
  159. {
  160. case GFXDevice::deDestroy :
  161. releaseTextures();
  162. break;
  163. default:
  164. break;
  165. }
  166. return true;
  167. }