Beispiel #1
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);
        }
Beispiel #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());
        }
Beispiel #3
0
        public void TransformConvertsToMatrix()
        {
            var matrix44 = SKMatrix44.CreateRotationDegrees(0, 0, 1, 45);
            var matrix   = SKMatrix.CreateRotationDegrees(45);

            Assert.Equal(matrix.Values, matrix44.Matrix.Values);
        }
Beispiel #4
0
        public static SKMatrix44 CreateScale(float x, float y, float z)
        {
            var matrix = new SKMatrix44();

            matrix.SetScale(x, y, z);
            return(matrix);
        }
Beispiel #5
0
        public void Matrix44Inverts()
        {
            var rowMajor = new float[] {
                1, 2, 3, 0,
                0, 1, 4, 0,
                5, 6, 1, 0,
                0, 0, 0, 1,
            };
            var expectedRowMajor = new float[] {
                -11.5f, 8, 2.5f, 0,
                10, -7, -2, 0,
                -2.5f, 2, 0.5f, 0,
                0, 0, 0, 1,
            };
            var determinant = 2f;

            var matrix = SKMatrix44.FromRowMajor(rowMajor);

            Assert.Equal(rowMajor, matrix.ToRowMajor());
            Assert.Equal(determinant, matrix.Determinant());

            var inverted = matrix.Invert();

            Assert.Equal(1f / determinant, inverted.Determinant());

            var actualRowMajor = inverted.ToRowMajor();

            Assert.Equal(expectedRowMajor, actualRowMajor);
        }
Beispiel #6
0
        public static SKMatrix44 CreateRotation(float x, float y, float z, float radians)
        {
            var matrix = new SKMatrix44();

            matrix.SetRotationAbout(x, y, z, radians);
            return(matrix);
        }
Beispiel #7
0
        public static SKMatrix44 ToSKMatrix44(this Matrix3x2 m)
        {
            var ret = new SKMatrix44();

            ret[0, 0] = m.M11;
            ret[1, 0] = m.M12;
            ret[2, 0] = 0;
            ret[3, 0] = 0;

            ret[0, 1] = m.M21;
            ret[1, 1] = m.M22;
            ret[2, 1] = 0;
            ret[3, 1] = 0;

            ret[0, 2] = m.M31;
            ret[1, 2] = m.M32;
            ret[2, 2] = 1;
            ret[3, 2] = 0;

            ret[0, 3] = 0;
            ret[1, 3] = 0;
            ret[2, 3] = 0;
            ret[3, 3] = 1;

            return(ret);
        }
Beispiel #8
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);
        }
Beispiel #9
0
        public static SKMatrix44 ToSKMatrix44(this Matrix4x4 m)
        {
            var ret = new SKMatrix44();

            ret[0, 0] = m.M11;
            ret[1, 0] = m.M12;
            ret[2, 0] = m.M13;
            ret[3, 0] = m.M14;

            ret[0, 1] = m.M21;
            ret[1, 1] = m.M22;
            ret[2, 1] = m.M23;
            ret[3, 1] = m.M24;

            ret[0, 2] = m.M31;
            ret[1, 2] = m.M32;
            ret[2, 2] = m.M33;
            ret[3, 2] = m.M34;

            ret[0, 3] = m.M41;
            ret[1, 3] = m.M42;
            ret[2, 3] = m.M43;
            ret[3, 3] = m.M44;

            return(ret);
        }
Beispiel #10
0
        public static SKMatrix44 CreateRotationDegrees(float x, float y, float z, float degrees)
        {
            var matrix = new SKMatrix44();

            matrix.SetRotationAboutDegrees(x, y, z, degrees);
            return(matrix);
        }
Beispiel #11
0
        public static SKMatrix44 FromColumnMajor(float [] src)
        {
            var matrix = new SKMatrix44();

            matrix.SetColumnMajor(src);
            return(matrix);
        }
Beispiel #12
0
        public static SKMatrix44 CreateIdentity()
        {
            var matrix = new SKMatrix44();

            matrix.SetIdentity();
            return(matrix);
        }
Beispiel #13
0
        protected override async Task OnInit()
        {
            // create the base and step 3D rotation matrices (around the y-axis)
            rotationMatrix = SKMatrix44.CreateRotationDegrees(0, 1, 0, 30);
            rotationStep   = SKMatrix44.CreateRotationDegrees(0, 1, 0, 5);

            await base.OnInit();
        }
 public static string Debug(this SKMatrix44 m)
 {
     return
         ($"{m[0, 0]}, {m[0, 1]}, {m[0, 2]}, {m[0, 3]}\n" +
          $"{m[1, 0]}, {m[1, 1]}, {m[1, 2]}, {m[1, 3]}\n" +
          $"{m[2, 0]}, {m[2, 1]}, {m[2, 2]}, {m[2, 3]}\n" +
          $"{m[3, 0]}, {m[3, 1]}, {m[3, 2]}, {m[3, 3]}\n");
 }
