class CanvasDevice : RESOURCE_WRAPPER_RUNTIME_CLASS()

in winrt/lib/drawing/CanvasDevice.h [183:412]


    class CanvasDevice : RESOURCE_WRAPPER_RUNTIME_CLASS(
        ID2D1Device1,
        CanvasDevice,
        ICanvasDevice,
        CloakedIid<ICanvasDeviceInternal>,
        ICanvasResourceCreator,
        IDirect3DDevice,
        CloakedIid<IDirect3DDxgiInterfaceAccess>)
    {
        InspectableClass(RuntimeClass_Microsoft_Graphics_Canvas_CanvasDevice, BaseTrust);

        // This is used for the public-facing property value. If this device was
        // created using interop, this is set to false.
        bool m_forceSoftwareRenderer; 
        
        ClosablePtr<IDXGIDevice3> m_dxgiDevice;
        
        // Null-versus-non-null is not necessarily tied to whether the 
        // device object is open or closed.
        ComPtr<IDXGIOutput> m_primaryOutput;

        EventSource<DeviceLostHandlerType, InvokeModeOptions<StopOnFirstError>> m_deviceLostEventList;

        // Backreference keeps the shared device state alive as long as any device exists.
        std::shared_ptr<SharedDeviceState> m_sharedState;

        DeviceContextPool m_deviceContextPool;

        ComPtr<ID2D1Effect> m_histogramEffect;
        ComPtr<ID2D1Effect> m_atlasEffect;

#if WINVER > _WIN32_WINNT_WINBLUE
        std::mutex m_quirkMutex;
        
        enum class SpriteBatchQuirk
        {
            NeedsCheck,
            Required,
            NotRequired
        };

        SpriteBatchQuirk m_spriteBatchQuirk;
#endif

    public:
        static ComPtr<CanvasDevice> CreateNew(bool forceSoftwareRenderer);
        static ComPtr<CanvasDevice> CreateNew(IDirect3DDevice* direct3DDevice);

        CanvasDevice(
            ID2D1Device1* d2dDevice,
            IDXGIDevice3* dxgiDevice = nullptr,
            bool forceSoftwareRenderer = false);

        //
        // ICanvasDevice
        //

        IFACEMETHOD(get_ForceSoftwareRenderer)(boolean* value) override;

        IFACEMETHOD(get_MaximumBitmapSizeInPixels)(int32_t* value) override;

        IFACEMETHOD(IsPixelFormatSupported)(DirectXPixelFormat pixelFormat, boolean* value) override;

        IFACEMETHOD(IsBufferPrecisionSupported)(CanvasBufferPrecision bufferPrecision, boolean* value) override;

        IFACEMETHOD(get_MaximumCacheSize)(UINT64* value) override;
        IFACEMETHOD(put_MaximumCacheSize)(UINT64 value) override;

        IFACEMETHOD(get_LowPriority)(boolean* value) override;
        IFACEMETHOD(put_LowPriority)(boolean value) override;

        IFACEMETHOD(add_DeviceLost)(DeviceLostHandlerType* value, EventRegistrationToken* token) override;

        IFACEMETHOD(remove_DeviceLost)(EventRegistrationToken token) override;

        IFACEMETHOD(IsDeviceLost)(int hresult, boolean* value) override;

        IFACEMETHOD(RaiseDeviceLost)() override;

        IFACEMETHOD(Lock)(ICanvasLock** value) override;

        //
        // ICanvasResourceCreator
        //

        IFACEMETHODIMP get_Device(ICanvasDevice** value);

        //
        // IClosable
        //

        IFACEMETHOD(Close)() override;

        //
        // ICanvasDeviceInternal
        //

        virtual ComPtr<ID2D1Device1> GetD2DDevice() override;

        virtual ComPtr<ID2D1DeviceContext1> CreateDeviceContextForDrawingSession() override;

        virtual ComPtr<ID2D1SolidColorBrush> CreateSolidColorBrush(D2D1_COLOR_F const& color) override;

        virtual ComPtr<ID2D1Bitmap1> CreateBitmapFromWicResource(
            IWICBitmapSource* wicBitmapSource,
            float dpi,
            CanvasAlphaMode alpha) override;

        virtual ComPtr<ID2D1Bitmap1> CreateBitmapFromBytes(
            uint8_t* bytes,
            uint32_t pitch,
            int32_t widthInPixels,
            int32_t heightInPixels,
            float dpi,
            DirectXPixelFormat format,
            CanvasAlphaMode alphaMode) override;
            
        virtual ComPtr<ID2D1Bitmap1> CreateBitmapFromSurface(
            IDirect3DSurface* surface,
            float dpi,
            CanvasAlphaMode alphaMode) override;

        virtual ComPtr<ID2D1Bitmap1> CreateRenderTargetBitmap(
            float width,
            float height,
            float dpi,
            DirectXPixelFormat format,
            CanvasAlphaMode alpha) override;

        virtual ComPtr<ID2D1BitmapBrush1> CreateBitmapBrush(ID2D1Bitmap1* bitmap) override;

        virtual ComPtr<ID2D1ImageBrush> CreateImageBrush(ID2D1Image* image) override;

        virtual ComPtr<ID2D1LinearGradientBrush> CreateLinearGradientBrush(
            ID2D1GradientStopCollection1* stopCollection) override;

        virtual ComPtr<ID2D1RadialGradientBrush> CreateRadialGradientBrush(
            ID2D1GradientStopCollection1* stopCollection) override;

        virtual ComPtr<ID2D1GradientStopCollection1> CreateGradientStopCollection(
            std::vector<D2D1_GRADIENT_STOP>&& stops,
            D2D1_COLOR_SPACE preInterpolationSpace,
            D2D1_COLOR_SPACE postInterpolationSpace,
            D2D1_BUFFER_PRECISION bufferPrecision,
            D2D1_EXTEND_MODE extendMode,
            D2D1_COLOR_INTERPOLATION_MODE interpolationMode) override;

        virtual ComPtr<IDXGISwapChain1> CreateSwapChainForComposition(
            int32_t widthInPixels,
            int32_t heightInPixels,
            DirectXPixelFormat format,
            int32_t bufferCount,
            CanvasAlphaMode alphaMode) override;

        virtual ComPtr<IDXGISwapChain1> CreateSwapChainForCoreWindow(
            ICoreWindow* coreWindow,
            int32_t widthInPixels,
            int32_t heightInPixels,
            DirectXPixelFormat format,
            int32_t bufferCount,
            CanvasAlphaMode alphaMode) override;

        virtual ComPtr<ID2D1CommandList> CreateCommandList() override;

        virtual ComPtr<ID2D1GeometryRealization> CreateFilledGeometryRealization(ID2D1Geometry* geometry, float flatteningTolerance) override;
        virtual ComPtr<ID2D1GeometryRealization> CreateStrokedGeometryRealization(
            ID2D1Geometry* geometry,
            float strokeWidth,
            ID2D1StrokeStyle* strokeStyle,
            float flatteningTolerance) override;

        virtual ComPtr<ID2D1PrintControl> CreatePrintControl(
            IPrintDocumentPackageTarget*,
            float dpi) override;

        virtual DeviceContextLease GetResourceCreationDeviceContext() override final;

        virtual ComPtr<IDXGIOutput> GetPrimaryDisplayOutput() override;

        virtual void ThrowIfCreateSurfaceFailed(HRESULT hr, wchar_t const* typeName, uint32_t width, uint32_t height) override;

        virtual HistogramAndAtlasEffects LeaseHistogramEffect(ID2D1DeviceContext* d2dContext) override;
        virtual void ReleaseHistogramEffect(HistogramAndAtlasEffects&& effects) override;

#if WINVER > _WIN32_WINNT_WINBLUE
        virtual ComPtr<ID2D1GradientMesh> CreateGradientMesh(D2D1_GRADIENT_MESH_PATCH const* patches, uint32_t patchCount) override;

        virtual bool IsSpriteBatchQuirkRequired() override;

        virtual ComPtr<ID2D1SvgDocument> CreateSvgDocument(IStream* inputXmlStream) override;
#endif

        //
        // IDirect3DDevice
        //

        IFACEMETHOD(Trim)() override;

        //
        // IDirect3DDxgiInterfaceAccess
        //

        IFACEMETHOD(GetInterface)(IID const&, void**) override;

        //
        // Internal
        //
        HRESULT GetDeviceRemovedErrorCode();

    private:
        static ComPtr<ID3D11Device> MakeD3D11Device(CanvasDeviceAdapter* adapter, bool forceSoftwareRenderer, bool useDebugD3DDevice);

        template<typename FN>
        ComPtr<IDXGISwapChain1> CreateSwapChain(
            int32_t widthInPixels,
            int32_t heightInPixels,
            DirectXPixelFormat format,
            int32_t bufferCount,
            CanvasAlphaMode alphaMode,
            FN&& createFn);

        ComPtr<ID2D1Factory2> GetD2DFactory();

        void InitializePrimaryOutput(IDXGIDevice3* dxgiDevice);

        bool DetectIfSpriteBatchQuirkIsRequired();

        ComPtr<ID2D1Bitmap1> CreateBitmapFromWicBitmap(ID2D1DeviceContext* deviceContext, IWICBitmapSource* wicBitmapSource, float dpi, CanvasAlphaMode alpha);
        ComPtr<ID2D1Bitmap1> CreateBitmapFromDdsFrame(ID2D1DeviceContext* deviceContext, IWICBitmapSource* wicBitmapSource, IWICDdsFrameDecode* ddsFrame, float dpi, CanvasAlphaMode alpha);
    };