Exemplo n.º 1
0
        static public void D2dRelease(Direct2DInformation info)
        {
            info._bufferBack?.Dispose();
            info.View?.Dispose();

            info.ImagingFacy?.Dispose();


            info.d2DDevice?.Dispose();
            info.D2DFacy?.Dispose();
            info.dxgiDevice?.Dispose();
            info.d3DDevice?.Dispose();
        }
Exemplo n.º 2
0
        private bool isRunning = false;//指示是否正在运行

        static public Direct2DInformation D2dInit(Size2 size)
        {
            Direct2DInformation result = new Direct2DInformation();

            result.d3DDevice = new SharpDX.Direct3D11.Device(DriverType.Hardware, DeviceCreationFlags.BgraSupport);
            var __dxgiDevice = result.d3DDevice.QueryInterface <Device>();

            result.dxgiDevice = __dxgiDevice.QueryInterface <SharpDX.DXGI.Device>();
            __dxgiDevice.Dispose();

            result.D2DFacy   = new D2DFactory();
            result.d2DDevice = new SharpDX.Direct2D1.Device(result.D2DFacy, result.dxgiDevice);

            result.View        = new DeviceContext(result.d2DDevice, DeviceContextOptions.EnableMultithreadedOptimizations);
            result.ImagingFacy = new ImagingFactory();
            result._bufferBack = new D2DBitmap(result.View, size,
                                               new BitmapProperties1(new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied), 72, 72, BitmapOptions.Target | BitmapOptions.CannotDraw));
            result.View.Target = result._bufferBack;

            return(result);
        }
Exemplo n.º 3
0
        public void DrawStartup()
        {
            buffer = new WriteableBitmap((int)Width, (int)Height, 72, 72, System.Windows.Media.PixelFormats.Pbgra32, null);

            this.Source = buffer;
            TargetDpi   = 60;

            Speed     = 1.0d / TargetDpi;
            isRunning = true;



            m_d2d_info = D2dInit(new Size2((int)Width, (int)Height));

            if (FirstDraw != null)
            {
                try
                {
                    FirstDraw(m_d2d_info, null);
                    this.Dispatcher.Invoke(new Action(() => { Commit(); }));
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.ToString());
                }
            }

            m_Dipter = new DispatcherTimer()
            {
                Interval = TimeSpan.FromSeconds(1)
            };
            m_Dipter.Tick += (e, v) =>
            {
                FrameRate = Times;
                Debug.WriteLine("Speed:" + FrameRate.ToString());

                Times = 0;
            };
            m_Dipter2 = new Thread(() =>
            {//绘图代码
                while (isRunning)
                {
                    DrawProcResult?UpData = null;
                    Stopwatch sw          = new Stopwatch();
                    sw.Start();

                    UpData = DrawProc?.Invoke(m_d2d_info);
                    if (!(UpData == DrawProcResult.Ignore || UpData == null))
                    {
                        this.Dispatcher.Invoke(new Action(() => { Commit(); }));
                    }

                    sw.Stop();

                    while (CleanTasks.Count > 0)
                    {
                        CleanTasks[0].Dispose();
                        CleanTasks.RemoveAt(0);
                    }
                    decimal time      = sw.ElapsedTicks / (decimal)Stopwatch.Frequency * 1000;
                    decimal wait_time = 1000.0M / (decimal)TargetDpi - time;

                    if (wait_time < 0)
                    {
                        wait_time = 0;
                    }

                    if (UpData == DrawProcResult.Normal || UpData == null)
                    {
                        Thread.Sleep((int)wait_time);
                        Times++;
                        continue;
                    }
                    if (UpData == DrawProcResult.Ignore)
                    {
                        continue;
                    }
                    if (UpData == DrawProcResult.Death)
                    {
                        this.Dispose();
                        break;
                    }
                }

                Disposing?.Invoke(this);

                buffer = null;

                //////
                D2dRelease(m_d2d_info);
                Disposed?.Invoke();
            })
            {
                IsBackground = true
            };


            Stretch = System.Windows.Media.Stretch.Fill;
            m_Dipter.Start();
            m_Dipter2.Start();
        }