Пример #1
0
        public unsafe void ReferencesCountedCorrectly()
        {
            var colorspace = SKColorSpace.CreateRgb(
                new SKColorSpaceTransferFn {
                A = 0.1f, B = 0.2f, C = 0.3f, D = 0.4f, E = 0.5f, F = 0.6f
            },
                SKMatrix44.CreateIdentity());
            var handle = colorspace.Handle;

            Assert.Equal(1, handle.GetReferenceCount(false));

            var info = new SKImageInfo(1, 1, SKImageInfo.PlatformColorType, SKAlphaType.Premul, colorspace);

            Assert.Equal(1, handle.GetReferenceCount(false));

            var pixels = new byte[info.BytesSize];

            fixed(byte *p = pixels)
            {
                var pixmap = new SKPixmap(info, (IntPtr)p);

                Assert.Equal(2, handle.GetReferenceCount(false));

                pixmap.Dispose();
                Assert.Equal(1, handle.GetReferenceCount(false));
            }

            GC.KeepAlive(colorspace);
        }
Пример #2
0
        public void SameColorSpaceCreatedDifferentWaysAreTheSameObject()
        {
            var colorspace1 = SKColorSpace.CreateSrgbLinear();

            Assert.Equal("SkiaSharp.SKColorSpace+SKColorSpaceStatic", colorspace1.GetType().FullName);
            Assert.Equal(2, colorspace1.GetReferenceCount());

            var colorspace2 = SKColorSpace.CreateRgb(SKNamedGamma.Linear, SKColorSpaceGamut.Srgb);

            Assert.Equal("SkiaSharp.SKColorSpace+SKColorSpaceStatic", colorspace2.GetType().FullName);
            Assert.Equal(2, colorspace2.GetReferenceCount());

            Assert.Same(colorspace1, colorspace2);

            var colorspace3 = SKColorSpace.CreateRgb(
                new SKColorSpaceTransferFn {
                A = 0.6f, B = 0.5f, C = 0.4f, D = 0.3f, E = 0.2f, F = 0.1f
            },
                SKMatrix44.CreateIdentity());

            Assert.NotSame(colorspace1, colorspace3);

            colorspace3.Dispose();
            Assert.True(colorspace3.IsDisposed);
            Assert.Equal(2, colorspace1.GetReferenceCount());

            colorspace2.Dispose();
            Assert.False(colorspace2.IsDisposed);
            Assert.Equal(2, colorspace1.GetReferenceCount());

            colorspace1.Dispose();
            Assert.False(colorspace1.IsDisposed);
            Assert.Equal(2, colorspace1.GetReferenceCount());
        }
Пример #3
0
        private void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
        {
            SKImageInfo info    = args.Info;
            SKSurface   surface = args.Surface;
            SKCanvas    canvas  = surface.Canvas;

            canvas.Clear();

            float xCenter = info.Width / 2;
            float yCenter = info.Height / 2;

            SKMatrix   matrix   = SKMatrix.MakeTranslation(-xCenter, -yCenter);
            SKMatrix44 matrix44 = SKMatrix44.CreateIdentity();

            matrix44.PostConcat(SKMatrix44.CreateRotationDegrees(1, 0, 0, _Deg));

            SKMatrix44 perspectiveMatrix = SKMatrix44.CreateIdentity();

            perspectiveMatrix[3, 2] = -1 / 5000f;
            matrix44.PostConcat(perspectiveMatrix);

            SKMatrix.PostConcat(ref matrix, matrix44.Matrix);
            SKMatrix.PostConcat(ref matrix, SKMatrix.MakeTranslation(xCenter, yCenter));
            canvas.SetMatrix(matrix);
            float xBitmap = xCenter - CurrentBitmap.Width / 2;
            float yBitmap = yCenter - CurrentBitmap.Height / 2;

            canvas.DrawBitmap(CurrentBitmap, xBitmap, yBitmap);
        }
Пример #4
0
        protected override async Task OnInit()
        {
            // create the base and step 3D rotation matrices (around the y-axis)
            rotationMatrix = SKMatrix44.CreateIdentity();
            rotationStep   = SKMatrix44.CreateRotationDegrees(0, 1, 0, 5);

            await base.OnInit();
        }
