private void Add <T>(T node) where T : class, IDrawOperation
 {
     using (var refCounted = RefCountable.Create(node))
     {
         Add(refCounted);
     }
 }
Example #2
0
        public void Replacing_Control_Releases_DrawOperation_Reference()
        {
            using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
            {
                var   bitmap = RefCountable.Create(Mock.Of <IBitmapImpl>());
                Image img;
                var   tree = new TestRoot
                {
                    Child = img = new Image
                    {
                        Source = new Bitmap(bitmap)
                    }
                };

                var scene        = new Scene(tree);
                var sceneBuilder = new SceneBuilder();
                sceneBuilder.UpdateAll(scene);

                var operation = scene.FindNode(img).DrawOperations[0];

                tree.Child = new Decorator();

                using (var result = scene.CloneScene())
                {
                    sceneBuilder.Update(result, img);
                    scene.Dispose();

                    Assert.Equal(0, operation.RefCount);
                    Assert.Equal(2, bitmap.RefCount);
                }
            }
        }
Example #3
0
        public void Should_Trim_DrawOperations()
        {
            var node = new VisualNode(new TestRoot(), null);

            node.LayerRoot = node.Visual;

            for (var i = 0; i < 4; ++i)
            {
                var drawOperation = new Mock <IDrawOperation>();
                using (var r = RefCountable.Create(drawOperation.Object))
                {
                    node.AddDrawOperation(r);
                }
            }

            var drawOperations = node.DrawOperations.Select(op => op.Item).ToList();
            var layers         = new SceneLayers(node.Visual);
            var target         = new DeferredDrawingContextImpl(null, layers);

            using (target.BeginUpdate(node))
            {
                target.FillRectangle(Brushes.Green, new Rect(0, 0, 10, 100));
                target.FillRectangle(Brushes.Blue, new Rect(0, 0, 20, 100));
            }

            Assert.Equal(2, node.DrawOperations.Count);

            foreach (var i in drawOperations)
            {
                Mock.Get(i).Verify(x => x.Dispose());
            }
        }
Example #4
0
 private void Add(IDrawOperation node)
 {
     using (var refCounted = RefCountable.Create(node))
     {
         Add(refCounted);
     }
 }
Example #5
0
        public void Disposing_Scene_Releases_DrawOperation_References()
        {
            using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
            {
                var   bitmap = RefCountable.Create(Mock.Of <IBitmapImpl>());
                Image img;
                var   tree = new TestRoot
                {
                    Child = img = new Image
                    {
                        Source = new Bitmap(bitmap)
                    }
                };

                Assert.Equal(2, bitmap.RefCount);
                IRef <IDrawOperation> operation;

                using (var scene = new Scene(tree))
                {
                    var sceneBuilder = new SceneBuilder();
                    sceneBuilder.UpdateAll(scene);
                    operation = scene.FindNode(img).DrawOperations[0];
                    Assert.Equal(1, operation.RefCount);

                    Assert.Equal(3, bitmap.RefCount);
                }
                Assert.Equal(0, operation.RefCount);
                Assert.Equal(2, bitmap.RefCount);
            }
        }
Example #6
0
        public Bitmap(PixelFormat format, IntPtr data, PixelSize size, Vector dpi, int stride)
        {
            var ri = GetFactory();

            PlatformImpl = RefCountable.Create(ri
                                               .LoadBitmap(format, ri.DefaultAlphaFormat, data, size, dpi, stride));
        }