Beispiel #15
0
        private static void AssertMatrix(float[] expected, SKMatrix44 actual)
        {
            var actualArray = actual
                              .ToRowMajor()
                              .Select(x => (float)Math.Round(x, 5))
                              .ToArray();

            Assert.Equal(expected, actualArray);
        }
Beispiel #16
0
        public void PostConcat(SKMatrix44 m)
        {
            if (m == null)
            {
                throw new ArgumentNullException(nameof(m));
            }

            SkiaApi.sk_matrix44_post_concat(Handle, m.Handle);
        }
Beispiel #17
0
        public ThreeDRotation()
        {
            Log.Debug("Demo", "Enter");
            InitializeComponent();

            // create the base and step 3D rotation matrices (around the y-axis)
            rotationMatrix = SKMatrix44.CreateRotationDegrees(0, 1, 0, 30);
            rotationStep   = SKMatrix44.CreateRotationDegrees(0, 1, 0, 5);
        }
Beispiel #18
0
        public bool Invert(SKMatrix44 inverse)
        {
            if (inverse == null)
            {
                throw new ArgumentNullException(nameof(inverse));
            }

            return(SkiaApi.sk_matrix44_invert(Handle, inverse.Handle));
        }
Beispiel #19
0
        public void TranslationMapsScalars()
        {
            var matrixTranslate = SKMatrix44.CreateTranslation(10, 20, 0);

            var resultTranslateZero  = matrixTranslate.MapScalars(0, 0, 0, 1);
            var resultTranslateValue = matrixTranslate.MapScalars(5, 25, 0, 1);

            Assert.Equal(new[] { 10f, 20f, 0f, 1f }, resultTranslateZero);
            Assert.Equal(new[] { 15f, 45f, 0f, 1f }, resultTranslateValue);
        }
Beispiel #20
0
        public void TranslationMapsPoints()
        {
            var matrixTranslate = SKMatrix44.CreateTranslation(10, 20, 0);

            var resultTranslateZero  = matrixTranslate.MapPoint(SKPoint.Empty);
            var resultTranslateValue = matrixTranslate.MapPoint(new SKPoint(5, 25));

            Assert.Equal(new SKPoint(10f, 20f), resultTranslateZero);
            Assert.Equal(new SKPoint(15f, 45f), resultTranslateValue);
        }
Beispiel #21
0
        public void RotationMapsScalars()
        {
            var matrixRotate = SKMatrix44.CreateRotationDegrees(0, 1, 0, 90);

            var resultRotateZero  = matrixRotate.MapScalars(0, 0, 0, 1);
            var resultRotateValue = matrixRotate.MapScalars(5, 25, 0, 1);

            Assert.Equal(new[] { 0f, 0f, 0f, 1f }, resultRotateZero);
            AssertSimilar(new[] { 0f, 25f, -5f, 1f }, resultRotateValue, PRECISION);
        }
Beispiel #22
0
        public void RotationMapsPoints()
        {
            var matrixRotate = SKMatrix44.CreateRotationDegrees(0, 1, 0, 90);

            var resultRotateZero  = matrixRotate.MapPoint(SKPoint.Empty);
            var resultRotateValue = matrixRotate.MapPoint(new SKPoint(5, 25));

            Assert.Equal(new SKPoint(0f, 0f), resultRotateZero);
            Assert.Equal(0, resultRotateValue.X, PRECISION);
            Assert.Equal(25, resultRotateValue.Y, PRECISION);
        }
Beispiel #23
0
 public SKColorSpace GetSKColorSpace()
 {
     if (skColorSpace == null)
     {
         skColorSpace = SKColorSpace.CreateIcc(Profile.GetBody(true).ToByteArray());
         xyzD50       = skColorSpace.FromXyzD50();
         skColorSpace.GetNumericalTransferFunction(out var spaceTransfer);
         transfer = spaceTransfer.Invert();
     }
     return(skColorSpace);
 }
Beispiel #24
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();
        }
Beispiel #25
0
 public SKMatrix44(SKMatrix44 src)
     : this(IntPtr.Zero, true)
 {
     if (src == null)
     {
         throw new ArgumentNullException(nameof(src));
     }
     Handle = SkiaApi.sk_matrix44_new_copy(src.Handle);
     if (Handle == IntPtr.Zero)
     {
         throw new InvalidOperationException("Unable to create a new SKMatrix44 instance.");
     }
 }
Beispiel #26
0
        public static bool Equal(SKMatrix44 left, SKMatrix44 right)
        {
            if (left == null)
            {
                throw new ArgumentNullException(nameof(left));
            }
            if (right == null)
            {
                throw new ArgumentNullException(nameof(right));
            }

            return(SkiaApi.sk_matrix44_equals(left.Handle, right.Handle));
        }