Пример #5
0
        public virtual void Update(SKCanvas canvas, long absoluteElapsedMillis)
        {
            var elapsedMillis = absoluteElapsedMillis - _absoluteElapsedMillisPrevious;

            if (_absoluteElapsedMillisPrevious == 0)
            {
                _absoluteElapsedMillisPrevious = absoluteElapsedMillis;
                return;
            }

            _internalAbsoluteMillis       += elapsedMillis;
            _absoluteElapsedMillisPrevious = absoluteElapsedMillis;

            canvas.Save();

            // Traversed distance = speed x time
            var dist = TranslationSpeed * _internalAbsoluteMillis * 0.001;

            // New position
            var deg2radFactor = 0.0174533;
            var angle         = Direction * deg2radFactor;

            Position = InitialPosition + new SKPoint
            {
                X = (float)(dist * Math.Cos(angle)),
                Y = (float)(dist * Math.Sin(angle))
            };

            var matrix = SKMatrix.CreateTranslation(-Position.X, -Position.Y);

            // New Orientation
            Orientation = InitialOrientation + new SKPoint3
            {
                X = _internalAbsoluteMillis * 0.001f * RotationSpeed.X,
                Y = _internalAbsoluteMillis * 0.001f * RotationSpeed.Y,
                Z = _internalAbsoluteMillis * 0.001f * RotationSpeed.Z
            };

            var matrix44 = SKMatrix44.CreateIdentity();

            matrix44.PostConcat(SKMatrix44.CreateRotationDegrees(1, 0, 0, Orientation.X));
            matrix44.PostConcat(SKMatrix44.CreateRotationDegrees(0, 1, 0, Orientation.Y));
            matrix44.PostConcat(SKMatrix44.CreateRotationDegrees(0, 0, 1, Orientation.Z));

            // Apply transforms
            matrix = matrix.PostConcat(matrix44.Matrix);
            matrix = matrix.PostConcat(SKMatrix.CreateTranslation(Position.X, Position.Y));
            canvas.SetMatrix(matrix);

            Draw(canvas);

            canvas.Restore();
        }
Пример #6
0
        public void Matrix44CreatesIdentity()
        {
            var matrix = SKMatrix44.CreateIdentity();

            var expectedRowMajor = new float[] {
                1, 0, 0, 0,
                0, 1, 0, 0,
                0, 0, 1, 0,
                0, 0, 0, 1,
            };
            var rowMajor = matrix.ToRowMajor();

            Assert.Equal(expectedRowMajor, rowMajor);
        }
        void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
        {
            SKImageInfo info    = args.Info;
            SKSurface   surface = args.Surface;
            SKCanvas    canvas  = surface.Canvas;

            canvas.Clear();

            // Find center of canvas
            float xCenter = info.Width / 2;
            float yCenter = info.Height / 2;

            // Translate center to origin
            SKMatrix matrix = SKMatrix.MakeTranslation(-xCenter, -yCenter);

            // Scale so text fits
            float scale = Math.Min(info.Width / textBounds.Width,
                                   info.Height / textBounds.Height);

            SKMatrix.PostConcat(ref matrix, SKMatrix.MakeScale(scale, scale));

            // Calculate composite 3D transforms
            float depth = 0.75f * scale * textBounds.Width;

            SKMatrix44 matrix44 = SKMatrix44.CreateIdentity();

            matrix44.PostConcat(SKMatrix44.CreateRotationDegrees(1, 0, 0, xRotationDegrees));
            matrix44.PostConcat(SKMatrix44.CreateRotationDegrees(0, 1, 0, yRotationDegrees));
            matrix44.PostConcat(SKMatrix44.CreateRotationDegrees(0, 0, 1, zRotationDegrees));

            SKMatrix44 perspectiveMatrix = SKMatrix44.CreateIdentity();

            perspectiveMatrix[3, 2] = -1 / depth;
            matrix44.PostConcat(perspectiveMatrix);

            // Concatenate with 2D matrix
            SKMatrix.PostConcat(ref matrix, matrix44.Matrix);

            // Translate back to center
            SKMatrix.PostConcat(ref matrix,
                                SKMatrix.MakeTranslation(xCenter, yCenter));

            // Set the matrix and display the text
            canvas.SetMatrix(matrix);
            float xText = xCenter - textBounds.MidX;
            float yText = yCenter - textBounds.MidY;

            canvas.DrawText(text, xText, yText, textPaint);
        }
