OSDN Git Service

レンダーターゲットをクラス変数として持たせておく意味はない
[fooeditengine/FooEditEngine.git] / WPF / FooEditEngine / Direct2D / D2DRender.cs
index cfa3f16..e12f559 100644 (file)
@@ -38,14 +38,13 @@ namespace FooEditEngine.WPF
         D3D11.Device device;
         D3D9.Device device9;
         D3D9.Surface surface9;
-        new D2D.RenderTarget render;
         double fontSize;
         FontFamily fontFamily;
         FontWeight fontWeigth;
         FontStyle fontStyle;
         D3DImage imageSource;
 
-        public D2DRender(FooTextBox textbox, double width, double height,Image image)
+        public D2DRender(FooTextBox textbox, double width, double height, Image image)
         {
             this.fontFamily = textbox.FontFamily;
             this.fontSize = textbox.FontSize;
@@ -65,11 +64,12 @@ namespace FooEditEngine.WPF
             this.LineMarker = ToColor4(textbox.LineMarker);
             this.UpdateArea = ToColor4(textbox.UpdateArea);
             this.LineNumber = ToColor4(textbox.LineNumber);
+            this.HilightForeground = ToColor4(textbox.HilightForeground);
             this.store = textbox.TextStore;
 
             this.CreateDevice();
 
-            this.ConstructDeviceResource(width, height);
+            this.ConstructRenderAndResource(width, height);
             this.InitTextFormat(this.fontFamily.Source, (float)this.fontSize, this.GetDWFontWeigth(this.fontWeigth), this.GetDWFontStyle(this.fontStyle));
 
             this.imageSource = new D3DImage();
@@ -111,7 +111,7 @@ namespace FooEditEngine.WPF
             set
             {
                 this.fontWeigth = value;
-                this.InitTextFormat(this.fontFamily.Source, (float)this.fontSize, this.GetDWFontWeigth(value),this.GetDWFontStyle(this.fontStyle));
+                this.InitTextFormat(this.fontFamily.Source, (float)this.fontSize, this.GetDWFontWeigth(value), this.GetDWFontStyle(this.fontStyle));
             }
         }
 
