Author: borisk
Date: 2005-09-07 05:47:27 -0400 (Wed, 07 Sep 2005)
New Revision: 49621

Modified:
   trunk/mcs/class/System.Drawing/System.Drawing.Drawing2D/ChangeLog
   
trunk/mcs/class/System.Drawing/System.Drawing.Drawing2D/ExtendedGeneralPath.jvm.cs
   trunk/mcs/class/System.Drawing/System.Drawing.Drawing2D/PathData.cs
   trunk/mcs/class/System.Drawing/Test/DrawingTest/Test/ChangeLog
   trunk/mcs/class/System.Drawing/Test/DrawingTest/Test/GraphicsPath.cs
Log:
Improve GraphicsPath perfomance.

Modified: trunk/mcs/class/System.Drawing/System.Drawing.Drawing2D/ChangeLog
===================================================================
--- trunk/mcs/class/System.Drawing/System.Drawing.Drawing2D/ChangeLog   
2005-09-07 09:39:26 UTC (rev 49620)
+++ trunk/mcs/class/System.Drawing/System.Drawing.Drawing2D/ChangeLog   
2005-09-07 09:47:27 UTC (rev 49621)
@@ -1,3 +1,9 @@
+2005-09-07 Boris Kirzner <[EMAIL PROTECTED]>
+
+       * ExtendedGeneralPath.jvm.cs: Cache PathData and GeneralPath.
+       * PathData.cs: always return cloned points and types. Use internals
+       without clone for internal processing.
+
 2005-09-07 Vladimir Krasnov <[EMAIL PROTECTED]>
 
        * LinearGradientBrush.jvm.cs: FIxed transform methods

Modified: 
trunk/mcs/class/System.Drawing/System.Drawing.Drawing2D/ExtendedGeneralPath.jvm.cs
===================================================================
--- 
trunk/mcs/class/System.Drawing/System.Drawing.Drawing2D/ExtendedGeneralPath.jvm.cs
  2005-09-07 09:39:26 UTC (rev 49620)
+++ 
trunk/mcs/class/System.Drawing/System.Drawing.Drawing2D/ExtendedGeneralPath.jvm.cs
  2005-09-07 09:47:27 UTC (rev 49621)
@@ -60,6 +60,9 @@
                private int _coordsCount;
                private int _windingRule;
 
+               private PathData _pathData;
+               private GeneralPath _generalPath;
+
                const int INIT_SIZE = 20;
                const int EXPAND_MAX = 500;
 
@@ -99,11 +102,10 @@
                private GeneralPath GeneralPath
                {
                        get {
-                               // FIXME : cache general path
-                               PathIterator iter = getPathIterator (null);
-                               GeneralPath path = new GeneralPath ();
-                               path.append (iter, false);
-                               return path;
+                               if (_generalPath == null) {                     
        
+                                       _generalPath = GetGeneralPath ();
+                               }
+                               return _generalPath;
                        }
                }
 
@@ -139,25 +141,7 @@
                public int PointCount
                {
                        get {
-                               int count = 0;
-                               for (int i=0; i < TypesCount; i++)
-                                       switch (Types [i] & SEG_MASK) {
-                                               case SEG_CLOSE:
-                                                       break;
-                                               case SEG_MOVETO:
-                                                       count++;
-                                                       break;
-                                               case SEG_LINETO:
-                                                       count++;
-                                                       break;
-                                               case SEG_QUADTO:
-                                                       count+=2;
-                                                       break;
-                                               case SEG_CUBICTO:
-                                                       count+=3;
-                                                       break;
-                                       }
-                               return count;
+                               return CoordsCount / 2;
                        }
                }
 