Example #7
0
        private void UpdateScene()
        {
            Dispatcher.UIThread.VerifyAccess();
            if (_root.IsVisible)
            {
                var sceneRef = RefCountable.Create(_scene?.Item.CloneScene() ?? new Scene(_root));
                var scene    = sceneRef.Item;

                if (_dirty == null)
                {
                    _dirty = new DirtyVisuals();
                    _sceneBuilder.UpdateAll(scene);
                }
                else if (_dirty.Count > 0)
                {
                    foreach (var visual in _dirty)
                    {
                        _sceneBuilder.Update(scene, visual);
                    }
                }

                var oldScene = Interlocked.Exchange(ref _scene, sceneRef);
                oldScene?.Dispose();

                _dirty.Clear();
                (_root as IRenderRoot)?.Invalidate(new Rect(scene.Size));
            }
            else
            {
                var oldScene = Interlocked.Exchange(ref _scene, null);
                oldScene?.Dispose();
            }
        }
Example #8
0
        public Bitmap(PixelFormat format, IntPtr data, PixelSize size, Vector dpi, int stride)
        {
            var ri = AvaloniaLocator.Current.GetService <IPlatformRenderInterface>();

            PlatformImpl = RefCountable.Create(AvaloniaLocator.Current.GetService <IPlatformRenderInterface>()
                                               .LoadBitmap(format, ri.DefaultAlphaFormat, data, size, dpi, stride));
        }
Example #9
0
        public void Render(IDrawingContextImpl context)
        {
            var finalRenderSurface = context.CreateLayer(_destRect.Size);

            if (finalRenderSurface is null)
            {
                context.Clear(Colors.Aqua);
                return;
            }

            using (var renderSurfaceCtx = finalRenderSurface.CreateDrawingContext(null))
            {
                using (_lottieCanvas.CreateSession(_destRect.Size, finalRenderSurface,
                                                   new DrawingContext(renderSurfaceCtx)))
                {
                    _compositionLayer.Draw(_lottieCanvas, _matrix, 255);
                }
            }

            context.DrawBitmap(RefCountable.Create(finalRenderSurface),
                               1,
                               new Rect(new Point(), finalRenderSurface.PixelSize.ToSize(1)), _destRect);

            finalRenderSurface.Dispose();
        }
Example #10
0
        public void Adding_DrawOperation_Should_Create_Collection()
        {
            var node       = new VisualNode(Mock.Of <IVisual>(), null);
            var collection = node.DrawOperations;

            node.AddDrawOperation(RefCountable.Create(Mock.Of <IDrawOperation>()));

            Assert.NotSame(collection, node.DrawOperations);
        }
