public IRenderer CreateRenderer(IRenderRoot root)
 {
     return(Renderer = new CompositingRenderer(root, _compositor));
 }
Beispiel #2
0
        protected async Task RenderToFile(Control target, [CallerMemberName] string testName = "", double dpi = 96)
        {
            if (!Directory.Exists(OutputPath))
            {
                Directory.CreateDirectory(OutputPath);
            }

            var immediatePath  = Path.Combine(OutputPath, testName + ".immediate.out.png");
            var deferredPath   = Path.Combine(OutputPath, testName + ".deferred.out.png");
            var compositedPath = Path.Combine(OutputPath, testName + ".composited.out.png");
            var factory        = AvaloniaLocator.Current.GetService <IPlatformRenderInterface>();
            var pixelSize      = new PixelSize((int)target.Width, (int)target.Height);
            var size           = new Size(target.Width, target.Height);
            var dpiVector      = new Vector(dpi, dpi);

            using (RenderTargetBitmap bitmap = new RenderTargetBitmap(pixelSize, dpiVector))
            {
                target.Measure(size);
                target.Arrange(new Rect(size));
                bitmap.Render(target);
                bitmap.Save(immediatePath);
            }


            using (var rtb = factory.CreateRenderTargetBitmap(pixelSize, dpiVector))
                using (var renderer = new DeferredRenderer(target, rtb))
                {
                    target.Measure(size);
                    target.Arrange(new Rect(size));
                    renderer.UnitTestUpdateScene();

                    // Do the deferred render on a background thread to expose any threading errors in
                    // the deferred rendering path.
                    await Task.Run((Action)renderer.UnitTestRender);

                    threadingInterface.MainThread = Thread.CurrentThread;

                    rtb.Save(deferredPath);
                }

            var timer = new ManualRenderTimer();

            var compositor = new Compositor(new RenderLoop(timer, Dispatcher.UIThread), null);

            using (var rtb = factory.CreateRenderTargetBitmap(pixelSize, dpiVector))
            {
                var root = new TestRenderRoot(dpiVector.X / 96, rtb);
                using (var renderer = new CompositingRenderer(root, compositor)
                {
                    RenderOnlyOnRenderThread = false
                })
                {
                    root.Initialize(renderer, target);
                    renderer.Start();
                    Dispatcher.UIThread.RunJobs();
                    timer.TriggerTick();
                }

                // Free pools
                for (var c = 0; c < 11; c++)
                {
                    TestThreadingInterface.RunTimers();
                }
                rtb.Save(compositedPath);
            }
        }