/// <summary> /// Hooked to allow resizing a texture/surface that is reused. Currently not in use as we create the texture for each /// request /// to support different sizes each time (as we use DirectX to copy only the region we are after rather than the entire /// backbuffer) /// </summary> /// <param name="swapChainPtr"></param> /// <param name="newTargetParameters"></param> /// <returns></returns> private int ResizeTargetHook(IntPtr swapChainPtr, ref ModeDescription newTargetParameters) { // Dispose of overlay engine (so it will be recreated with correct renderTarget view size) if (_overlayEngine != null) { _overlayEngine.Dispose(); _overlayEngine = null; } return DXGISwapChain_ResizeTargetHook.Original(swapChainPtr, ref newTargetParameters); }
/// <summary> /// Our present hook that will grab a copy of the backbuffer when requested. Note: this supports multi-sampling /// (anti-aliasing) /// </summary> /// <param name="swapChainPtr"></param> /// <param name="syncInterval"></param> /// <param name="flags"></param> /// <returns>The HRESULT of the original method</returns> private int PresentHook(IntPtr swapChainPtr, int syncInterval, PresentFlags flags) { Frame(); var swapChain = (SwapChain) swapChainPtr; try { #region Screenshot Request if (Request != null) { DebugMessage("PresentHook: Request Start"); var startTime = DateTime.Now; using (var currentRT = SharpDX.Direct3D11.Resource.FromSwapChain<Texture2D>(swapChain, 0)) { #region Determine region to capture var captureRegion = new Rectangle(0, 0, currentRT.Description.Width, currentRT.Description.Height); if (Request.RegionToCapture.Width > 0) { captureRegion = new Rectangle(Request.RegionToCapture.Left, Request.RegionToCapture.Top, Request.RegionToCapture.Right, Request.RegionToCapture.Bottom); } else if (Request.Resize.HasValue) { captureRegion = new Rectangle(0, 0, Request.Resize.Value.Width, Request.Resize.Value.Height); } #endregion // Create / Recreate resources as necessary EnsureResources(currentRT.Device, currentRT.Description, captureRegion, Request); Texture2D sourceTexture = null; // If texture is multisampled, then we can use ResolveSubresource to copy it into a non-multisampled texture if (currentRT.Description.SampleDescription.Count > 1 || Request.Resize.HasValue) { if (Request.Resize.HasValue) DebugMessage("PresentHook: resizing texture"); else DebugMessage("PresentHook: resolving multi-sampled texture"); // Resolve into _resolvedRT if (_resolvedRTKeyedMutex != null) _resolvedRTKeyedMutex.Acquire(0, int.MaxValue); currentRT.Device.ImmediateContext.ResolveSubresource(currentRT, 0, _resolvedRT, 0, _resolvedRT.Description.Format); if (_resolvedRTKeyedMutex != null) _resolvedRTKeyedMutex.Release(1); if (Request.Resize.HasValue) { lock (_lock) { if (_resolvedRTKeyedMutex_Dev2 != null) _resolvedRTKeyedMutex_Dev2.Acquire(1, int.MaxValue); _saQuad.ShaderResource = _resolvedSharedSRV; _saQuad.RenderTargetView = _resizedRTV; _saQuad.RenderTarget = _resizedRT; _saQuad.Render(); if (_resolvedRTKeyedMutex_Dev2 != null) _resolvedRTKeyedMutex_Dev2.Release(0); } // set sourceTexture to the resized RT sourceTexture = _resizedRT; } else { // Make sourceTexture be the resolved texture sourceTexture = _resolvedRTShared; } } else { // Copy the resource into the shared texture if (_resolvedRTKeyedMutex != null) _resolvedRTKeyedMutex.Acquire(0, int.MaxValue); currentRT.Device.ImmediateContext.CopySubresourceRegion(currentRT, 0, null, _resolvedRT, 0); if (_resolvedRTKeyedMutex != null) _resolvedRTKeyedMutex.Release(1); sourceTexture = _resolvedRTShared; } // Copy to memory and send back to host process on a background thread so that we do not cause any delay in the rendering pipeline _requestCopy = Request.Clone(); // this.Request gets set to null, so copy the Request for use in the thread // Prevent the request from being processed a second time Request = null; var acquireLock = sourceTexture == _resolvedRTShared; ThreadPool.QueueUserWorkItem(o => { // Acquire lock on second device if (acquireLock && _resolvedRTKeyedMutex_Dev2 != null) _resolvedRTKeyedMutex_Dev2.Acquire(1, int.MaxValue); lock (_lock) { // Copy the subresource region, we are dealing with a flat 2D texture with no MipMapping, so 0 is the subresource index sourceTexture.Device.ImmediateContext.CopySubresourceRegion(sourceTexture, 0, new ResourceRegion { Top = captureRegion.Top, Bottom = captureRegion.Bottom, Left = captureRegion.Left, Right = captureRegion.Right, Front = 0, Back = 1 // Must be 1 or only black will be copied }, _finalRT, 0, 0, 0, 0); // Release lock upon shared surface on second device if (acquireLock && _resolvedRTKeyedMutex_Dev2 != null) _resolvedRTKeyedMutex_Dev2.Release(0); _finalRT.Device.ImmediateContext.End(_query); _queryIssued = true; while (!_finalRT.Device.ImmediateContext.GetData(_query).ReadBoolean()) { // Spin (usually no spin takes place) } var startCopyToSystemMemory = DateTime.Now; try { var db = default(DataBox); if (_requestCopy.Format == ImageFormat.PixelData) { db = _finalRT.Device.ImmediateContext.MapSubresource(_finalRT, 0, MapMode.Read, MapFlags.DoNotWait); _finalRTMapped = true; } _queryIssued = false; try { using (var ms = new MemoryStream()) { switch (_requestCopy.Format) { case ImageFormat.Bitmap: SharpDX.Direct3D11.Resource.ToStream( _finalRT.Device.ImmediateContext, _finalRT, ImageFileFormat.Bmp, ms); break; case ImageFormat.Jpeg: SharpDX.Direct3D11.Resource.ToStream( _finalRT.Device.ImmediateContext, _finalRT, ImageFileFormat.Jpg, ms); break; case ImageFormat.Png: SharpDX.Direct3D11.Resource.ToStream( _finalRT.Device.ImmediateContext, _finalRT, ImageFileFormat.Png, ms); break; case ImageFormat.PixelData: if (db.DataPointer != IntPtr.Zero) { ProcessCapture(_finalRT.Description.Width, _finalRT.Description.Height, db.RowPitch, PixelFormat.Format32bppArgb, db.DataPointer, _requestCopy); } return; } ms.Position = 0; ProcessCapture(ms, _requestCopy); } } finally { DebugMessage("PresentHook: Copy to System Memory time: " + (DateTime.Now - startCopyToSystemMemory).ToString()); } if (_finalRTMapped) { lock (_lock) { _finalRT.Device.ImmediateContext.UnmapSubresource(_finalRT, 0); _finalRTMapped = false; } } } catch (SharpDXException exc) { // Catch DXGI_ERROR_WAS_STILL_DRAWING and ignore - the data isn't available yet } } }); // Note: it would be possible to capture multiple frames and process them in a background thread } DebugMessage("PresentHook: Copy BackBuffer time: " + (DateTime.Now - startTime)); DebugMessage("PresentHook: Request End"); } #endregion #region Draw overlay (after screenshot so we don't capture overlay as well) if (Config.ShowOverlay) { // Initialise Overlay Engine if (_swapChainPointer != swapChain.NativePointer || _overlayEngine == null || IsOverlayUpdatePending) { if (_overlayEngine != null) _overlayEngine.Dispose(); _overlayEngine = new DXOverlayEngine(); _overlayEngine.Overlays.Add(new Overlay { Elements = OverlayElements }); _overlayEngine.Initialise(swapChain); _swapChainPointer = swapChain.NativePointer; } // Draw Overlay(s) else if (_overlayEngine != null) { foreach (var overlay in _overlayEngine.Overlays) overlay.Frame(); _overlayEngine.Draw(); } } #endregion } catch (Exception e) { // If there is an error we do not want to crash the hooked application, so swallow the exception DebugMessage("PresentHook: Exeception: " + e.GetType().FullName + ": " + e); //return unchecked((int)0x8000FFFF); //E_UNEXPECTED } // As always we need to call the original method, note that EasyHook will automatically skip the hook and call the original method // i.e. calling it here will not cause a stack overflow into this function return DXGISwapChain_PresentHook.Original(swapChainPtr, syncInterval, flags); }
public override void Cleanup() { try { if (_overlayEngine != null) { _overlayEngine.Dispose(); _overlayEngine = null; } } catch { } }