Direct3DInterop.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #include "pch.h"
  2. #include "Direct3DInterop.h"
  3. #include "Direct3DContentProvider.h"
  4. #include <windows.storage.streams.h>
  5. #include <wrl.h>
  6. #include <robuffer.h>
  7. #include <opencv2\imgproc\types_c.h>
  8. using namespace Windows::Storage::Streams;
  9. using namespace Microsoft::WRL;
  10. using namespace Windows::Foundation;
  11. using namespace Windows::UI::Core;
  12. using namespace Microsoft::WRL;
  13. using namespace Windows::Phone::Graphics::Interop;
  14. using namespace Windows::Phone::Input::Interop;
  15. namespace PhoneXamlDirect3DApp1Comp
  16. {
  17. void Direct3DInterop::ApplyGrayFilter(const cv::Mat& image)
  18. {
  19. cv::Mat intermediateMat;
  20. cv::cvtColor(image, intermediateMat, COLOR_RGBA2GRAY);
  21. cv::cvtColor(intermediateMat, image, COLOR_GRAY2BGRA);
  22. }
  23. void Direct3DInterop::ApplyCannyFilter(const cv::Mat& image)
  24. {
  25. cv::Mat intermediateMat;
  26. cv::Canny(image, intermediateMat, 80, 90);
  27. cv::cvtColor(intermediateMat, image, COLOR_GRAY2BGRA);
  28. }
  29. void Direct3DInterop::ApplySepiaFilter(const cv::Mat& image)
  30. {
  31. const float SepiaKernelData[16] =
  32. {
  33. /* B */0.131f, 0.534f, 0.272f, 0.f,
  34. /* G */0.168f, 0.686f, 0.349f, 0.f,
  35. /* R */0.189f, 0.769f, 0.393f, 0.f,
  36. /* A */0.000f, 0.000f, 0.000f, 1.f
  37. };
  38. const cv::Mat SepiaKernel(4, 4, CV_32FC1, (void*)SepiaKernelData);
  39. cv::transform(image, image, SepiaKernel);
  40. }
  41. Direct3DInterop::Direct3DInterop() :
  42. m_timer(ref new BasicTimer())
  43. {
  44. }
  45. IDrawingSurfaceContentProvider^ Direct3DInterop::CreateContentProvider()
  46. {
  47. ComPtr<Direct3DContentProvider> provider = Make<Direct3DContentProvider>(this);
  48. return reinterpret_cast<IDrawingSurfaceContentProvider^>(provider.Detach());
  49. }
  50. // IDrawingSurfaceManipulationHandler
  51. void Direct3DInterop::SetManipulationHost(DrawingSurfaceManipulationHost^ manipulationHost)
  52. {
  53. manipulationHost->PointerPressed +=
  54. ref new TypedEventHandler<DrawingSurfaceManipulationHost^, PointerEventArgs^>(this, &Direct3DInterop::OnPointerPressed);
  55. manipulationHost->PointerMoved +=
  56. ref new TypedEventHandler<DrawingSurfaceManipulationHost^, PointerEventArgs^>(this, &Direct3DInterop::OnPointerMoved);
  57. manipulationHost->PointerReleased +=
  58. ref new TypedEventHandler<DrawingSurfaceManipulationHost^, PointerEventArgs^>(this, &Direct3DInterop::OnPointerReleased);
  59. }
  60. void Direct3DInterop::RenderResolution::set(Windows::Foundation::Size renderResolution)
  61. {
  62. if (renderResolution.Width != m_renderResolution.Width ||
  63. renderResolution.Height != m_renderResolution.Height)
  64. {
  65. m_renderResolution = renderResolution;
  66. if (m_renderer)
  67. {
  68. m_renderer->UpdateForRenderResolutionChange(m_renderResolution.Width, m_renderResolution.Height);
  69. RecreateSynchronizedTexture();
  70. }
  71. }
  72. }
  73. // Event Handlers
  74. void Direct3DInterop::OnPointerPressed(DrawingSurfaceManipulationHost^ sender, PointerEventArgs^ args)
  75. {
  76. // Insert your code here.
  77. }
  78. void Direct3DInterop::OnPointerMoved(DrawingSurfaceManipulationHost^ sender, PointerEventArgs^ args)
  79. {
  80. // Insert your code here.
  81. }
  82. void Direct3DInterop::OnPointerReleased(DrawingSurfaceManipulationHost^ sender, PointerEventArgs^ args)
  83. {
  84. // Insert your code here.
  85. }
  86. // Interface With Direct3DContentProvider
  87. HRESULT Direct3DInterop::Connect(_In_ IDrawingSurfaceRuntimeHostNative* host)
  88. {
  89. m_renderer = ref new CubeRenderer();
  90. m_renderer->Initialize();
  91. m_renderer->UpdateForWindowSizeChange(WindowBounds.Width, WindowBounds.Height);
  92. m_renderer->UpdateForRenderResolutionChange(m_renderResolution.Width, m_renderResolution.Height);
  93. // Restart timer after renderer has finished initializing.
  94. m_timer->Reset();
  95. return S_OK;
  96. }
  97. void Direct3DInterop::Disconnect()
  98. {
  99. m_renderer = nullptr;
  100. }
  101. HRESULT Direct3DInterop::PrepareResources(_In_ const LARGE_INTEGER* presentTargetTime, _Out_ BOOL* contentDirty)
  102. {
  103. *contentDirty = true;
  104. return S_OK;
  105. }
  106. HRESULT Direct3DInterop::GetTexture(_In_ const DrawingSurfaceSizeF* size, _Out_ IDrawingSurfaceSynchronizedTextureNative** synchronizedTexture, _Out_ DrawingSurfaceRectF* textureSubRectangle)
  107. {
  108. m_timer->Update();
  109. m_renderer->Update(m_timer->Total, m_timer->Delta);
  110. m_renderer->Render();
  111. RequestAdditionalFrame();
  112. return S_OK;
  113. }
  114. ID3D11Texture2D* Direct3DInterop::GetTexture()
  115. {
  116. return m_renderer->GetTexture();
  117. }
  118. void Direct3DInterop::CreateTexture(const Platform::Array<int>^ buffer,int width,int height, OCVFilterType filter)
  119. {
  120. if (m_renderer)
  121. {
  122. cv::Mat Lena = cv::Mat(height, width, CV_8UC4);
  123. memcpy(Lena.data, buffer->Data, 4 * height*width);
  124. switch (filter)
  125. {
  126. case OCVFilterType::ePreview:
  127. break;
  128. case OCVFilterType::eGray:
  129. ApplyGrayFilter(Lena);
  130. break;
  131. case OCVFilterType::eCanny:
  132. ApplyCannyFilter(Lena);
  133. break;
  134. case OCVFilterType::eSepia:
  135. ApplySepiaFilter(Lena);
  136. break;
  137. }
  138. m_renderer->CreateTextureFromByte(Lena.data, width, height);
  139. }
  140. }
  141. byte* GetPointerToPixelData( Windows::Storage::Streams::IBuffer ^ pixelBuffer)
  142. {
  143. // Query the IBufferByteAccess interface.
  144. ComPtr<IBufferByteAccess> bufferByteAccess;
  145. reinterpret_cast<IInspectable*>( pixelBuffer)->QueryInterface(IID_PPV_ARGS(&bufferByteAccess));
  146. // Retrieve the buffer data.
  147. byte* pixels = nullptr;
  148. bufferByteAccess->Buffer(&pixels);
  149. return pixels;
  150. }
  151. }