Direct3DInterop.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #pragma once
  2. #include "pch.h"
  3. #include "BasicTimer.h"
  4. #include "QuadRenderer.h"
  5. #include <DrawingSurfaceNative.h>
  6. #include <ppltasks.h>
  7. #include <windows.storage.streams.h>
  8. #include <memory>
  9. #include <mutex>
  10. #include <opencv2\imgproc\types_c.h>
  11. namespace PhoneXamlDirect3DApp1Comp
  12. {
  13. public enum class OCVFilterType
  14. {
  15. ePreview,
  16. eGray,
  17. eCanny,
  18. eBlur,
  19. eFindFeatures,
  20. eSepia,
  21. eNumOCVFilterTypes
  22. };
  23. class CameraCapturePreviewSink;
  24. class CameraCaptureSampleSink;
  25. public delegate void RequestAdditionalFrameHandler();
  26. public delegate void RecreateSynchronizedTextureHandler();
  27. [Windows::Foundation::Metadata::WebHostHidden]
  28. public ref class Direct3DInterop sealed : public Windows::Phone::Input::Interop::IDrawingSurfaceManipulationHandler
  29. {
  30. public:
  31. Direct3DInterop();
  32. Windows::Phone::Graphics::Interop::IDrawingSurfaceContentProvider^ CreateContentProvider();
  33. // IDrawingSurfaceManipulationHandler
  34. virtual void SetManipulationHost(Windows::Phone::Input::Interop::DrawingSurfaceManipulationHost^ manipulationHost);
  35. event RequestAdditionalFrameHandler^ RequestAdditionalFrame;
  36. event RecreateSynchronizedTextureHandler^ RecreateSynchronizedTexture;
  37. property Windows::Foundation::Size WindowBounds;
  38. property Windows::Foundation::Size NativeResolution;
  39. property Windows::Foundation::Size RenderResolution
  40. {
  41. Windows::Foundation::Size get(){ return m_renderResolution; }
  42. void set(Windows::Foundation::Size renderResolution);
  43. }
  44. void SetAlgorithm(OCVFilterType type) { m_algorithm = type; };
  45. void UpdateFrame(byte* buffer, int width, int height);
  46. protected:
  47. // Event Handlers
  48. void OnPointerPressed(Windows::Phone::Input::Interop::DrawingSurfaceManipulationHost^ sender, Windows::UI::Core::PointerEventArgs^ args);
  49. void OnPointerMoved(Windows::Phone::Input::Interop::DrawingSurfaceManipulationHost^ sender, Windows::UI::Core::PointerEventArgs^ args);
  50. void OnPointerReleased(Windows::Phone::Input::Interop::DrawingSurfaceManipulationHost^ sender, Windows::UI::Core::PointerEventArgs^ args);
  51. internal:
  52. HRESULT STDMETHODCALLTYPE Connect(_In_ IDrawingSurfaceRuntimeHostNative* host);
  53. void STDMETHODCALLTYPE Disconnect();
  54. HRESULT STDMETHODCALLTYPE PrepareResources(_In_ const LARGE_INTEGER* presentTargetTime, _Out_ BOOL* contentDirty);
  55. HRESULT STDMETHODCALLTYPE GetTexture(_In_ const DrawingSurfaceSizeF* size, _Out_ IDrawingSurfaceSynchronizedTextureNative** synchronizedTexture, _Out_ DrawingSurfaceRectF* textureSubRectangle);
  56. ID3D11Texture2D* GetTexture();
  57. private:
  58. void StartCamera();
  59. void ProcessFrame();
  60. bool SwapFrames();
  61. QuadRenderer^ m_renderer;
  62. Windows::Foundation::Size m_renderResolution;
  63. OCVFilterType m_algorithm;
  64. bool m_contentDirty;
  65. std::shared_ptr<cv::Mat> m_backFrame;
  66. std::shared_ptr<cv::Mat> m_frontFrame;
  67. std::mutex m_mutex;
  68. Windows::Phone::Media::Capture::AudioVideoCaptureDevice ^pAudioVideoCaptureDevice;
  69. ICameraCaptureDeviceNative* pCameraCaptureDeviceNative;
  70. IAudioVideoCaptureDeviceNative* pAudioVideoCaptureDeviceNative;
  71. CameraCapturePreviewSink* pCameraCapturePreviewSink;
  72. CameraCaptureSampleSink* pCameraCaptureSampleSink;
  73. //void ApplyPreviewFilter(const cv::Mat& image);
  74. void ApplyGrayFilter(cv::Mat* mat);
  75. void ApplyCannyFilter(cv::Mat* mat);
  76. void ApplyBlurFilter(cv::Mat* mat);
  77. void ApplyFindFeaturesFilter(cv::Mat* mat);
  78. void ApplySepiaFilter(cv::Mat* mat);
  79. };
  80. class CameraCapturePreviewSink :
  81. public Microsoft::WRL::RuntimeClass<
  82. Microsoft::WRL::RuntimeClassFlags<Microsoft::WRL::RuntimeClassType::ClassicCom>,
  83. ICameraCapturePreviewSink>
  84. {
  85. public:
  86. void SetDelegate(Direct3DInterop^ delegate)
  87. {
  88. m_Direct3dInterop = delegate;
  89. }
  90. IFACEMETHODIMP_(void) OnFrameAvailable(
  91. DXGI_FORMAT format,
  92. UINT width,
  93. UINT height,
  94. BYTE* pixels);
  95. private:
  96. Direct3DInterop^ m_Direct3dInterop;
  97. };
  98. class CameraCaptureSampleSink :
  99. public Microsoft::WRL::RuntimeClass<
  100. Microsoft::WRL::RuntimeClassFlags<Microsoft::WRL::RuntimeClassType::ClassicCom>,
  101. ICameraCaptureSampleSink>
  102. {
  103. public:
  104. void SetDelegate(Direct3DInterop^ delegate)
  105. {
  106. m_Direct3dInterop = delegate;
  107. }
  108. IFACEMETHODIMP_(void) OnSampleAvailable(
  109. ULONGLONG hnsPresentationTime,
  110. ULONGLONG hnsSampleDuration,
  111. DWORD cbSample,
  112. BYTE* pSample);
  113. private:
  114. Direct3DInterop^ m_Direct3dInterop;
  115. };
  116. }