Example #11
0
        public void RecreateBitmap(IDrawingContextImpl drawingContext, Size size, double scaling)
        {
            if (Size != size || Scaling != scaling)
            {
                var resized = RefCountable.Create(drawingContext.CreateLayer(size));

                using (var context = resized.Item.CreateDrawingContext(null))
                {
                    Bitmap.Dispose();
                    context.Clear(default);
Example #12
0
        public void Cloned_Nodes_Should_Share_DrawOperations_Collection()
        {
            var node1 = new VisualNode(Mock.Of <IVisual>(), null);

            node1.AddDrawOperation(RefCountable.Create(Mock.Of <IDrawOperation>()));

            var node2 = node1.Clone(null);

            Assert.Same(node1.DrawOperations, node2.DrawOperations);
        }
Example #13
0
        public void Image_Node_Releases_Reference_To_Bitmap_On_Dispose()
        {
            var bitmap    = RefCountable.Create(Mock.Of <IBitmapImpl>());
            var imageNode = new ImageNode(Matrix.Identity, bitmap, 1, new Rect(1, 1, 1, 1), new Rect(1, 1, 1, 1));

            Assert.Equal(2, bitmap.RefCount);

            imageNode.Dispose();

            Assert.Equal(1, bitmap.RefCount);
        }
Example #14
0
 public RenderLayer(
     IDrawingContextImpl drawingContext,
     Size size,
     double scaling,
     IVisual layerRoot)
 {
     Bitmap    = RefCountable.Create(drawingContext.CreateLayer(size));
     Size      = size;
     Scaling   = scaling;
     LayerRoot = layerRoot;
     IsEmpty   = true;
 }
Example #15
0
        public override IGlPlatformSurfaceRenderTarget CreateGlRenderTarget()
        {
            using (_egl.PrimaryContext.EnsureCurrent())
            {
                if (_window?.Item == null)
                {
                    _window = RefCountable.Create(_connection.CreateWindow(_info.Handle));
                    _window.Item.SetBlur(_blurEffect);
                }

                return(new CompositionRenderTarget(_egl, _window, _info));
            }
        }
Example #16
0
        public void DrawOperations_In_Cloned_Node_Are_Cloned()
        {
            var node1      = new VisualNode(Mock.Of <IVisual>(), null);
            var operation1 = RefCountable.Create(Mock.Of <IDrawOperation>());

            node1.AddDrawOperation(operation1);

            var node2      = node1.Clone(null);
            var operation2 = RefCountable.Create(Mock.Of <IDrawOperation>());

            node2.AddDrawOperation(operation2);

            Assert.Same(node1.DrawOperations[0].Item, node2.DrawOperations[0].Item);
            Assert.NotSame(node1.DrawOperations[0], node2.DrawOperations[0]);
        }
Example #17
0
 private void EnsureDrawOperationsCreated()
 {
     if (_drawOperations == null)
     {
         _drawOperations           = new List <IRef <IDrawOperation> >();
         _drawOperationsRefCounter = RefCountable.Create(Disposable.Create(DisposeDrawOperations));
         _drawOperationsCloned     = false;
     }
     else if (_drawOperationsCloned)
     {
         _drawOperations = new List <IRef <IDrawOperation> >(_drawOperations.Select(op => op.Clone()));
         _drawOperationsRefCounter.Dispose();
         _drawOperationsRefCounter = RefCountable.Create(Disposable.Create(DisposeDrawOperations));
         _drawOperationsCloned     = false;
     }
 }
Example #18
0
        public void ResizeBitmap(Size size, double scaling)
        {
            if (Size != size || Scaling != scaling)
            {
                var resized = RefCountable.Create(_drawingContext.CreateLayer(size));

                using (var context = resized.Item.CreateDrawingContext(null))
                {
                    context.Clear(Colors.Transparent);
                    context.DrawImage(Bitmap, 1, new Rect(Size), new Rect(Size));
                    Bitmap.Dispose();
                    Bitmap = resized;
                    Size   = size;
                }
            }
        }
        private Frame CreateFrame(Surface frameSurface)
        {
            if (lastSurface != frameSurface)
            {
                lastSurface = frameSurface;
                var surfaceBitmap = new Bitmap1(context, frameSurface);
                lastSurfaceBitmap = surfaceBitmap;
            }
            var frameBitmap = new Bitmap(context, lastSurfaceBitmap.PixelSize,
                                         new BitmapProperties(lastSurfaceBitmap.PixelFormat,
                                                              lastSurfaceBitmap.DotsPerInch.Width,
                                                              lastSurfaceBitmap.DotsPerInch.Height));

            frameBitmap.CopyFromBitmap(lastSurfaceBitmap);
            return(new Frame(RefCountable.Create(new D2DBitmapImpl(factory, frameBitmap))));
        }
Example #20
0
        private void UpdateScene()
        {
            Dispatcher.UIThread.VerifyAccess();
            lock (_sceneLock)
            {
                if (_scene?.Item.Generation > _lastSceneId)
                {
                    return;
                }
            }
            if (_root.IsVisible)
            {
                var sceneRef = RefCountable.Create(_scene?.Item.CloneScene() ?? new Scene(_root));
                var scene    = sceneRef.Item;

                if (_dirty == null)
                {
                    _dirty = new DirtyVisuals();
                    _sceneBuilder.UpdateAll(scene);
                }
                else if (_dirty.Count > 0)
                {
                    foreach (var visual in _dirty)
                    {
                        _sceneBuilder.Update(scene, visual);
                    }
                }

                lock (_sceneLock)
                {
                    var oldScene = _scene;
                    _scene = sceneRef;
                    oldScene?.Dispose();
                }

                _dirty.Clear();
            }
            else
            {
                lock (_sceneLock)
                {
                    var oldScene = _scene;
                    _scene = null;
                    oldScene?.Dispose();
                }
            }
        }
        public void RecreateBitmap(IDrawingContextImpl drawingContext, Size size, double scaling)
        {
            if (Size != size || Scaling != scaling)
            {
                var resized = RefCountable.Create(drawingContext.CreateLayer(size));

                using (var context = resized.Item.CreateDrawingContext(null))
                {
                    context.Clear(Colors.Transparent);
                    Bitmap.Dispose();
                    Bitmap  = resized;
                    Scaling = scaling;
                    Size    = size;
                    IsEmpty = true;
                }
            }
        }
Example #22
0
        private IRef <IRenderTargetBitmapImpl> GetOverlay(
            IDrawingContextImpl parentContext,
            Size size,
            double scaling)
        {
            var pixelSize = size * scaling;

            if (_overlay == null ||
                _overlay.Item.PixelWidth != pixelSize.Width ||
                _overlay.Item.PixelHeight != pixelSize.Height)
            {
                _overlay?.Dispose();
                _overlay = RefCountable.Create(parentContext.CreateLayer(size));
            }

            return(_overlay);
        }
Example #23
0
        public void Adding_DrawOperation_To_Cloned_Node_Should_Create_New_Collection()
        {
            var node1      = new VisualNode(Mock.Of <IVisual>(), null);
            var operation1 = RefCountable.Create(Mock.Of <IDrawOperation>());

            node1.AddDrawOperation(operation1);

            var node2      = node1.Clone(null);
            var operation2 = RefCountable.Create(Mock.Of <IDrawOperation>());

            node2.ReplaceDrawOperation(0, operation2);

            Assert.NotSame(node1.DrawOperations, node2.DrawOperations);
            Assert.Equal(1, node1.DrawOperations.Count);
            Assert.Equal(1, node2.DrawOperations.Count);
            Assert.Same(operation1.Item, node1.DrawOperations[0].Item);
            Assert.Same(operation2.Item, node2.DrawOperations[0].Item);
        }
Example #24
0
        public void Trimmed_DrawOperations_Releases_Reference()
        {
            var node      = new VisualNode(new TestRoot(), null);
            var operation = RefCountable.Create(new RectangleNode(Matrix.Identity, Brushes.Red, null, new Rect(0, 0, 100, 100), 0));
            var layers    = new SceneLayers(node.Visual);
            var target    = new DeferredDrawingContextImpl(null, layers);

            node.LayerRoot = node.Visual;
            node.AddDrawOperation(operation);
            Assert.Equal(2, operation.RefCount);

            using (target.BeginUpdate(node))
            {
                target.FillRectangle(Brushes.Green, new Rect(0, 0, 100, 100));
            }

            Assert.Equal(1, node.DrawOperations.Count);
            Assert.NotSame(operation, node.DrawOperations.Single());
            Assert.Equal(1, operation.RefCount);
        }
Example #25
0
        public void Should_Not_Replace_Identical_DrawOperation()
        {
            var node      = new VisualNode(new TestRoot(), null);
            var operation = RefCountable.Create(new RectangleNode(Matrix.Identity, Brushes.Red, null, new Rect(0, 0, 100, 100), 0));
            var layers    = new SceneLayers(node.Visual);
            var target    = new DeferredDrawingContextImpl(null, layers);

            node.LayerRoot = node.Visual;
            node.AddDrawOperation(operation);

            using (target.BeginUpdate(node))
            {
                target.FillRectangle(Brushes.Red, new Rect(0, 0, 100, 100));
            }

            Assert.Equal(1, node.DrawOperations.Count);
            Assert.Same(operation.Item, node.DrawOperations.Single().Item);

            Assert.IsType <RectangleNode>(node.DrawOperations[0].Item);
        }
        public void Replacing_Control_Releases_DrawOperation_Reference()
        {
            using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
            {
                var bitmap = RefCountable.Create(Mock.Of <IBitmapImpl>(
                                                     x => x.PixelSize == new PixelSize(100, 100) &&
                                                     x.Dpi == new Vector(96, 96)));

                Image img;
                var   tree = new TestRoot
                {
                    Child = img = new Image
                    {
                        Source = new Bitmap(bitmap),
                        Width  = 100,
                        Height = 100
                    }
                };

                tree.Measure(Size.Infinity);
                tree.Arrange(new Rect(new Size(100, 100)));

                var scene        = new Scene(tree);
                var sceneBuilder = new SceneBuilder();
                sceneBuilder.UpdateAll(scene);

                var operation = scene.FindNode(img).DrawOperations[0];

                tree.Child = new Decorator();

                using (var result = scene.CloneScene())
                {
                    sceneBuilder.Update(result, img);
                    scene.Dispose();

                    Assert.Equal(0, operation.RefCount);
                    Assert.Equal(2, bitmap.RefCount);
                }
            }
        }
Example #27
0
 public Frame(IPlatformRenderInterface renderInterface, Framebuffer.FramebufferFrame frame)
 {
     Time = frame.Time;
     using (var framebuffer = frame.Framebuffer.Lock())
     {
         var writableBitmap = RefCountable.Create(renderInterface.CreateWritableBitmap(framebuffer.Width, framebuffer.Height, framebuffer.Format));
         using (var bitmapBuffer = writableBitmap.Item.Lock())
         {
             unsafe
             {
                 for (int i = 0; i < framebuffer.Height; i++)
                 {
                     Unsafe.CopyBlockUnaligned(
                         (byte *)framebuffer.Address + i * framebuffer.RowBytes,
                         (byte *)bitmapBuffer.Address + i * bitmapBuffer.RowBytes,
                         (uint)bitmapBuffer.RowBytes);
                 }
             }
         }
         FrameBitmap = writableBitmap;
     }
 }
Example #28
0
        /// <summary>
        /// Ensures that this node draw operations have been created and are mutable (in case we are using cloned operations).
        /// </summary>
        private void EnsureDrawOperationsCreated()
        {
            if (_drawOperations == null)
            {
                _drawOperations           = new List <IRef <IDrawOperation> >();
                _drawOperationsRefCounter = RefCountable.Create(CreateDisposeDrawOperations(_drawOperations));
                _drawOperationsCloned     = false;
            }
            else if (_drawOperationsCloned)
            {
                var oldDrawOperations = _drawOperations;

                _drawOperations = new List <IRef <IDrawOperation> >(oldDrawOperations.Count);

                foreach (var drawOperation in oldDrawOperations)
                {
                    _drawOperations.Add(drawOperation.Clone());
                }

                _drawOperationsRefCounter.Dispose();
                _drawOperationsRefCounter = RefCountable.Create(CreateDisposeDrawOperations(_drawOperations));
                _drawOperationsCloned     = false;
            }
        }
Example #29
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Bitmap"/> class.
 /// </summary>
 /// <param name="impl">A platform-specific bitmap implementation. Bitmap class takes the ownership.</param>
 protected Bitmap(IBitmapImpl impl)
 {
     PlatformImpl = RefCountable.Create(impl);
 }
Example #30
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Bitmap"/> class.
        /// </summary>
        /// <param name="stream">The stream to read the bitmap from.</param>
        public Bitmap(Stream stream)
        {
            IPlatformRenderInterface factory = AvaloniaLocator.Current.GetService <IPlatformRenderInterface>();

            PlatformImpl = RefCountable.Create(factory.LoadBitmap(stream));
        }