Ejemplo n.º 1
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();
            }
        }
        private BitmapRenderTarget RenderIntermediate(
            SharpDX.Direct2D1.RenderTarget target,
            BitmapImpl bitmap,
            TileBrushCalculator calc)
        {
            var result = new BitmapRenderTarget(
                target,
                CompatibleRenderTargetOptions.None,
                calc.IntermediateSize.ToSharpDX());

            using (var context = new RenderTarget(result).CreateDrawingContext(null))
            {
                var dpi  = new Vector(target.DotsPerInch.Width, target.DotsPerInch.Height);
                var rect = new Rect(bitmap.PixelSize.ToSizeWithDpi(dpi));

                context.Clear(Colors.Transparent);
                context.PushClip(calc.IntermediateClip);
                context.Transform = calc.IntermediateTransform;

                context.DrawBitmap(RefCountable.CreateUnownedNotClonable(bitmap), 1, rect, rect, _bitmapInterpolationMode);
                context.PopClip();
            }

            return(result);
        }
 private void Add <T>(T node) where T : class, IDrawOperation
 {
     using (var refCounted = RefCountable.Create(node))
     {
         Add(refCounted);
     }
 }
Ejemplo n.º 4
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();
        }
Ejemplo n.º 5
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());
            }
        }
Ejemplo n.º 6
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);
                }
            }
        }
Ejemplo n.º 7
0
            public XImageCursor(IntPtr display, IBitmapImpl bitmap, PixelPoint hotSpot)
            {
                var size = Marshal.SizeOf <XcursorImage>() +
                           (bitmap.PixelSize.Width * bitmap.PixelSize.Height * 4);
                var runtimePlatform = AvaloniaLocator.Current.GetService <IRuntimePlatform>() ??
                                      throw new InvalidOperationException("Unable to locate IRuntimePlatform");
                var platformRenderInterface = AvaloniaLocator.Current.GetService <IPlatformRenderInterface>() ??
                                              throw new InvalidOperationException("Unable to locate IPlatformRenderInterface");

                _pixelSize = bitmap.PixelSize;
                _blob      = runtimePlatform.AllocBlob(size);

                var image = (XcursorImage *)_blob.Address;

                image->version = 1;
                image->size    = Marshal.SizeOf <XcursorImage>();
                image->width   = bitmap.PixelSize.Width;
                image->height  = bitmap.PixelSize.Height;
                image->xhot    = hotSpot.X;
                image->yhot    = hotSpot.Y;
                image->pixels  = (IntPtr)(image + 1);

                using (var renderTarget = platformRenderInterface.CreateRenderTarget(new[] { this }))
                    using (var ctx = renderTarget.CreateDrawingContext(null))
                    {
                        var r = new Rect(_pixelSize.ToSize(1));
                        ctx.DrawBitmap(RefCountable.CreateUnownedNotClonable(bitmap), 1, r, r);
                    }

                Handle = XLib.XcursorImageLoadCursor(display, _blob.Address);
            }
Ejemplo n.º 8
0
 private void Add(IDrawOperation node)
 {
     using (var refCounted = RefCountable.Create(node))
     {
         Add(refCounted);
     }
 }
Ejemplo n.º 9
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);
            }
        }
Ejemplo n.º 10
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));
        }
Ejemplo n.º 11
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));
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Configure paint wrapper for using tile brush.
        /// </summary>
        /// <param name="paintWrapper">Paint wrapper.</param>
        /// <param name="targetSize">Target size.</param>
        /// <param name="tileBrush">Tile brush to use.</param>
        /// <param name="tileBrushImage">Tile brush image.</param>
        private void ConfigureTileBrush(ref PaintWrapper paintWrapper, Size targetSize, ITileBrush tileBrush, IDrawableBitmapImpl tileBrushImage)
        {
            var calc         = new TileBrushCalculator(tileBrush, tileBrushImage.PixelSize.ToSizeWithDpi(_dpi), targetSize);
            var intermediate = CreateRenderTarget(calc.IntermediateSize);

            paintWrapper.AddDisposable(intermediate);

            using (var context = intermediate.CreateDrawingContext(null))
            {
                var sourceRect = new Rect(tileBrushImage.PixelSize.ToSizeWithDpi(96));
                var targetRect = new Rect(tileBrushImage.PixelSize.ToSizeWithDpi(_dpi));

                context.Clear(Colors.Transparent);
                context.PushClip(calc.IntermediateClip);
                context.Transform = calc.IntermediateTransform;
                context.DrawBitmap(
                    RefCountable.CreateUnownedNotClonable(tileBrushImage),
                    1,
                    sourceRect,
                    targetRect,
                    tileBrush.BitmapInterpolationMode);
                context.PopClip();
            }

            var tileTransform =
                tileBrush.TileMode != TileMode.None
                    ? SKMatrix.CreateTranslation(-(float)calc.DestinationRect.X, -(float)calc.DestinationRect.Y)
                    : SKMatrix.CreateIdentity();

            SKShaderTileMode tileX =
                tileBrush.TileMode == TileMode.None
                    ? SKShaderTileMode.Clamp
                    : tileBrush.TileMode == TileMode.FlipX || tileBrush.TileMode == TileMode.FlipXY
                        ? SKShaderTileMode.Mirror
                        : SKShaderTileMode.Repeat;

            SKShaderTileMode tileY =
                tileBrush.TileMode == TileMode.None
                    ? SKShaderTileMode.Clamp
                    : tileBrush.TileMode == TileMode.FlipY || tileBrush.TileMode == TileMode.FlipXY
                        ? SKShaderTileMode.Mirror
                        : SKShaderTileMode.Repeat;


            var image = intermediate.SnapshotImage();

            paintWrapper.AddDisposable(image);

            var paintTransform = default(SKMatrix);

            SKMatrix.Concat(
                ref paintTransform,
                tileTransform,
                SKMatrix.CreateScale((float)(96.0 / _dpi.X), (float)(96.0 / _dpi.Y)));

            using (var shader = image.ToShader(tileX, tileY, paintTransform))
            {
                paintWrapper.Paint.Shader = shader;
            }
        }
