Exemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DrawingContextImpl"/> class.
        /// </summary>
        /// <param name="visualBrushRenderer">The visual brush renderer.</param>
        /// <param name="renderTarget">The render target to draw to.</param>
        /// <param name="layerFactory">
        /// An object to use to create layers. May be null, in which case a
        /// <see cref="WicRenderTargetBitmapImpl"/> will created when a new layer is requested.
        /// </param>
        /// <param name="swapChain">An optional swap chain associated with this drawing context.</param>
        /// <param name="finishedCallback">An optional delegate to be called when context is disposed.</param>
        public DrawingContextImpl(
            IVisualBrushRenderer visualBrushRenderer,
            ILayerFactory layerFactory,
            SharpDX.Direct2D1.RenderTarget renderTarget,
            SharpDX.DXGI.SwapChain1 swapChain = null,
            Action finishedCallback           = null)
        {
            _visualBrushRenderer = visualBrushRenderer;
            _layerFactory        = layerFactory;
            _renderTarget        = renderTarget;
            _swapChain           = swapChain;
            _finishedCallback    = finishedCallback;

            if (_renderTarget is DeviceContext deviceContext)
            {
                _deviceContext     = deviceContext;
                _ownsDeviceContext = false;
            }
            else
            {
                _deviceContext     = _renderTarget.QueryInterface <DeviceContext>();
                _ownsDeviceContext = true;
            }

            _deviceContext.BeginDraw();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Sets the render target, releasing resources tied to previous render target if necessary</summary>
        /// <param name="renderTarget">RenderTarget to set</param>
        protected void SetRenderTarget(RenderTarget renderTarget)
        {
            if (renderTarget == null)
                throw new ArgumentNullException("renderTarget");

            m_clipStack.Clear();
            ReleaseResources(true);
            m_renderTargetNumber++;
            m_renderTarget = renderTarget;
            m_gdiInterop = m_renderTarget.QueryInterface<GdiInteropRenderTarget>();
            Transform = Matrix3x2F.Identity;

            if (s_strokeStyle == null)
            {
                var props = new D2dStrokeStyleProperties { EndCap = D2dCapStyle.Round, StartCap = D2dCapStyle.Round };
                s_strokeStyle = D2dFactory.CreateD2dStrokeStyle(props);
            }
            m_solidColorBrush = CreateSolidBrush(Color.Empty);

        }
Exemplo n.º 3
0
        private void Resize(Size size)
        {
            Utilities.Dispose(ref _deviceContext);
            Utilities.Dispose(ref _backBuffer);
            Utilities.Dispose(ref _renderView);
            Utilities.Dispose(ref _depthBuffer);
            Utilities.Dispose(ref _depthView);
            var context = Direct2D1Platform.Direct3D11Device.ImmediateContext;

            // Resize the backbuffer
            _swapChain.ResizeBuffers(0, 0, 0, Format.Unknown, SwapChainFlags.None);

            // Get the backbuffer from the swapchain
            _backBuffer = Resource.FromSwapChain <Texture2D>(_swapChain, 0);

            // Renderview on the backbuffer
            _renderView = new RenderTargetView(Direct2D1Platform.Direct3D11Device, _backBuffer);

            // Create the depth buffer
            _depthBuffer = new Texture2D(
                Direct2D1Platform.Direct3D11Device,
                new Texture2DDescription()
            {
                Format            = Format.D32_Float_S8X24_UInt,
                ArraySize         = 1,
                MipLevels         = 1,
                Width             = (int)size.Width,
                Height            = (int)size.Height,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = ResourceUsage.Default,
                BindFlags         = BindFlags.DepthStencil,
                CpuAccessFlags    = CpuAccessFlags.None,
                OptionFlags       = ResourceOptionFlags.None
            });

            // Create the depth buffer view
            _depthView = new DepthStencilView(Direct2D1Platform.Direct3D11Device, _depthBuffer);

            // Setup targets and viewport for rendering
            context.Rasterizer.SetViewport(new Viewport(0, 0, (int)size.Width, (int)size.Height, 0.0f, 1.0f));
            context.OutputMerger.SetTargets(_depthView, _renderView);

            // Setup new projection matrix with correct aspect ratio
            _proj = Matrix.PerspectiveFovLH((float)Math.PI / 4.0f, (float)(size.Width / size.Height), 0.1f, 100.0f);

            using (var dxgiBackBuffer = _swapChain.GetBackBuffer <Surface>(0))
            {
                var renderTarget = new SharpDX.Direct2D1.RenderTarget(
                    Direct2D1Platform.Direct2D1Factory,
                    dxgiBackBuffer,
                    new RenderTargetProperties
                {
                    DpiX        = 96,
                    DpiY        = 96,
                    Type        = RenderTargetType.Default,
                    PixelFormat = new PixelFormat(
                        Format.Unknown,
                        AlphaMode.Premultiplied)
                });

                _deviceContext = renderTarget.QueryInterface <DeviceContext>();

                renderTarget.Dispose();
            }
        }