Пример #8
0
        static SKMatrix44 GetMatrix(VoxelData voxelData, int size)
        {
            var r      = 1.61803398875f;
            var d      = size / (r * voxelData.size.MaxDimension);
            var tran   = SKMatrix44.CreateTranslate(size * 0.5f, size * 0.5f, 0);
            var rotx   = SKMatrix44.CreateRotationDegrees(1, 0, 0, -26f);
            var roty   = SKMatrix44.CreateRotationDegrees(0, 1, 0, 45);
            var matrix = SKMatrix44.CreateIdentity();

            matrix.PreConcat(tran);
            matrix.PreConcat(rotx);
            matrix.PreConcat(roty);
            matrix.PreScale(d, -d, d);
            matrix.PreTranslate(-voxelData.size.X * 0.5f, -voxelData.size.Z * 0.5f, voxelData.size.Y * 0.5f);
            return(matrix);
        }
Пример #9
0
        void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
        {
            SKImageInfo info    = args.Info;
            SKSurface   surface = args.Surface;
            SKCanvas    canvas  = surface.Canvas;

            canvas.Clear();

            // Find center of canvas
            float xCenter = info.Width / 2;
            float yCenter = info.Height / 2;

            // Translate center to origin
            SKMatrix matrix = SKMatrix.MakeTranslation(-xCenter, -yCenter);

            // Use 3D matrix for 3D rotations and perspective
            SKMatrix44 matrix44 = SKMatrix44.CreateIdentity();

            matrix44.PostConcat(SKMatrix44.CreateRotationDegrees(1, 0, 0, (float)xRotateSlider.Value));
            matrix44.PostConcat(SKMatrix44.CreateRotationDegrees(0, 1, 0, (float)yRotateSlider.Value));
            matrix44.PostConcat(SKMatrix44.CreateRotationDegrees(0, 0, 1, (float)zRotateSlider.Value));

            SKMatrix44 perspectiveMatrix = SKMatrix44.CreateIdentity();

            perspectiveMatrix[3, 2] = -1 / (float)depthSlider.Value;
            matrix44.PostConcat(perspectiveMatrix);

            // Concatenate with 2D matrix
            SKMatrix.PostConcat(ref matrix, matrix44.Matrix);

            // Translate back to center
            SKMatrix.PostConcat(ref matrix,
                                SKMatrix.MakeTranslation(xCenter, yCenter));

            // Set the matrix and display the bitmap
            canvas.SetMatrix(matrix);
            float xBitmap = xCenter - bitmap.Width / 2;
            float yBitmap = yCenter - bitmap.Height / 2;

            canvas.DrawBitmap(bitmap, xBitmap, yBitmap);
        }
Пример #10
0
        public void ImageInfoColorSpaceIsReferencedCorrectly()
        {
            VerifyImmediateFinalizers();

            var img = DoWork(out var colorspaceHandle);

            CollectGarbage();

            Assert.Equal(1, colorspaceHandle.GetReferenceCount(true));

            Check();

            CollectGarbage();

            Assert.Equal(1, colorspaceHandle.GetReferenceCount(true));

            void Check()
            {
                var peek = img.PeekPixels();

                Assert.Equal(2, colorspaceHandle.GetReferenceCount(true));

                // get the info and color space
                var info1 = peek.Info;
                var cs1   = info1.ColorSpace;

                Assert.Equal(3, colorspaceHandle.GetReferenceCount(true));
                Assert.NotNull(cs1);

                // get the info and color space again and make sure we are all using the same things
                var info2 = peek.Info;
                var cs2   = info2.ColorSpace;

                Assert.Equal(3, colorspaceHandle.GetReferenceCount(true));
                Assert.NotNull(cs2);

                Assert.Same(cs1, cs2);
            }

            SKImage DoWork(out IntPtr handle)
            {
                var colorspace = SKColorSpace.CreateRgb(
                    new SKColorSpaceTransferFn {
                    A = 0.6f, B = 0.5f, C = 0.4f, D = 0.3f, E = 0.2f, F = 0.1f
                },
                    SKMatrix44.CreateIdentity());

                Assert.NotNull(colorspace);

                handle = colorspace.Handle;
                Assert.Equal(1, handle.GetReferenceCount(true));

                var info = new SKImageInfo(100, 100, SKImageInfo.PlatformColorType, SKAlphaType.Premul, colorspace);

                Assert.Equal(1, handle.GetReferenceCount(true));

                var image = SKImage.Create(info);

                Assert.Equal(2, handle.GetReferenceCount(true));

                return(image);
            }
        }