@@ -165,52 +149,10 @@
                {
                        get 
                        {
-                               // FIXME : cache path data
-                               PathData pathData = new PathData();
-                               pathData.Types = new byte [PointCount];
-                               pathData.Points = new PointF [PointCount];
-                               int tpos = 0;
-                               int ppos = 0;
-                               int cpos = 0;
-                               byte marker;
-                               bool start;
-                               for (int i = 0; i < TypesCount; i++) {
-                                       sbyte segmentType = (sbyte)(Types [i] & 
SEG_MASK);
-
-                                       // set the masks and the markers
-                                       marker = ((Types [i] & SEG_MARKER) != 
0) ? (byte)PathPointType.PathMarker : (byte)0;
-                                       start = ((Types [i] & SEG_START) != 0);
-                                       
-                                       switch (segmentType) {
-                                               case SEG_CLOSE:
-                                                       pathData.Types [tpos - 
1] = (byte) (pathData.Types [tpos - 1] | (byte) PathPointType.CloseSubpath | 
marker);
-                                                       break;
-                                               case SEG_MOVETO:
-                                                       pathData.Types [tpos++] 
= (byte)((byte) PathPointType.Start | marker);
-                                                       pathData.Points 
[ppos++] = new PointF (Coords [cpos++], Coords [cpos++]);
-                                                       break;
-                                               case SEG_LINETO:
-                                                       pathData.Types [tpos++] 
= (byte) ((byte) PathPointType.Line | marker);
-                                                       pathData.Points 
[ppos++] = new PointF (Coords [cpos++], Coords [cpos++]);
-                                                       break;
-                                               case SEG_QUADTO:
-                                                       // FIXME : use 4 cp , 
two of which 
-                                                       pathData.Types [tpos++] 
= (byte)(byte) PathPointType.Bezier;
-                                                       pathData.Points 
[ppos++] = new PointF (Coords [cpos++], Coords [cpos++]);
-                                                       pathData.Types [tpos++] 
= (byte) ((byte)PathPointType.Bezier | marker);
-                                                       pathData.Points 
[ppos++] = new PointF (Coords [cpos++], Coords [cpos++]);
-                                                       break;
-                                               case SEG_CUBICTO:
-                                                       pathData.Types [tpos++] 
= (byte)(byte) PathPointType.Bezier3;
-                                                       pathData.Points 
[ppos++] = new PointF (Coords [cpos++], Coords [cpos++]);
-                                                       pathData.Types [tpos++] 
= (byte) PathPointType.Bezier3;
-                                                       pathData.Points 
[ppos++] = new PointF (Coords [cpos++], Coords [cpos++]);
-                                                       pathData.Types [tpos++] 
= (byte) ((byte)PathPointType.Bezier3 | marker);
-                                                       pathData.Points 
[ppos++] = new PointF (Coords [cpos++], Coords [cpos++]);
-                                                       break;
-                                       }
-                               }
-                               return pathData;
+                               if (_pathData == null)
+                                       _pathData = GetPathData ();
+                               
+                               return _pathData;
                        }
                }
 
@@ -218,6 +160,73 @@
 
                #region Methods
 
