OSDN Git Service

ファクトリーの類は共有にしないといけない
[fooeditengine/FooEditEngine.git] / Core / Direct2D / D2DRenderCommon.cs
index 505fcd7..cc76282 100644 (file)
@@ -94,8 +94,6 @@ namespace FooEditEngine
     {
         ResourceManager<HilightType, D2D.StrokeStyle> cache = new ResourceManager<HilightType, D2D.StrokeStyle>();
 
-        public D2D.Factory Factory;
-
         public D2D.StrokeStyle Get(D2D.RenderTarget render,HilightType type)
         {
             return this.Get(render.Factory, type);
@@ -104,13 +102,11 @@ namespace FooEditEngine
         [Obsolete]
         public D2D.StrokeStyle Get(HilightType type)
         {
-            return this.Get(this.Factory, type);
+            return this.Get(D2DRenderShared.D2DFactory, type);
         }
 
         public D2D.StrokeStyle Get(D2D.Factory factory,HilightType type)
         {
-            if(this.Factory == null || this.Factory.IsDisposed)
-                throw new InvalidOperationException();
             D2D.StrokeStyle stroke;
             if (this.cache.TryGetValue(type, out stroke))
                 return stroke;
@@ -143,7 +139,7 @@ namespace FooEditEngine
                     prop.DashStyle = D2D.DashStyle.Dot;
                     break;
             }
-            stroke = new D2D.StrokeStyle(this.Factory, prop);
+            stroke = new D2D.StrokeStyle(D2DRenderShared.D2DFactory, prop);
             this.cache.Add(type, stroke);
             return stroke;
         }
@@ -153,12 +149,56 @@ namespace FooEditEngine
         }
     }
 