Пример #11
0
        public void ColorSpaceIsNotDisposedPrematurely()
        {
            VerifyImmediateFinalizers();

            var img = DoWork(out var colorSpaceHandle, out var weakColorspace);

            CheckBeforeCollection(colorSpaceHandle);

            CheckExistingImage(3, img, colorSpaceHandle);

            CollectGarbage();

            Assert.Null(weakColorspace.Target);
            Assert.Equal(1, colorSpaceHandle.GetReferenceCount(true));

            CheckExistingImage(2, img, colorSpaceHandle);

            CollectGarbage();

            Assert.Null(weakColorspace.Target);
            Assert.Equal(1, colorSpaceHandle.GetReferenceCount(true));

            CollectGarbage();

            Assert.Equal(1, colorSpaceHandle.GetReferenceCount(true));

            GC.KeepAlive(img);

            void CheckBeforeCollection(IntPtr csh)
            {
                Assert.NotNull(weakColorspace.Target);
                Assert.Equal(2, csh.GetReferenceCount(true));
            }

            void CheckExistingImage(int expected, SKImage image, IntPtr csh)
            {
                var peek = image.PeekPixels();

                Assert.Equal(expected, csh.GetReferenceCount(true));

                var info = peek.Info;

                Assert.Equal(3, csh.GetReferenceCount(true));

                var cs = info.ColorSpace;

                Assert.Equal(3, csh.GetReferenceCount(true));
                Assert.NotNull(cs);
            }

            SKImage DoWork(out IntPtr handle, out WeakReference weak)
            {
                var colorspace = SKColorSpace.CreateRgb(
                    new SKColorSpaceTransferFn {
                    A = 0.1f, B = 0.2f, C = 0.3f, D = 0.4f, E = 0.5f, F = 0.6f
                },
                    SKMatrix44.CreateIdentity());

                Assert.NotNull(colorspace);

                handle = colorspace.Handle;
                weak   = new WeakReference(colorspace);

                Assert.Equal(1, handle.GetReferenceCount(true));

                var info = new SKImageInfo(100, 100, SKImageInfo.PlatformColorType, SKAlphaType.Premul, colorspace);

                Assert.Equal(1, handle.GetReferenceCount(true));

                var image = SKImage.Create(info);

                Assert.Equal(2, handle.GetReferenceCount(true));

                return(image);
            }
        }