+               #region CachedData
+
+               private void ClearCache ()
+               {
+                       _pathData = null;
+                       _generalPath = null;
+               }
+
+               private GeneralPath GetGeneralPath ()
+               {
+                       PathIterator iter = getPathIterator (null);
+                       GeneralPath path = new GeneralPath ();
+                       path.append (iter, false);
+                       return path;
+               }
+
+               private PathData GetPathData ()
+               {
+                       PathData pathData = new PathData();
+                       pathData.Types = new byte [PointCount];
+                       pathData.Points = new PointF [PointCount];
+                       int tpos = 0;
+                       int ppos = 0;
+                       int cpos = 0;
+                       byte marker;
+                       bool start;
+                       for (int i = 0; i < TypesCount; i++) {
+                               sbyte segmentType = (sbyte)(Types [i] & 
SEG_MASK);
+
+                               // set the masks and the markers
+                               marker = ((Types [i] & SEG_MARKER) != 0) ? 
(byte)PathPointType.PathMarker : (byte)0;
+                               start = ((Types [i] & SEG_START) != 0);
+                               
+                               switch (segmentType) {
+                                       case SEG_CLOSE:
+                                               pathData.InternalTypes [tpos - 
1] = (byte) (pathData.InternalTypes [tpos - 1] | (byte) 
PathPointType.CloseSubpath | marker);
+                                               break;
+                                       case SEG_MOVETO:
+                                               pathData.InternalTypes [tpos++] 
= (byte)((byte) PathPointType.Start | marker);
+                                               pathData.InternalPoints 
[ppos++] = new PointF (Coords [cpos++], Coords [cpos++]);
+                                               break;
+                                       case SEG_LINETO:
+                                               pathData.InternalTypes [tpos++] 
= (byte) ((byte) PathPointType.Line | marker);
+                                               pathData.InternalPoints 
[ppos++] = new PointF (Coords [cpos++], Coords [cpos++]);
+                                               break;
+                                       case SEG_QUADTO:
+                                               // FIXME : use 4 cp , two of 
which 
+                                               pathData.InternalTypes [tpos++] 
= (byte)(byte) PathPointType.Bezier;
+                                               pathData.InternalPoints 
[ppos++] = new PointF (Coords [cpos++], Coords [cpos++]);
+                                               pathData.InternalTypes [tpos++] 
= (byte) ((byte)PathPointType.Bezier | marker);
+                                               pathData.InternalPoints 
[ppos++] = new PointF (Coords [cpos++], Coords [cpos++]);
+                                               break;
+                                       case SEG_CUBICTO:
+                                               pathData.InternalTypes [tpos++] 
= (byte)(byte) PathPointType.Bezier3;
+                                               pathData.InternalPoints 
[ppos++] = new PointF (Coords [cpos++], Coords [cpos++]);
+                                               pathData.InternalTypes [tpos++] 
= (byte) PathPointType.Bezier3;
+                                               pathData.InternalPoints 
[ppos++] = new PointF (Coords [cpos++], Coords [cpos++]);
+                                               pathData.InternalTypes [tpos++] 
= (byte) ((byte)PathPointType.Bezier3 | marker);
+                                               pathData.InternalPoints 
[ppos++] = new PointF (Coords [cpos++], Coords [cpos++]);
+                                               break;
+                               }
+                       }
+                       return pathData;
+               }
+
+               #endregion // CachedData
+
                public void append(Shape s)
                {
                        append (s, !LastFigureClosed);
@@ -227,6 +236,7 @@
 
                public void append(PathIterator pi, bool connect) 
                {
+                       ClearCache ();
                        float [] coords = new float [6];
                        while (!pi.isDone ()) {
                                switch (pi.currentSegment (coords)) {
@@ -274,6 +284,7 @@
 
                public void closePath() 
                {
+                       ClearCache ();
                        if (_typesCount == 0 || _types[_typesCount - 1] != 
SEG_CLOSE) {
                                needRoom (1, 0, true);
                                _types [_typesCount++] = SEG_CLOSE;
@@ -311,6 +322,7 @@
 
                public void curveTo(float x1, float y1, float x2, float y2, 
float x3, float y3) 
                {
+                       ClearCache ();
                        needRoom (1, 6, true);
                        _types [_typesCount++] = SEG_CUBICTO;
                        _coords [_coordsCount++] = x1;
@@ -403,6 +415,7 @@
 
                public void lineTo(float x, float y) 
                {
+                       ClearCache ();
                        needRoom (1, 2, true);
                        _types [_typesCount++] = SEG_LINETO;
                        _coords [_coordsCount++] = x;
@@ -411,6 +424,7 @@
 
                public void moveTo(float x, float y) 
                {
+                       ClearCache ();
                        if (_typesCount > 0 && _types [_typesCount - 1] == 
SEG_MOVETO) {
                                _coords [_coordsCount - 2] = x;
                                _coords [_coordsCount - 1] = y;
@@ -425,6 +439,7 @@
 
                public void quadTo(float x1, float y1, float x2, float y2) 
                {
+                       ClearCache ();
                        needRoom (1, 4, true);
                        _types [_typesCount++] = SEG_QUADTO;
                        _coords [_coordsCount++] = x1;
@@ -435,6 +450,7 @@
 
                public void reset() 
                {
+                       ClearCache ();
                        _typesCount = 0;
                        _coordsCount = 0;
                }
@@ -449,6 +465,7 @@
 
                public void transform(AffineTransform at) 
                {
+                       ClearCache ();
                        at.transform (_coords, 0, _coords, 0, _coordsCount/2);
                }
 
@@ -489,24 +506,28 @@
 
                public void SetMarkers()
                {
+                       ClearCache ();
                        if (TypesCount > 0)
                                Types [ TypesCount - 1] |= SEG_MARKER;
                }
 
                public void ClearMarkers()
                {
+                       ClearCache ();
                        for (int i = 0; i < TypesCount; i++)
                                Types [i] &= ~SEG_MARKER;
                }
 
                public void StartFigure ()
                {
+                       ClearCache ();
                        if (TypesCount > 0)
                                Types [TypesCount - 1] |= 
ExtendedGeneralPath.SEG_START;
                }
 
                private void Reset (int initialTypes, int initialCoords)
                {
+                       ClearCache ();
                        _types = new sbyte [initialTypes];
                        _coords = new float [initialCoords * 2];
                        _typesCount = 0;
@@ -520,6 +541,7 @@
 
                internal void Reverse ()
                {
+                       ClearCache ();
                        // revert coordinates
                        for (int i=0, max = CoordsCount / 2; i < max;) {
                                int ix = i++;

Modified: trunk/mcs/class/System.Drawing/System.Drawing.Drawing2D/PathData.cs
===================================================================
--- trunk/mcs/class/System.Drawing/System.Drawing.Drawing2D/PathData.cs 
2005-09-07 09:39:26 UTC (rev 49620)
+++ trunk/mcs/class/System.Drawing/System.Drawing.Drawing2D/PathData.cs 
2005-09-07 09:47:27 UTC (rev 49621)
@@ -46,13 +46,21 @@
                public PathData () { }
 
                public PointF[] Points {
-                       get { return points; } 
+                       get { return (PointF[]) points.Clone (); } 
                        set { points = value; }
                }
 
                public byte[] Types {
-                       get { return types; }
+                       get { return (byte[]) types.Clone (); }
                        set { types = value; }
                }
+
+               public PointF[] InternalPoints {
+                       get { return points; } 
+               }
+
+               public byte[] InternalTypes {
+                       get { return types; }
+               }
        }
 }

Modified: trunk/mcs/class/System.Drawing/Test/DrawingTest/Test/ChangeLog
===================================================================
--- trunk/mcs/class/System.Drawing/Test/DrawingTest/Test/ChangeLog      
2005-09-07 09:39:26 UTC (rev 49620)
+++ trunk/mcs/class/System.Drawing/Test/DrawingTest/Test/ChangeLog      
2005-09-07 09:47:27 UTC (rev 49621)
@@ -1,3 +1,6 @@
+2005-09-07 Boris Kirzner <[EMAIL PROTECTED]>
+       * GraphicsPath.cs: Added another PathData test..
+
 2005-09-06 Boris Kirzner <[EMAIL PROTECTED]>
        * GraphicsPathIterator.cs: added.
        * GraphicsPath.cs: Changed tests. Added NotWorking for the tests that

Modified: trunk/mcs/class/System.Drawing/Test/DrawingTest/Test/GraphicsPath.cs
===================================================================
--- trunk/mcs/class/System.Drawing/Test/DrawingTest/Test/GraphicsPath.cs        
2005-09-07 09:39:26 UTC (rev 49620)
+++ trunk/mcs/class/System.Drawing/Test/DrawingTest/Test/GraphicsPath.cs        
2005-09-07 09:47:27 UTC (rev 49621)
@@ -4242,6 +4242,26 @@
                }
 
                [Test]
+               public void PathData2 ()
+               {
+                       path = new GraphicsPath ();
+                       path.AddLine (new Point (100, 100), new Point (400, 
100));
+                       path.AddLine (new Point (400, 200), new Point (10, 
100));
+
+                       byte [] types = path.PathTypes;
+                       PointF [] points = path.PathPoints;
+
+                       types [1] = 88;
+                       points [1] = new PointF (-88, -88);
+
+                       Assert.AreEqual ( 88, types [1]);
+                       DrawingTest.AssertAlmostEqual ( new PointF (-88,-88), 
points [1]);
+
+                       Assert.AreEqual ( 1, path.PathData.Types [1]);
+                       DrawingTest.AssertAlmostEqual ( new PointF (400,100), 
path.PathData.Points [1]);
+               }
+
+               [Test]
                public void Reset()
                {
                        path = new GraphicsPath ();

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to