+    class D2DRenderShared
+    {
+        static DW.Factory _DWFactory;
+        static public DW.Factory DWFactory
+        {
+            get
+            {
+                if(_DWFactory == null)
+                {
+                    _DWFactory = new DW.Factory(DW.FactoryType.Shared);
+                }
+                return _DWFactory;
+            }
+        }
+#if METRO || WINDOWS_UWP
+        static D2D.Factory1 _D2DFactory;
+
+        static public D2D.Factory1 D2DFactory
+        {
+            get
+            {
+                if (_D2DFactory == null)
+                {
+                    _D2DFactory = new D2D.Factory1(D2D.FactoryType.SingleThreaded);
+                }
+                return _D2DFactory;
+            }
+        }
+#else
+        static D2D.Factory _D2DFactory;
+        static public D2D.Factory D2DFactory
+        {
+            get
+            {
+                if (_D2DFactory == null)
+                {
+                    _D2DFactory = new D2D.Factory(D2D.FactoryType.SingleThreaded);
+                }
+                return _D2DFactory;
+            }
+        }
+#endif
+
+    }
+
     class D2DRenderCommon : IDisposable
     {
         InlineManager HiddenChars;
         TextAntialiasMode _TextAntialiasMode;
         Color4 _ControlChar,_Forground,_URL,_Hilight;
-        DW.Factory DWFactory;
         DW.TextFormat format;
         protected CustomTextRenderer textRender;
         protected D2D.Bitmap cachedBitMap;
@@ -168,20 +208,6 @@ namespace FooEditEngine
         protected bool hasCache = false;
         protected Size renderSize;
 
-#if METRO || WINDOWS_UWP
-        protected D2D.Factory1 D2DFactory
-        {
-            get;
-            private set;
-        }
-#else
-        protected D2D.Factory D2DFactory
-        {
-            get;
-            private set;
-        }
-#endif
-
         protected ColorBrushCollection Brushes
         {
             get;
@@ -208,15 +234,8 @@ namespace FooEditEngine
 
         public D2DRenderCommon()
         {
-            this.DWFactory = new DW.Factory(DW.FactoryType.Shared);
-#if METRO || WINDOWS_UWP
-            this.D2DFactory = new D2D.Factory1(D2D.FactoryType.SingleThreaded);
-#else
-            this.D2DFactory = new D2D.Factory(D2D.FactoryType.SingleThreaded);
-#endif
             this.Brushes = new ColorBrushCollection();
             this.Strokes = new StrokeCollection();
-            this.Strokes.Factory = this.D2DFactory;
             this.ChangedRenderResource += (s, e) => { };
             this.ChangedRightToLeft += (s, e) => { };
             this.renderSize = new Size();
@@ -236,12 +255,12 @@ namespace FooEditEngine
             float dpix, dpiy;
             this.GetDpi(out dpix, out dpiy);
 
-            this.format = new DW.TextFormat(this.DWFactory, fontName, fontWeigth, fontStyle, fontSize);
+            this.format = new DW.TextFormat(D2DRenderShared.DWFactory, fontName, fontWeigth, fontStyle, fontSize);
             this.format.WordWrapping = DW.WordWrapping.NoWrap;
             this.format.ReadingDirection = GetDWRightDirect(_RightToLeft);
 
             if (this.HiddenChars == null)
-                this.HiddenChars = new InlineManager(this.DWFactory, this.format, this.ControlChar, this.Brushes);
+                this.HiddenChars = new InlineManager(D2DRenderShared.DWFactory, this.format, this.ControlChar, this.Brushes);
             else
                 this.HiddenChars.Format = this.format;
 
@@ -249,12 +268,12 @@ namespace FooEditEngine
 
             this.hasCache = false;
 
-            MyTextLayout layout = new MyTextLayout(this.DWFactory, "0", this.format, float.MaxValue, float.MaxValue, dpix, false);
+            MyTextLayout layout = new MyTextLayout(D2DRenderShared.DWFactory, "0", this.format, float.MaxValue, float.MaxValue, dpix, false);
             layout.RightToLeft = false;
             this.emSize = new Size(layout.Width, layout.Height);
             layout.Dispose();
 
-            layout = new MyTextLayout(this.DWFactory, "+", this.format, float.MaxValue, float.MaxValue, dpix, false);
+            layout = new MyTextLayout(D2DRenderShared.DWFactory, "+", this.format, float.MaxValue, float.MaxValue, dpix, false);
             layout.RightToLeft = false;
 #if METRO
             this.FoldingWidth = Math.Max(D2DRenderCommon.MiniumeWidth, layout.Width);
@@ -552,7 +571,7 @@ namespace FooEditEngine
                 if (value == 0)
                     return;
                 this.tabLength = value;
-                DW.TextLayout layout = new DW.TextLayout(this.DWFactory, "0", this.format, float.MaxValue, float.MaxValue);
+                DW.TextLayout layout = new DW.TextLayout(D2DRenderShared.DWFactory, "0", this.format, float.MaxValue, float.MaxValue);
                 float width = (float)(layout.Metrics.Width * value);
                 this.HiddenChars.TabWidth = width;
                 this.format.IncrementalTabStop = width;
@@ -624,7 +643,7 @@ namespace FooEditEngine
                     throw new ArgumentOutOfRangeException();
             }
             this.GetDpi(out dpix, out dpiy);
-            MyTextLayout layout = new MyTextLayout(this.DWFactory, str, this.format, (float)layoutRect.Width, (float)layoutRect.Height,dpix,false);
+            MyTextLayout layout = new MyTextLayout(D2DRenderShared.DWFactory, str, this.format, (float)layoutRect.Width, (float)layoutRect.Height,dpix,false);
             layout.StringAlignment = align;
             layout.Draw(this.render, (float)x, (float)y, brush);
             layout.Dispose();
@@ -728,7 +747,7 @@ namespace FooEditEngine
         public void DrawLine(Point from, Point to)
         {
             D2D.Brush brush = this.Brushes.Get(this.Foreground);
-            D2D.StrokeStyle stroke = this.Strokes.Get(this.D2DFactory,HilightType.Sold);
+            D2D.StrokeStyle stroke = this.Strokes.Get(D2DRenderShared.D2DFactory,HilightType.Sold);
             this.render.DrawLine(from, to, brush, 1.0f, stroke);
         }
 
@@ -754,13 +773,13 @@ namespace FooEditEngine
             D2D.SolidColorBrush brush = this.Brushes.Get(color);
 
             if (type == HilightType.Squiggle)
-                effecter = new D2DSquilleLineMarker(this.render, brush, this.Strokes.Get(this.D2DFactory, HilightType.Squiggle), thickness);
+                effecter = new D2DSquilleLineMarker(this.render, brush, this.Strokes.Get(D2DRenderShared.D2DFactory, HilightType.Squiggle), thickness);
             else if (type == HilightType.Select)
                 effecter = new HilightMarker(this.render, brush);
             else if (type == HilightType.None)
                 effecter = null;
             else
-                effecter = new LineMarker(this.render, brush, this.Strokes.Get(this.D2DFactory,type), thickness);
+                effecter = new LineMarker(this.render, brush, this.Strokes.Get(D2DRenderShared.D2DFactory,type), thickness);
 
             if (effecter != null)
             {
@@ -781,7 +800,7 @@ namespace FooEditEngine
             this.GetDpi(out dpix,out dpiy);
 
             bool hasNewLine = str.Length > 0 && str[str.Length - 1] == Document.NewLine;
-            MyTextLayout newLayout = new MyTextLayout(this.DWFactory,
+            MyTextLayout newLayout = new MyTextLayout(D2DRenderShared.DWFactory,
                 str,
                 this.format,
                 this.TextArea.Width,
@@ -853,7 +872,7 @@ namespace FooEditEngine
 
             foreach (string str in doc.GetLines(startIndex, endIndex))
             {
-                DW.TextLayout layout = new DW.TextLayout(this.DWFactory, str, this.format, (float)wrapwidth, float.MaxValue);
+                DW.TextLayout layout = new DW.TextLayout(D2DRenderShared.DWFactory, str, this.format, (float)wrapwidth, float.MaxValue);
 
                 int i = startIndex;
                 foreach (DW.LineMetrics metrics in layout.GetLineMetrics())
@@ -891,10 +910,6 @@ namespace FooEditEngine
                 this.HiddenChars.Clear();
                 if (this.format != null)
                     this.format.Dispose();
-                if (this.DWFactory != null)
-                    this.DWFactory.Dispose();
-                if (this.D2DFactory != null)
-                    this.D2DFactory.Dispose();
             }
             this._Disposed = true;
         }