Пример #12
0
        void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
        {
            SKImageInfo info    = args.Info;
            SKSurface   surface = args.Surface;
            SKCanvas    canvas  = surface.Canvas;

            canvas.Clear();

            string strName   = Apartment.Name;
            string strRating = Apartment.RatesAverage?.ToString();

            // Create an SKPaint object to display the text
            SKPaint textPaint = new SKPaint
            {
                Typeface = SKTypeface.FromFamilyName("Brandon_reg"),
                Color    = SKColors.Black
            };

            // Adjust TextSize property so text is 90% of screen width
            textPaint.TextSize = 0.05f * info.Width;

            // Find the text bounds
            SKRect textBounds = new SKRect();

            textPaint.MeasureText(strName, ref textBounds);

            // Calculate offsets to center the text on the screen
            float xTextName = 0.1f * info.Width;
            float yTextName = info.Height / 2 - textBounds.MidY;

            float yTextRating = yTextName + 1.2f * textBounds.Height;


            // And draw the text


            float xCenter = info.Width / 2;
            float yCenter = info.Height / 2;

            // Translate center to origin
            SKMatrix matrix = SKMatrix.MakeTranslation(-xCenter, -yCenter);

            // Use 3D matrix for 3D rotations and perspective
            SKMatrix44 matrix44 = SKMatrix44.CreateIdentity();

            matrix44.PostConcat(SKMatrix44.CreateRotationDegrees(1, 0, 0, angle));


            SKMatrix44 perspectiveMatrix = SKMatrix44.CreateIdentity();

            perspectiveMatrix[3, 2] = -1 / (float)2000;
            matrix44.PostConcat(perspectiveMatrix);

            // Concatenate with 2D matrix
            SKMatrix.PostConcat(ref matrix, matrix44.Matrix);

            // Translate back to center
            SKMatrix.PostConcat(ref matrix,
                                SKMatrix.MakeTranslation(xCenter, yCenter));

            // Set the matrix and display the bitmap
            canvas.SetMatrix(matrix);

            // Create a new SKRect object for the frame around the text
            SKRect frameRect = SKRect.Create(0.05f * info.Width, 0, 0.9f * info.Width, info.Height);

            // Create an SKPaint object to display the frame
            SKPaint framePaint = new SKPaint
            {
                Style       = SKPaintStyle.Stroke,
                StrokeWidth = 5,
                Color       = SKColors.Blue
            };

            SKPaint rectanglePaint = new SKPaint
            {
                Color = SKColors.Beige
            };

            // Draw one frame
            canvas.DrawRoundRect(frameRect, 20, 20, rectanglePaint);

            canvas.DrawText(strName, xTextName, yTextName, textPaint);
            canvas.DrawText($"{strRating}", xTextName, yTextRating, textPaint);


            if (bitmap != null)
            {
                var pictureFrame = SKRect.Create(info.Width * (float)0.7, 0, info.Width * (float)0.25, info.Height);

                float xBitmap = (float)0.8 * info.Width;
                float yBitmap = 0;

                canvas.DrawBitmap(bitmap, pictureFrame);
            }
        }
        private void FolderCanvas_PaintSurface(object sender, SkiaSharp.Views.Forms.SKPaintSurfaceEventArgs e)
        {
            SKImageInfo info    = e.Info;
            SKSurface   surface = e.Surface;
            SKCanvas    canvas  = surface.Canvas;

            canvas.Clear();


            float scaleFactor = 0.67f;
            float density     = scaleFactor * info.Size.Height / (float)this.Height;


            canvas.Save();

            using (SKPath backPath = SKPath.ParseSvgPathData(
                       "M87.909,9.917V4.02A4.032,4.032,0,0,0,83.889,0H23.72A4.032,4.032,0,0,0,19.7,4.02v5.9H13.338a4.529,4.529,0,0,0-3.822,4.02l.27,112.7a3.954,3.954,0,0,0,3.951,4.024H167.344a3.963,3.963,0,0,0,3.97-4.011l-.6-112.71a4.092,4.092,0,0,0-4.07-4.02Z"))
            {
                backPath.Transform(SKMatrix.MakeScale(density, density));
                backPath.GetTightBounds(out var backPathTightBounds);
                var translateXBackPath = info.Width * 0.5f - backPathTightBounds.MidX;
                var translateYbackPath = info.Height - backPathTightBounds.Bottom - 20f;
                canvas.Translate(translateXBackPath, translateYbackPath);
                fillPaint.Shader = SKShader.CreateLinearGradient(
                    new SKPoint(info.Width * 0.5f, 0),
                    new SKPoint(info.Width * 0.5f, info.Height),
                    new SKColor[] { SKColor.Parse(Color1), SKColor.Parse(Color2), SKColor.Parse(Color2) },
                    new float[] { 0, 0.37f, 1 },
                    SKShaderTileMode.Clamp
                    );
                canvas.DrawPath(backPath, fillPaint);
            }

            canvas.Restore();

            canvas.Save();
            using (SKPath backFilePath = SKPath.ParseSvgPathData("M174.079,150.908H29.155l-.4-114.144H174.481Z"))
            {
                backFilePath.Transform(SKMatrix.MakeScale(density, density));
                backFilePath.GetTightBounds(out var backFilePathTightBounds);
                var translateXBackFilePath = info.Width * 0.5f - backFilePathTightBounds.MidX;
                var translateYbackFilePath = info.Height - backFilePathTightBounds.Bottom - 20f;
                canvas.Translate(translateXBackFilePath, translateYbackFilePath);
                fillPaint.Shader = SKShader.CreateLinearGradient(
                    new SKPoint(info.Width * 0.5f, 0),
                    new SKPoint(info.Width * 0.5f, info.Height),
                    new SKColor[] { SKColor.Parse("#e6e6e6"), SKColor.Parse("#e8e8e8"), SKColor.Parse("#f0f0f0"), SKColor.Parse("#f2f2f2") },
                    new float[] { 0, 0.633f, 0.949f, 1 },
                    SKShaderTileMode.Clamp
                    );
                canvas.DrawPath(backFilePath, fillPaint);
            }
            canvas.Restore();

            canvas.Save();
            using (SKPath frontFilePath = SKPath.ParseSvgPathData("M170.491,158.681H25.567L22.753,49.764H173.3Z"))
            {
                frontFilePath.Transform(SKMatrix.MakeScale(density, density));
                frontFilePath.GetTightBounds(out var frontFilePathTightBounds);
                var translateXFrontFilePath = info.Width * 0.5f - frontFilePathTightBounds.MidX;
                var translateYFrontFilePath = info.Height - frontFilePathTightBounds.Bottom - 20f;
                canvas.Translate(translateXFrontFilePath, translateYFrontFilePath);
                fillPaint.Shader = SKShader.CreateLinearGradient(
                    new SKPoint(info.Width * 0.5f, 0),
                    new SKPoint(info.Width * 0.5f, info.Height),
                    new SKColor[] { SKColor.Parse("#cccccc"), SKColor.Parse("#cecece"), SKColor.Parse("#d6d6d6"), SKColor.Parse("#e3e3e3"), SKColor.Parse("#f6f6f6"), SKColor.Parse("#ffffff") },
                    new float[] { 0, 0.427f, 0.64f, 0.806f, 0.947f, 1 },
                    SKShaderTileMode.Clamp
                    );
                canvas.DrawPath(frontFilePath, fillPaint);
            }
            canvas.Restore();

            canvas.Save();
            using (SKPath frontPath = SKPath.ParseSvgPathData("M165.433,164.917a4.106,4.106,0,0,1-4.089,4.011H7.735a4.578,4.578,0,0,1-4.325-4.292L0,68.019A3.948,3.948,0,0,1,3.95,64H164.895a3.948,3.948,0,0,1,3.95,4.02Z"))
            {
                frontPath.Transform(SKMatrix.MakeScale(density, density));
                frontPath.GetTightBounds(out var frontPathTightBounds);

                fillPaint.Shader = SKShader.CreateLinearGradient(
                    new SKPoint(info.Width * 0.5f, 0),
                    new SKPoint(info.Width * 0.5f, info.Height),
                    new SKColor[] { SKColor.Parse(Color3), SKColor.Parse(Color4), SKColor.Parse(Color5), SKColor.Parse(Color6) },
                    new float[] { 0, 0.595f, 0.957f, 1 },
                    SKShaderTileMode.Clamp
                    );

                SKMatrix   matrix   = SKMatrix.MakeTranslation(-frontPathTightBounds.Right, -frontPathTightBounds.Bottom);
                SKMatrix44 matrix44 = SKMatrix44.CreateIdentity();
                matrix44.PostConcat(SKMatrix44.CreateRotationDegrees(1, 0, 0, -FrontPathDegree));
                matrix44.PostConcat(SKMatrix44.CreateRotationDegrees(0, 1, 0, -0.5f * FrontPathDegree));
                matrix44.PostConcat(SKMatrix44.CreateRotationDegrees(0, 0, 1, 0));
                SKMatrix.PostConcat(ref matrix, matrix44.Matrix);
                SKMatrix.PostConcat(ref matrix, SKMatrix.MakeTranslation(frontPathTightBounds.Right, frontPathTightBounds.Bottom));
                canvas.SetMatrix(matrix);

                var translateXFrontPath = info.Width * 0.5f - frontPathTightBounds.MidX;
                var translateYFrontPath = info.Height - frontPathTightBounds.Bottom - 20f;
                canvas.Translate(translateXFrontPath, translateYFrontPath);
                canvas.DrawPath(frontPath, fillPaint);
            }
            canvas.Restore();
        }