Beispiel #27
0
        public void SetConcat(SKMatrix44 a, SKMatrix44 b)
        {
            if (a == null)
            {
                throw new ArgumentNullException(nameof(a));
            }
            if (b == null)
            {
                throw new ArgumentNullException(nameof(b));
            }

            SkiaApi.sk_matrix44_set_concat(Handle, a.Handle, b.Handle);
        }
Beispiel #28
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);
        }
        /// Update the information associated with the node with the given `id`.
        ///
        /// The semantics nodes form a tree, with the root of the tree always having
        /// an id of zero. The `childrenInTraversalOrder` and `childrenInHitTestOrder`
        /// are the ids of the nodes that are immediate children of this node. The
        /// former enumerates children in traversal order, and the latter enumerates
        /// the same children in the hit test order. The two lists must have the same
        /// length and contain the same ids. They may only differ in the order the
        /// ids are listed in. For more information about different child orders, see
        /// [DebugSemanticsDumpOrder].
        ///
        /// The system retains the nodes that are currently reachable from the root.
        /// A given update need not contain information for nodes that do not change
        /// in the update. If a node is not reachable from the root after an update,
        /// the node will be discarded from the tree.
        ///
        /// The `flags` are a bit field of [SemanticsFlag]s that apply to this node.
        ///
        /// The `actions` are a bit field of [SemanticsAction]s that can be undertaken
        /// by this node. If the user wishes to undertake one of these actions on this
        /// node, the [Window.onSemanticsAction] will be called with `id` and one of
        /// the possible [SemanticsAction]s. Because the semantics tree is maintained
        /// asynchronously, the [Window.onSemanticsAction] callback might be called
        /// with an action that is no longer possible.
        ///
        /// The `label` is a string that describes this node. The `value` property
        /// describes the current value of the node as a string. The `increasedValue`
        /// string will become the `value` string after a [SemanticsAction.increase]
        /// action is performed. The `decreasedValue` string will become the `value`
        /// string after a [SemanticsAction.decrease] action is performed. The `hint`
        /// string describes what result an action performed on this node has. The
        /// reading direction of all these strings is given by `textDirection`.
        ///
        /// The fields 'textSelectionBase' and 'textSelectionExtent' describe the
        /// currently selected text within `value`.
        ///
        /// For scrollable nodes `scrollPosition` describes the current scroll
        /// position in logical pixel. `scrollExtentMax` and `scrollExtentMin`
        /// describe the maximum and minimum in-rage values that `scrollPosition` can
        /// be. Both or either may be infinity to indicate unbound scrolling. The
        /// value for `scrollPosition` can (temporarily) be outside this range, for
        /// example during an overscroll. `scrollChildren` is the count of the
        /// total number of child nodes that contribute semantics and `scrollIndex`
        /// is the index of the first visible child node that contributes semantics.
        ///
        /// The `rect` is the region occupied by this node in its own coordinate
        /// system.
        ///
        /// The `transform` is a matrix that maps this node's coordinate system into
        /// its parent's coordinate system.
        public void updateNode(
            int id = 0,
            SemanticsFlag flags     = 0,
            SemanticsAction actions = 0,
            int textSelectionBase   = 0,
            int textSelectionExtent = 0,
            int scrollChildren      = 0,
            int scrollIndex         = 0,
            double scrollPosition   = 0.0,
            double scrollExtentMax  = 0.0,
            double scrollExtentMin  = 0.0,
            Rect rect                           = null,
            String label                        = null,
            String hint                         = null,
            String value                        = null,
            String increasedValue               = null,
            String decreasedValue               = null,
            TextDirection textDirection         = TextDirection.ltr,
            SKMatrix44 transform                = null,
            List <int> childrenInTraversalOrder = null,
            List <int> childrenInHitTestOrder   = null,
            List <int> additionalActions        = null)
        {
            SemanticsNode node = new SemanticsNode
            {
                id                         = id,
                flags                      = flags,
                actions                    = actions,
                textSelectionBase          = textSelectionBase,
                textSelectionExtent        = textSelectionExtent,
                scrollChildren             = scrollChildren,
                scrollIndex                = scrollIndex,
                scrollPosition             = scrollPosition,
                scrollExtentMax            = scrollExtentMax,
                scrollExtentMin            = scrollExtentMin,
                rect                       = rect.ToSKRect(),
                label                      = label,
                hint                       = hint,
                value                      = value,
                increasedValue             = increasedValue,
                decreasedValue             = decreasedValue,
                textDirection              = textDirection,
                transform                  = transform,
                childrenInTraversalOrder   = childrenInTraversalOrder,
                childrenInHitTestOrder     = childrenInHitTestOrder,
                customAccessibilityActions = additionalActions
            };

            nodes_[id] = node;
        }
Beispiel #30
0
        public SKMatrix44 Invert()
        {
            var inverse = new SKMatrix44();

            if (Invert(inverse))
            {
                return(inverse);
            }
            else
            {
                inverse.Dispose();
                return(null);
            }
        }