Direct3DInterop.h 4.45 KB
Newer Older
wester committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
#pragma once

#include "pch.h"
#include "BasicTimer.h"
#include "QuadRenderer.h"
#include <DrawingSurfaceNative.h>
#include <ppltasks.h>
#include <windows.storage.streams.h>
#include <memory>
#include <mutex>


#include <opencv2\imgproc\types_c.h>


namespace PhoneXamlDirect3DApp1Comp
{

public enum class OCVFilterType
{
    ePreview,
    eGray,
    eCanny,
    eBlur,
    eFindFeatures,
    eSepia,
    eNumOCVFilterTypes
};

class CameraCapturePreviewSink;
class CameraCaptureSampleSink;

public delegate void RequestAdditionalFrameHandler();
public delegate void RecreateSynchronizedTextureHandler();

[Windows::Foundation::Metadata::WebHostHidden]
public ref class Direct3DInterop sealed : public Windows::Phone::Input::Interop::IDrawingSurfaceManipulationHandler
{
public:
    Direct3DInterop();

    Windows::Phone::Graphics::Interop::IDrawingSurfaceContentProvider^ CreateContentProvider();

    // IDrawingSurfaceManipulationHandler
    virtual void SetManipulationHost(Windows::Phone::Input::Interop::DrawingSurfaceManipulationHost^ manipulationHost);

    event RequestAdditionalFrameHandler^ RequestAdditionalFrame;
    event RecreateSynchronizedTextureHandler^ RecreateSynchronizedTexture;

    property Windows::Foundation::Size WindowBounds;
    property Windows::Foundation::Size NativeResolution;
    property Windows::Foundation::Size RenderResolution
    {
        Windows::Foundation::Size get(){ return m_renderResolution; }
        void set(Windows::Foundation::Size renderResolution);
    }
    void SetAlgorithm(OCVFilterType type) { m_algorithm = type; };
    void UpdateFrame(byte* buffer, int width, int height);


protected:
    // Event Handlers
    void OnPointerPressed(Windows::Phone::Input::Interop::DrawingSurfaceManipulationHost^ sender, Windows::UI::Core::PointerEventArgs^ args);
    void OnPointerMoved(Windows::Phone::Input::Interop::DrawingSurfaceManipulationHost^ sender, Windows::UI::Core::PointerEventArgs^ args);
    void OnPointerReleased(Windows::Phone::Input::Interop::DrawingSurfaceManipulationHost^ sender, Windows::UI::Core::PointerEventArgs^ args);

internal:
    HRESULT STDMETHODCALLTYPE Connect(_In_ IDrawingSurfaceRuntimeHostNative* host);
    void STDMETHODCALLTYPE Disconnect();
    HRESULT STDMETHODCALLTYPE PrepareResources(_In_ const LARGE_INTEGER* presentTargetTime, _Out_ BOOL* contentDirty);
    HRESULT STDMETHODCALLTYPE GetTexture(_In_ const DrawingSurfaceSizeF* size, _Out_ IDrawingSurfaceSynchronizedTextureNative** synchronizedTexture, _Out_ DrawingSurfaceRectF* textureSubRectangle);
    ID3D11Texture2D* GetTexture();

private:
    void StartCamera();
    void ProcessFrame();
    bool SwapFrames();

    QuadRenderer^ m_renderer;
    Windows::Foundation::Size m_renderResolution;
    OCVFilterType m_algorithm;
    bool m_contentDirty;
    std::shared_ptr<cv::Mat> m_backFrame;
    std::shared_ptr<cv::Mat> m_frontFrame;
    std::mutex m_mutex;

    Windows::Phone::Media::Capture::AudioVideoCaptureDevice ^pAudioVideoCaptureDevice;
    ICameraCaptureDeviceNative* pCameraCaptureDeviceNative;
    IAudioVideoCaptureDeviceNative* pAudioVideoCaptureDeviceNative;
    CameraCapturePreviewSink* pCameraCapturePreviewSink;
    CameraCaptureSampleSink* pCameraCaptureSampleSink;

    //void ApplyPreviewFilter(const cv::Mat& image);
    void ApplyGrayFilter(cv::Mat* mat);
    void ApplyCannyFilter(cv::Mat* mat);
    void ApplyBlurFilter(cv::Mat* mat);
    void ApplyFindFeaturesFilter(cv::Mat* mat);
    void ApplySepiaFilter(cv::Mat* mat);
};

class CameraCapturePreviewSink :
    public Microsoft::WRL::RuntimeClass<
        Microsoft::WRL::RuntimeClassFlags<Microsoft::WRL::RuntimeClassType::ClassicCom>,
        ICameraCapturePreviewSink>
{
public:
    void SetDelegate(Direct3DInterop^ delegate)
    {
        m_Direct3dInterop = delegate;
    }

    IFACEMETHODIMP_(void) OnFrameAvailable(
        DXGI_FORMAT format,
        UINT width,
        UINT height,
        BYTE* pixels);

private:
    Direct3DInterop^ m_Direct3dInterop;
};

class CameraCaptureSampleSink :
    public Microsoft::WRL::RuntimeClass<
        Microsoft::WRL::RuntimeClassFlags<Microsoft::WRL::RuntimeClassType::ClassicCom>,
        ICameraCaptureSampleSink>
{
public:
    void SetDelegate(Direct3DInterop^ delegate)
    {
        m_Direct3dInterop = delegate;
    }

    IFACEMETHODIMP_(void) OnSampleAvailable(
            ULONGLONG hnsPresentationTime,
            ULONGLONG hnsSampleDuration,
            DWORD cbSample,
            BYTE* pSample);

private:
    Direct3DInterop^ m_Direct3dInterop;
};

}