Ejemplo n.º 13
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);
        }
Ejemplo n.º 14
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);
Ejemplo n.º 15
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);
        }
Ejemplo n.º 16
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);
        }
Ejemplo n.º 17
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;
 }
Ejemplo n.º 18
0
 void ApplyTransform()
 {
     if(_path == null)
         return;
     if (_transformedPath != null)
     {
         _transformedPath.Dispose();
         _transformedPath = null;
     }
     if (!_transform.IsIdentity)
         _transformedPath =
             new RefCountable<SkPath>(new SkPath(MethodTable.Instance.TransformPath(_path.Handle, Transform)));
 }
Ejemplo n.º 19
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));
            }
        }
Ejemplo n.º 20
0
        /// <summary>
        /// Configure paint wrapper for using tile brush.
        /// </summary>
        /// <param name="paintWrapper">Paint wrapper.</param>
        /// <param name="targetSize">Target size.</param>
        /// <param name="tileBrush">Tile brush to use.</param>
        /// <param name="tileBrushImage">Tile brush image.</param>
        /// <param name="interpolationMode">The bitmap interpolation mode.</param>
        private void ConfigureTileBrush(ref PaintWrapper paintWrapper, Size targetSize, ITileBrush tileBrush, IDrawableBitmapImpl tileBrushImage)
        {
            var calc = new TileBrushCalculator(tileBrush,
                                               new Size(tileBrushImage.PixelWidth, tileBrushImage.PixelHeight), targetSize);

            var intermediate = CreateRenderTarget(
                (int)calc.IntermediateSize.Width,
                (int)calc.IntermediateSize.Height, _dpi);

            paintWrapper.AddDisposable(intermediate);

            using (var context = intermediate.CreateDrawingContext(null))
            {
                var rect = new Rect(0, 0, tileBrushImage.PixelWidth, tileBrushImage.PixelHeight);

                context.Clear(Colors.Transparent);
                context.PushClip(calc.IntermediateClip);
                context.Transform = calc.IntermediateTransform;
                context.DrawImage(RefCountable.CreateUnownedNotClonable(tileBrushImage), 1, rect, rect, tileBrush.BitmapInterpolationMode);
                context.PopClip();
            }

            var tileTransform =
                tileBrush.TileMode != TileMode.None
                    ? SKMatrix.MakeTranslation(-(float)calc.DestinationRect.X, -(float)calc.DestinationRect.Y)
                    : SKMatrix.MakeIdentity();

            SKShaderTileMode tileX =
                tileBrush.TileMode == TileMode.None
                    ? SKShaderTileMode.Clamp
                    : tileBrush.TileMode == TileMode.FlipX || tileBrush.TileMode == TileMode.FlipXY
                        ? SKShaderTileMode.Mirror
                        : SKShaderTileMode.Repeat;

            SKShaderTileMode tileY =
                tileBrush.TileMode == TileMode.None
                    ? SKShaderTileMode.Clamp
                    : tileBrush.TileMode == TileMode.FlipY || tileBrush.TileMode == TileMode.FlipXY
                        ? SKShaderTileMode.Mirror
                        : SKShaderTileMode.Repeat;


            var image = intermediate.SnapshotImage();

            paintWrapper.AddDisposable(image);

            using (var shader = image.ToShader(tileX, tileY, tileTransform))
            {
                paintWrapper.Paint.Shader = shader;
            }
        }
Ejemplo n.º 21
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]);
        }
Ejemplo n.º 22
0
        public override void Save(Stream stream)
        {
            using (var wic = new WicRenderTargetBitmapImpl(PixelSize, Dpi))
            {
                using (var dc = wic.CreateDrawingContext(null))
                {
                    dc.DrawBitmap(
                        RefCountable.CreateUnownedNotClonable(this),
                        1,
                        new Rect(PixelSize.ToSizeWithDpi(Dpi.X)),
                        new Rect(PixelSize.ToSizeWithDpi(Dpi.X)));
                }

                wic.Save(stream);
            }
        }
Ejemplo n.º 23
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;
                }
            }
        }
Ejemplo n.º 24
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;
     }
 }
Ejemplo n.º 25
0
        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))));
        }
Ejemplo n.º 26
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();
                }
            }
        }
Ejemplo n.º 27
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))
                {
                    context.Clear(Colors.Transparent);
                    Bitmap.Dispose();
                    Bitmap  = resized;
                    Scaling = scaling;
                    Size    = size;
                    IsEmpty = true;
                }
            }
        }
Ejemplo n.º 28
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);
        }
Ejemplo n.º 29
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);
        }
Ejemplo n.º 30
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);
        }
Ejemplo n.º 31
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);
        }