@@ -137,7 +137,7 @@ namespace FooEditEngine.WPF
         {
             if (Math.Floor(width) != Math.Floor(this.imageSource.Width) || Math.Floor(height) != Math.Floor(this.imageSource.Height))
             {
-                this.ReConstructDeviceResource(width, height);
+                this.ReConstructRenderAndResource(width, height);
                 this.imageSource.Lock();
                 this.imageSource.SetBackBuffer(D3DResourceType.IDirect3DSurface9, this.surface9.NativePointer);
                 this.imageSource.Unlock();
@@ -146,7 +146,13 @@ namespace FooEditEngine.WPF
             return false;
         }
 
-        public void DrawContent(EditView view,bool IsEnabled,Rectangle updateRect)
+        public void ReConstructRenderAndResource(double width, double height)
+        {
+            this.DestructRenderAndResource();
+            this.ConstructRenderAndResource(width, height);
+        }
+
+        public void DrawContent(EditView view, bool IsEnabled, Rectangle updateRect)
         {
             if (this.imageSource.IsFrontBufferAvailable)
             {
@@ -161,27 +167,28 @@ namespace FooEditEngine.WPF
                 base.EndDraw();
                 this.device.ImmediateContext.Flush();
 
-                this.imageSource.AddDirtyRect(new Int32Rect(0, 0, (int)this.imageSource.Width, (int)this.imageSource.Height));
+                this.imageSource.AddDirtyRect(new Int32Rect(0, 0, (int)this.imageSource.PixelWidth, (int)this.imageSource.PixelHeight));
                 this.imageSource.Unlock();
             }
         }
 
-        public void DrawOneLine(LineToIndexTable lti, int row, double x, double y, IEnumerable<Selection> SelectRanges)
+        public void DrawOneLine(Document doc, LineToIndexTable lti, int row, double x, double y)
         {
             PreDrawOneLineHandler PreDrawOneLine = null;
 
             if (InputMethod.Current.ImeState == InputMethodState.On)
                 PreDrawOneLine = this.DrawImeConversionLine;
 
-            base.DrawOneLine(lti,
+            base.DrawOneLine(doc,
+                lti,
                 row,
                 x,
                 y,
-                SelectRanges,
-                PreDrawOneLine);
+                PreDrawOneLine
+                );
         }
 
-        private void DrawImeConversionLine(MyTextLayout layout,LineToIndexTable lti,int row,double x,double y)
+        private void DrawImeConversionLine(MyTextLayout layout, LineToIndexTable lti, int row, double x, double y)
         {
             using (Unlocker locker = this.store.LockDocument(false))
             {
@@ -238,7 +245,7 @@ namespace FooEditEngine.WPF
         private Color4? GetColor4(TF_DA_COLOR cr)
         {
             COLORREF colorref;
-            switch(cr.type)
+            switch (cr.type)
             {
                 case TF_DA_COLORTYPE.TF_CT_SYSCOLOR:
                     colorref = new COLORREF(NativeMethods.GetSysColor((int)cr.indexOrColorRef));
@@ -252,12 +259,6 @@ namespace FooEditEngine.WPF
             return new Color4(colorref.R / 255.0f, colorref.G / 255.0f, colorref.B / 255.0f, 1);
         }
 
-        public override void Dispose()
-        {
-            base.Dispose();
-            this.DestructDevice();
-        }
-
         DW.FontStyle GetDWFontStyle(FontStyle style)
         {
             return (DW.FontStyle)Enum.Parse(typeof(DW.FontStyle), style.ToString());
@@ -270,7 +271,7 @@ namespace FooEditEngine.WPF
 
         public override void DrawCachedBitmap(Rectangle rect)
         {
-            if (this.render == null || this.render.IsDisposed)
+            if (this.render == null || this.render.IsDisposed || this.cachedBitMap == null || this.cachedBitMap.IsDisposed)
                 return;
             render.DrawBitmap(this.cachedBitMap, rect, 1.0f, D2D.BitmapInterpolationMode.Linear, rect);
         }
@@ -364,16 +365,18 @@ namespace FooEditEngine.WPF
             }
         }
 
-        protected override void ReCreateTarget()
+        public void ConstructRenderAndResource(double width, double height)
         {
-            System.Diagnostics.Debug.WriteLine("ReCreatedDevice");
-            this.DestructDevice();
-            this.CreateDevice();
-            this.ReConstructDeviceResource();
-        }
+            float dpiX, dpiY;
+            this.GetDpi(out dpiX, out dpiY);
+            D2D.RenderTargetProperties prop = new D2D.RenderTargetProperties(
+                D2D.RenderTargetType.Default,
+                new D2D.PixelFormat(DXGI.Format.B8G8R8A8_UNorm, D2D.AlphaMode.Premultiplied),
+                dpiX,
+                dpiY,
+                D2D.RenderTargetUsage.None,
+                D2D.FeatureLevel.Level_DEFAULT);
 
-        protected override D2D.RenderTarget ConstructRender(D2D.Factory factory, D2D.RenderTargetProperties prop, double width, double height)
-        {
             D3D11.Texture2DDescription desc = new D3D11.Texture2DDescription();
             desc.Width = (int)width;
             desc.Height = (int)height;
@@ -404,19 +407,37 @@ namespace FooEditEngine.WPF
             resource.Dispose();
             texture.Dispose();
 
-            this.render = new D2D.RenderTarget(factory, this.surface, prop);
-            return this.render;
-        }
+            this.render = new D2D.RenderTarget(D2DRenderShared.D2DFactory, this.surface, prop);
 
-        protected override void ConstrctedResource()
-        {
+            D2D.BitmapProperties bmpProp = new D2D.BitmapProperties();
+            bmpProp.DpiX = dpiX;
+            bmpProp.DpiY = dpiY;
+            bmpProp.PixelFormat = new D2D.PixelFormat(DXGI.Format.B8G8R8A8_UNorm, D2D.AlphaMode.Premultiplied);
+            this.cachedBitMap = new D2D.Bitmap(this.render, new SharpDX.Size2((int)width, (int)height), bmpProp);
+            this.hasCache = false;
+
+            this.textRender = new CustomTextRenderer(this.Brushes, this.Strokes, this.Foreground);
+
+            this.renderSize = new Size(width, height);
         }
 
-        public double GetScale()
+        public void DestructRenderAndResource()
         {
-            float dpi;
-            this.GetDpi(out dpi, out dpi);
-            return dpi / 96.0;
+            this.hasCache = false;
+            if (this.cachedBitMap != null)
+                this.cachedBitMap.Dispose();
+            this.Brushes.Clear();
+            this.Strokes.Clear();
+            if (this.textRender != null)
+                this.textRender.Dispose();
+            if (this.texture != null)
+                this.texture.Dispose();
+            if (this.surface != null)
+                this.surface.Dispose();
+            if (this.surface9 != null)
+                this.surface9.Dispose();
+            if (this.render != null)
+                this.render.Dispose();
         }
 
         public override void GetDpi(out float dpix, out float dpiy)
@@ -428,16 +449,12 @@ namespace FooEditEngine.WPF
             dpiy = (int)dpiYProperty.GetValue(null, null);
         }
 
-        protected override void DestructRender()
+        protected override void Dispose(bool dispose)
         {
-            if (this.texture != null)
-                this.texture.Dispose();
-            if (this.surface != null)
-                this.surface.Dispose();
-            if (this.surface9 != null)
-                this.surface9.Dispose();
-            if (this.render != null)
-                this.render.Dispose();
+            base.Dispose(dispose);
+            this.DestructRenderAndResource();
+            this.DestructDevice();
         }
+
     }
 }