OSDN Git Service

リファクタリングを行った
authorkonekoneko <jbh03215@hotmail.co.jp>
Sun, 28 Oct 2012 17:02:16 +0000 (02:02 +0900)
committerkonekoneko <jbh03215@hotmail.co.jp>
Sun, 28 Oct 2012 17:02:16 +0000 (02:02 +0900)
14 files changed:
D2DBench/D2DBench.csproj
D2DBench/Direct2D.cs [new file with mode: 0644]
D2DBench/Form1.cs
D2DBench/GDI.cs
D2DBench/IBench.cs
D2DBench/IRender.cs [new file with mode: 0644]
D2DBench/Method1.cs
D2DBench/Method2.cs
D2DBench/Method3.cs
D2DBench/Method4.cs
D2DBench/Method5.cs [deleted file]
D2DBench/Method6.cs [deleted file]
D2DBench/Method7.cs [deleted file]
D2DBench/Method8.cs [deleted file]

index 714fb43..7dda90a 100644 (file)
@@ -58,6 +58,7 @@
   </ItemGroup>\r
   <ItemGroup>\r
     <Compile Include="ColorTableD2D.cs" />\r
+    <Compile Include="Direct2D.cs" />\r
     <Compile Include="Form1.cs">\r
       <SubType>Form</SubType>\r
     </Compile>\r
     <Compile Include="GDI.cs" />\r
     <Compile Include="HWInfo.cs" />\r
     <Compile Include="IBench.cs" />\r
+    <Compile Include="IRender.cs" />\r
     <Compile Include="Method1.cs" />\r
     <Compile Include="Method2.cs" />\r
     <Compile Include="Method3.cs" />\r
     <Compile Include="Method4.cs" />\r
-    <Compile Include="Method5.cs" />\r
-    <Compile Include="Method6.cs" />\r
-    <Compile Include="Method7.cs" />\r
-    <Compile Include="Method8.cs" />\r
     <Compile Include="Program.cs" />\r
     <Compile Include="Properties\AssemblyInfo.cs" />\r
     <Compile Include="RenderAdapter.cs" />\r
diff --git a/D2DBench/Direct2D.cs b/D2DBench/Direct2D.cs
new file mode 100644 (file)
index 0000000..12e3eaf
--- /dev/null
@@ -0,0 +1,144 @@
+using System;
+using System.Drawing;
+using System.Diagnostics;
+using System.Windows.Forms;
+using SharpDX;
+using SharpDX.Direct2D1;
+using SharpDX.DirectWrite;
+
+using D2D = SharpDX.Direct2D1;
+using DW = SharpDX.DirectWrite;
+using Drawing = System.Drawing;
+
+namespace D2DBench
+{
+    class Direct2DBitmap : IDisposable,IBitmap
+    {
+        internal D2D.Bitmap bmp;
+        public Direct2DBitmap(D2D.Bitmap bmp)
+        {
+            this.bmp = bmp;
+        }
+
+        public void Dispose()
+        {
+            this.bmp.Dispose();
+        }
+
+        public int Width
+        {
+            get { return this.bmp.PixelSize.Width; }
+        }
+
+        public int Height
+        {
+            get { return this.bmp.PixelSize.Height; }
+        }
+    }
+
+    class Direct2D : IDisposable, IRender
+    {
+        D2D.Factory Factory2D;
+        DW.Factory FactoryDWrite;
+        WindowRenderTarget render;
+        TextFormat format;
+        TextLayout layout;
+        ColorTableD2D colors;
+        const string showStr = "D2DBenchMark";
+
+        public Direct2D(Control ctrl, Drawing.Size ClientSize, bool antialias)
+        {
+            Factory2D = new SharpDX.Direct2D1.Factory();
+            FactoryDWrite = new SharpDX.DirectWrite.Factory();
+            HwndRenderTargetProperties hwndprops = new HwndRenderTargetProperties { Hwnd = ctrl.Handle, PixelSize = ClientSize, PresentOptions = PresentOptions.Immediately };
+            render = new WindowRenderTarget(Factory2D, new RenderTargetProperties(), hwndprops);
+            format = new TextFormat(FactoryDWrite, ctrl.Font.Name, ctrl.Font.Size * render.DotsPerInch.Width / 72.0f);
+            colors = new ColorTableD2D(render);
+            layout = new TextLayout(FactoryDWrite, showStr, format, ClientSize.Width, ClientSize.Height);
+            render.AntialiasMode = antialias ? AntialiasMode.PerPrimitive : AntialiasMode.Aliased;
+        }
+
+        public IBitmap Load(Drawing.Bitmap srcBitmap)
+        {
+            var bitmapData = srcBitmap.LockBits(
+                new Drawing.Rectangle(0, 0, srcBitmap.Width, srcBitmap.Height),
+                System.Drawing.Imaging.ImageLockMode.ReadOnly,
+                System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
+
+            var stream = new DataStream(bitmapData.Scan0, bitmapData.Stride * bitmapData.Height, true, false);
+
+            var properties = new BitmapProperties()
+            {
+                PixelFormat = new D2D.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied)
+            };
+            var bitmap = new D2D.Bitmap(this.render, srcBitmap.Size, stream, bitmapData.Stride, properties);
+
+            srcBitmap.UnlockBits(bitmapData);
+
+            return new Direct2DBitmap(bitmap);
+        }
+
+        public void UnLoad(IBitmap bmp)
+        {
+            Direct2DBitmap dbmp = (Direct2DBitmap)bmp;
+            dbmp.Dispose();
+        }
+
+        public void BeginDraw()
+        {
+            render.BeginDraw();
+        }
+
+        public int ColorCount { get { return this.colors.Count; } }
+
+        public void DrawBitmap(IBitmap bmp,int left,int top,int right,int bottom)
+        {
+            Direct2DBitmap localBmp = (Direct2DBitmap)bmp;
+            SharpDX.RectangleF rect = new SharpDX.RectangleF();
+            rect.Left = left;
+            rect.Top = top;
+            rect.Right = right;
+            rect.Bottom = bottom;
+            render.DrawBitmap(localBmp.bmp, rect, 1.0f, BitmapInterpolationMode.NearestNeighbor);
+        }
+
+        public void DrawString(int x, int y,int colorno)
+        {
+            render.DrawTextLayout(new Point(x,y), layout, colors[colorno]);
+        }
+
+        public void DrawLine(int x, int y,int tox,int toy, int colorno)
+        {
+            var fore = colors[colorno];
+            Point from = new Point(x,y);
+            Point to = new Point(tox,toy);
+            render.DrawLine(from, to, fore, 1.0f);
+        }
+
+        public void FillRectangle(int left,int top,int right,int bottom,int colorno)
+        {
+            var fore = colors[colorno];
+            SharpDX.RectangleF rect = new SharpDX.RectangleF();
+            rect.Left = left;
+            rect.Top = top;
+            rect.Right = right;
+            rect.Bottom = bottom;
+            render.FillRectangle(rect, fore);
+        }
+
+        public void EndDraw()
+        {
+            render.EndDraw();
+        }
+
+        public void Dispose()
+        {
+            colors.Clear();
+            layout.Dispose();
+            format.Dispose();
+            render.Dispose();
+            Factory2D.Dispose();
+            FactoryDWrite.Dispose();
+        }
+    }
+}
index 9a76dd5..6292d78 100644 (file)
@@ -38,24 +38,17 @@ namespace D2DBench
 \r
             List<IBench> methods = new List<IBench>();\r
 \r
-            if (this.comboBox3.SelectedIndex == 0)\r
-            {\r
-                methods.Add(new Method5());\r
-                methods.Add(new Method6());\r
-                methods.Add(new Method7());\r
-                methods.Add(new Method8());\r
-            }\r
-            else\r
-            {\r
-                methods.Add(new Method1());\r
-                methods.Add(new Method2());\r
-                methods.Add(new Method3());\r
-                methods.Add(new Method4());\r
-            }\r
+            methods.Add(new Method1());\r
+            methods.Add(new Method2());\r
+            methods.Add(new Method3());\r
+            methods.Add(new Method4());\r
 \r
             foreach (IBench method in methods)\r
             {\r
-                method.Init(benchFrom,this.comboBox3.SelectedIndex == 2);\r
+                RenderMethod renderMethod = RenderMethod.Direct2D;\r
+                if (this.comboBox3.SelectedIndex == 0)\r
+                    renderMethod = RenderMethod.GDI;\r
+                method.Init(benchFrom, this.comboBox3.SelectedIndex == 2, renderMethod);\r
                 method.loopCount = loopCountAtOneLoop;\r
 \r
                 Stopwatch watch = new Stopwatch();\r
index e95d5b7..7236177 100644 (file)
@@ -6,19 +6,33 @@ using System.Security;
 \r
 namespace D2DBench\r
 {\r
-    class GDIBitmap\r
+    class GDIBitmap : IBitmap\r
     {\r
         public IntPtr hbmp;\r
         public Graphics bmpGrp;\r
         public IntPtr bmpDC;\r
-        public GDIBitmap(IntPtr hbmp, Graphics bmpGrp, IntPtr bmpDC)\r
+        public GDIBitmap(IntPtr hbmp, Graphics bmpGrp, IntPtr bmpDC,int width,int height)\r
         {\r
             this.hbmp = hbmp;\r
             this.bmpGrp = bmpGrp;\r
             this.bmpDC = bmpDC;\r
+            this.Width = width;\r
+            this.Height = height;\r
+        }\r
+\r
+        public int Width\r
+        {\r
+            get;\r
+            private set;\r
+        }\r
+\r
+        public int Height\r
+        {\r
+            get;\r
+            private set;\r
         }\r
     }\r
-    class GDI : IDisposable\r
+    class GDI : IDisposable,IRender\r
     {\r
         [StructLayout(LayoutKind.Sequential)]\r
         struct SIZE\r
@@ -223,10 +237,13 @@ namespace D2DBench
         Graphics frontgrp;\r
         Rectangle ClientRect;\r
         IntPtr frontdc,backdc,backbmp;\r
+        ColorTableGDI colors = new ColorTableGDI();\r
+        Font font;\r
 \r
         public GDI(Control ctrl)\r
         {\r
             ClientRect = ctrl.ClientRectangle;\r
+            this.font = ctrl.Font;\r
             this.frontgrp = ctrl.CreateGraphics();\r
             this.frontdc = this.frontgrp.GetHdc();\r
             this.backbmp = CreateCompatibleBitmap(this.frontdc, ClientRect.Width, ClientRect.Height);\r
@@ -251,62 +268,67 @@ namespace D2DBench
             DeleteDC(this.backdc);\r
         }\r
 \r
-        public GDIBitmap LoadImage(Bitmap src)\r
+        public int ColorCount { get { return this.colors.Count; } }\r
+        \r
+        public IBitmap Load(Bitmap src)\r
         {\r
             IntPtr hbmp = src.GetHbitmap();\r
             Graphics bmpGrp = Graphics.FromImage(src);\r
             IntPtr hsrc = bmpGrp.GetHdc();\r
-            return new GDIBitmap(hbmp, bmpGrp, hsrc);\r
+            return new GDIBitmap(hbmp, bmpGrp, hsrc,src.Width,src.Height);\r
         }\r
 \r
-        public void UnloadImage(GDIBitmap bmp)\r
+        public void UnLoad(IBitmap bmp)\r
         {\r
-            bmp.bmpGrp.ReleaseHdc(bmp.bmpDC);\r
-            DeleteObject(bmp.hbmp);\r
+            GDIBitmap gbmp = (GDIBitmap)bmp;\r
+            gbmp.bmpGrp.ReleaseHdc(gbmp.bmpDC);\r
+            DeleteObject(gbmp.hbmp);\r
         }\r
 \r
-        public void DrawImage(Rectangle dst,GDIBitmap gbmp)\r
+        public void DrawBitmap(IBitmap bmp,int left,int top,int right,int bottom)\r
         {\r
+            GDIBitmap gbmp = (GDIBitmap)bmp;\r
             IntPtr hbmpold = SelectObject(gbmp.bmpDC, gbmp.hbmp);\r
 \r
-            BitBlt(backdc, dst.X, dst.Y, dst.Width, dst.Height, gbmp.bmpDC, 0, 0, 0xcc0020);\r
+            BitBlt(backdc, left, top, right, bottom, gbmp.bmpDC, 0, 0, 0xcc0020);\r
 \r
             SelectObject(gbmp.bmpDC, hbmpold);\r
         }\r
         \r
-        public void FillRectangle(Rectangle rectangle,Color color)\r
+        public void FillRectangle(int left, int top, int right, int bottom, int colorno)\r
         {\r
-            int OldBackColor = (int)SetBkColor(backdc, ColorTranslator.ToWin32(color));\r
-            RECT rect = new RECT(rectangle.X, rectangle.Y, rectangle.Right + 1, rectangle.Bottom + 1);\r
+            int OldBackColor = (int)SetBkColor(backdc, ColorTranslator.ToWin32(this.colors[colorno]));\r
+            RECT rect = new RECT(left, top, right + 1, bottom + 1);\r
             ExtTextOut(backdc, rect.left, rect.top, (uint)ETOOptions.ETO_OPAQUE, ref rect, string.Empty, (uint)0, null);\r
             SetTextColor(backdc, OldBackColor);\r
         }\r
 \r
-        public void DrawLine(Point from,Point to, int LineHeight, Color ForeColor)\r
+        public void DrawLine(int x, int y, int tox, int toy, int colorno)\r
         {\r
             PenStyle style = PenStyle.PS_SOLID;\r
-            IntPtr pen = CreatePen(style, LineHeight, (uint)ColorTranslator.ToWin32(ForeColor));\r
+            IntPtr pen = CreatePen(style, 1, (uint)ColorTranslator.ToWin32(this.colors[colorno]));\r
 \r
             IntPtr oldpen = SelectObject(backdc, pen);\r
 \r
-            MoveToEx(backdc, from.X, from.Y, IntPtr.Zero);\r
-            LineTo(backdc, to.X, to.Y);\r
+            MoveToEx(backdc, x, y, IntPtr.Zero);\r
+            LineTo(backdc, tox, toy);\r
             \r
             DeleteObject(pen);\r
             SelectObject(backdc, oldpen);\r
         }\r
 \r
-        public void DrawString(Font font,string str, Color ForeColor,int x,int y)\r
+        const string showStr = "D2DBenchMark";\r
+        public void DrawString(int x, int y, int colorno)\r
         {\r
-            int ForeColorCode = ColorTranslator.ToWin32(ForeColor);\r
+            int ForeColorCode = ColorTranslator.ToWin32(this.colors[colorno]);\r
 \r
-            IntPtr hfont = GetHFont(font);\r
+            IntPtr hfont = GetHFont(this.font);\r
             IntPtr OldFont = SelectObject(backdc, hfont);\r
             int OldForeColor = (int)SetTextColor(backdc, ForeColorCode);\r
 \r
             ETOOptions opt = ETOOptions.ETO_NONE;\r
             RECT rect = new RECT(x, y, x + 100, y + 100);\r
-            ExtTextOut(backdc, x, y, (uint)opt, ref rect, str, (uint)str.Length, null);\r
+            ExtTextOut(backdc, x, y, (uint)opt, ref rect, showStr, (uint)showStr.Length, null);\r
 \r
             DeleteObject(hfont);\r
             SelectObject(backdc, OldFont);\r
index d4fcbe2..9058eac 100644 (file)
@@ -8,7 +8,13 @@ namespace D2DBench
         int fillAreaSize { get; }\r
         string methodName { get; }\r
         int loopCount { get; set; }\r
-        void Init(Control ctrl,bool antialias);\r
+        void Init(Control ctrl,bool antialias,RenderMethod method);\r
         void Exec();\r
     }\r
+\r
+    enum RenderMethod\r
+    {\r
+        Direct2D,\r
+        GDI,\r
+    }\r
 }\r
diff --git a/D2DBench/IRender.cs b/D2DBench/IRender.cs
new file mode 100644 (file)
index 0000000..f66fa6a
--- /dev/null
@@ -0,0 +1,22 @@
+using System;
+namespace D2DBench
+{
+    interface IRender
+    {
+        void BeginDraw();
+        int ColorCount { get; }
+        void Dispose();
+        void DrawBitmap(IBitmap bmp, int left, int top, int right, int bottom);
+        void DrawLine(int x, int y, int tox, int toy, int colorno);
+        void DrawString(int x, int y, int colorno);
+        void EndDraw();
+        void FillRectangle(int left, int top, int right, int bottom, int colorno);
+        IBitmap Load(System.Drawing.Bitmap srcBitmap);
+        void UnLoad(IBitmap bmp);
+    }
+    interface IBitmap
+    {
+        int Width { get; }
+        int Height { get; }
+    }
+}
index 2828037..cf99972 100644 (file)
@@ -18,48 +18,33 @@ namespace D2DBench
         public string methodName { get { return "D2D文字列"; } }\r
         public int loopCount { get; set; }\r
 \r
-        D2D.Factory Factory2D;\r
-        DW.Factory FactoryDWrite;\r
-        WindowRenderTarget render;\r
-        TextFormat format;\r
-        TextLayout layout;\r
-        ColorTableD2D colors;\r
+        IRender d2d;\r
         Drawing.Size ClientSize;\r
         Random rnd;\r
-        const string showStr = "D2DBenchMark";\r
 \r
-        public void Init(Control ctrl, bool antialias)\r
+        public void Init(Control ctrl, bool antialias,RenderMethod method)\r
         {\r
             ClientSize = ctrl.ClientSize;\r
-            Factory2D = new SharpDX.Direct2D1.Factory();\r
-            FactoryDWrite = new SharpDX.DirectWrite.Factory();\r
-            HwndRenderTargetProperties hwndprops = new HwndRenderTargetProperties { Hwnd = ctrl.Handle, PixelSize = ClientSize, PresentOptions = PresentOptions.Immediately };\r
-            render = new WindowRenderTarget(Factory2D, new RenderTargetProperties(), hwndprops);\r
-            format = new TextFormat(FactoryDWrite, ctrl.Font.Name, ctrl.Font.Size * render.DotsPerInch.Width / 72.0f);\r
-            colors = new ColorTableD2D(render);\r
+            if (method == RenderMethod.Direct2D)\r
+                this.d2d = new Direct2D(ctrl, ClientSize, antialias);\r
+            else\r
+                this.d2d = new GDI(ctrl);\r
             rnd = new Random();\r
-            layout = new TextLayout(FactoryDWrite, showStr, format, ClientSize.Width, ClientSize.Height);\r
-            render.AntialiasMode = antialias ? AntialiasMode.PerPrimitive : AntialiasMode.Aliased;\r
         }\r
 \r
         public void Exec()\r
         {\r
-            render.BeginDraw();\r
+            this.d2d.BeginDraw();\r
             for (int i = 0; i < this.loopCount; i++)\r
             {\r
-                render.DrawTextLayout(new Point(rnd.Next(ClientSize.Width), rnd.Next(ClientSize.Height)), layout, colors[rnd.Next(colors.Count - 1)]);\r
+                this.d2d.DrawString(rnd.Next(ClientSize.Width), rnd.Next(ClientSize.Height), rnd.Next(this.d2d.ColorCount - 1));\r
             }\r
-            render.EndDraw();\r
+            this.d2d.EndDraw();\r
         }\r
 \r
         public void Dispose()\r
         {\r
-            colors.Clear();\r
-            layout.Dispose();\r
-            format.Dispose();\r
-            render.Dispose();\r
-            Factory2D.Dispose();\r
-            FactoryDWrite.Dispose();\r
+            this.d2d.Dispose();\r
         }\r
     }\r
 }\r
index 8a6f6e5..5fb19b4 100644 (file)
@@ -18,45 +18,37 @@ namespace D2DBench
         public string methodName { get { return "D2D線"; } }\r
         public int loopCount { get; set; }\r
 \r
-        D2D.Factory Factory2D;\r
-        DW.Factory FactoryDWrite;\r
-        WindowRenderTarget render;\r
-        ColorTableD2D colors;\r
+        IRender d2d;\r
         Drawing.Size ClientSize;\r
         Random rnd;\r
-        const string showStr = "D2DBenchMark";\r
 \r
-        public void Init(Control ctrl, bool antialias)\r
+        public void Init(Control ctrl, bool antialias, RenderMethod method)\r
         {\r
             ClientSize = ctrl.ClientSize;\r
-            Factory2D = new SharpDX.Direct2D1.Factory();\r
-            FactoryDWrite = new SharpDX.DirectWrite.Factory();\r
-            HwndRenderTargetProperties hwndprops = new HwndRenderTargetProperties { Hwnd = ctrl.Handle, PixelSize = ClientSize, PresentOptions = PresentOptions.Immediately };\r
-            render = new WindowRenderTarget(Factory2D, new RenderTargetProperties(), hwndprops);\r
-            colors = new ColorTableD2D(render);\r
+            if (method == RenderMethod.Direct2D)\r
+                this.d2d = new Direct2D(ctrl, ClientSize, antialias);\r
+            else\r
+                this.d2d = new GDI(ctrl);\r
             rnd = new Random();\r
-            render.AntialiasMode = antialias ? AntialiasMode.PerPrimitive : AntialiasMode.Aliased;\r
         }\r
 \r
         public void Exec()\r
         {\r
-            render.BeginDraw();\r
+            this.d2d.BeginDraw();\r
             for (int i = 0; i < this.loopCount; i++)\r
             {\r
-                var fore = colors[rnd.Next(colors.Count - 1)];\r
-                Point from = new Point(rnd.Next(ClientSize.Width), rnd.Next(ClientSize.Height));\r
-                Point to = new Point(rnd.Next(ClientSize.Width), rnd.Next(ClientSize.Height));\r
-                render.DrawLine(from, to, fore, 1.0f);\r
+                this.d2d.DrawLine(rnd.Next(ClientSize.Width),\r
+                    rnd.Next(ClientSize.Height),\r
+                    rnd.Next(ClientSize.Width),\r
+                    rnd.Next(ClientSize.Height),\r
+                    rnd.Next(this.d2d.ColorCount - 1));\r
             }\r
-            render.EndDraw();\r
+            this.d2d.EndDraw();\r
         }\r
 \r
         public void Dispose()\r
         {\r
-            colors.Clear();\r
-            render.Dispose();\r
-            Factory2D.Dispose();\r
-            FactoryDWrite.Dispose();\r
+            this.d2d.Dispose();\r
         }\r
     }\r
 }\r
index 2eb5d0d..17c937e 100644 (file)
@@ -18,49 +18,40 @@ namespace D2DBench
         public string methodName { get { return "D2D塗りつぶし"; } }\r
         public int loopCount { get; set; }\r
 \r
-        D2D.Factory Factory2D;\r
-        DW.Factory FactoryDWrite;\r
-        WindowRenderTarget render;\r
-        ColorTableD2D colors;\r
+\r
+        IRender d2d;\r
         Drawing.Size ClientSize;\r
-        Random rnd;\r
         Drawing.Size bmpSize = D2DBench.Properties.Resources.image.Size;\r
-        const string showStr = "D2DBenchMark";\r
+        Random rnd;\r
 \r
-        public void Init(Control ctrl, bool antialias)\r
+        public void Init(Control ctrl, bool antialias, RenderMethod method)\r
         {\r
             ClientSize = ctrl.ClientSize;\r
-            Factory2D = new SharpDX.Direct2D1.Factory();\r
-            FactoryDWrite = new SharpDX.DirectWrite.Factory();\r
-            HwndRenderTargetProperties hwndprops = new HwndRenderTargetProperties { Hwnd = ctrl.Handle, PixelSize = ClientSize, PresentOptions = PresentOptions.Immediately };\r
-            render = new WindowRenderTarget(Factory2D, new RenderTargetProperties(), hwndprops);\r
-            colors = new ColorTableD2D(render);\r
+            if (method == RenderMethod.Direct2D)\r
+                this.d2d = new Direct2D(ctrl, ClientSize, antialias);\r
+            else\r
+                this.d2d = new GDI(ctrl);\r
             rnd = new Random();\r
-            render.AntialiasMode = antialias ? AntialiasMode.PerPrimitive : AntialiasMode.Aliased;\r
         }\r
 \r
         public void Exec()\r
         {\r
-            render.BeginDraw();\r
+            this.d2d.BeginDraw();\r
             for (int i = 0; i < this.loopCount; i++)\r
             {\r
-                var fore = colors[rnd.Next(colors.Count - 1)];\r
-                SharpDX.RectangleF rect = new SharpDX.RectangleF();\r
-                rect.Left = rnd.Next(ClientSize.Width);\r
-                rect.Top = rnd.Next(ClientSize.Height);\r
-                rect.Right = rect.Left + bmpSize.Width;\r
-                rect.Bottom = rect.Right + bmpSize.Height;\r
-                render.FillRectangle(rect, fore);\r
+                this.d2d.FillRectangle(\r
+                    rnd.Next(ClientSize.Width),\r
+                    rnd.Next(ClientSize.Height),\r
+                    rnd.Next(ClientSize.Width),\r
+                    rnd.Next(ClientSize.Height),\r
+                    rnd.Next(this.d2d.ColorCount - 1));\r
             }\r
-            render.EndDraw();\r
+            this.d2d.EndDraw();\r
         }\r
 \r
         public void Dispose()\r
         {\r
-            colors.Clear();\r
-            render.Dispose();\r
-            Factory2D.Dispose();\r
-            FactoryDWrite.Dispose();\r
+            this.d2d.Dispose();\r
         }\r
     }\r
 }\r
index 9c78354..0e5d7b1 100644 (file)
@@ -17,73 +17,46 @@ namespace D2DBench
 {\r
     class Method4 : IBench\r
     {\r
-        public int fillAreaSize { get { return bmpSize.Width * bmpSize.Height * this.loopCount; } }\r
+        public int fillAreaSize { get { return bmp.Width * bmp.Height * this.loopCount; } }\r
         public string methodName { get { return "D2DBitBlt"; } }\r
         public int loopCount { get; set; }\r
 \r
-        D2D1.Factory Factory2D;\r
-        DW.Factory FactoryDWrite;\r
-        WindowRenderTarget render;\r
         Drawing.Size ClientSize;\r
         Random rnd;\r
-        const string showStr = "D2DBenchMark";\r
-        Drawing.Size bmpSize;\r
-        D2D1.Bitmap bmp;\r
+        IRender d2d;\r
+        IBitmap bmp;\r
 \r
-        public void Init(Control ctrl, bool antialias)\r
+        public void Init(Control ctrl, bool antialias, RenderMethod method)\r
         {\r
             ClientSize = ctrl.ClientSize;\r
-            Factory2D = new SharpDX.Direct2D1.Factory();\r
-            FactoryDWrite = new SharpDX.DirectWrite.Factory();\r
-            HwndRenderTargetProperties hwndprops = new HwndRenderTargetProperties { Hwnd = ctrl.Handle, PixelSize = ClientSize, PresentOptions = PresentOptions.Immediately };\r
-            render = new WindowRenderTarget(Factory2D, new RenderTargetProperties(), hwndprops);\r
-            bmpSize = Resources.image.Size;\r
-            bmp = Load(render, Resources.image);\r
+            if (method == RenderMethod.Direct2D)\r
+                this.d2d = new Direct2D(ctrl, ClientSize, antialias);\r
+            else\r
+                this.d2d = new GDI(ctrl);\r
+            this.bmp = this.d2d.Load(Resources.image);\r
             rnd = new Random();\r
-            render.AntialiasMode = antialias ? AntialiasMode.PerPrimitive : AntialiasMode.Aliased;\r
         }\r
 \r
         public void Dispose()\r
         {\r
-            bmp.Dispose();\r
-            render.Dispose();\r
-            Factory2D.Dispose();\r
-            FactoryDWrite.Dispose();\r
+            this.d2d.UnLoad(this.bmp);\r
+            this.d2d.Dispose();\r
         }\r
 \r
         public void Exec()\r
         {\r
-            render.BeginDraw();\r
+            this.d2d.BeginDraw();\r
             for (int i = 0; i < this.loopCount; i++)\r
             {\r
-                SharpDX.RectangleF rect = new SharpDX.RectangleF();\r
-                rect.Left = rnd.Next(ClientSize.Width);\r
-                rect.Top = rnd.Next(ClientSize.Height);\r
-                rect.Right = rect.Left + bmpSize.Width;\r
-                rect.Bottom = rect.Top + bmpSize.Height;\r
-                render.DrawBitmap(bmp, rect, 1.0f, BitmapInterpolationMode.NearestNeighbor);\r
+                int left = rnd.Next(ClientSize.Width);\r
+                int top = rnd.Next(ClientSize.Height);\r
+                this.d2d.DrawBitmap(bmp,\r
+                    left,\r
+                    top,\r
+                    left + bmp.Width,\r
+                    top + bmp.Height);\r
             }\r
-            render.EndDraw();\r
-        }\r
-\r
-        D2D1.Bitmap Load(WindowRenderTarget render, Drawing.Bitmap srcBitmap)\r
-        {\r
-            var bitmapData = srcBitmap.LockBits(\r
-                new Drawing.Rectangle(0, 0, srcBitmap.Width, srcBitmap.Height),\r
-                System.Drawing.Imaging.ImageLockMode.ReadOnly,\r
-                System.Drawing.Imaging.PixelFormat.Format32bppPArgb);\r
-\r
-            var stream = new DataStream(bitmapData.Scan0, bitmapData.Stride * bitmapData.Height, true, false);\r
-\r
-            var properties = new BitmapProperties()\r
-            {\r
-                PixelFormat = new D2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied)\r
-            };\r
-            var bitmap = new D2D1.Bitmap(render, srcBitmap.Size, stream, bitmapData.Stride, properties);\r
-\r
-            srcBitmap.UnlockBits(bitmapData);\r
-\r
-            return bitmap;\r
+            this.d2d.EndDraw();\r
         }\r
     }\r
 }\r
diff --git a/D2DBench/Method5.cs b/D2DBench/Method5.cs
deleted file mode 100644 (file)
index 61362c2..0000000
+++ /dev/null
@@ -1,47 +0,0 @@
-using System;\r
-using System.Drawing;\r
-using System.Diagnostics;\r
-using System.Windows.Forms;\r
-\r
-namespace D2DBench\r
-{\r
-    class Method5 : IBench\r
-    {\r
-        public int fillAreaSize { get { return 0; } }\r
-        public string methodName { get { return "GDI文字列"; } }\r
-        public int loopCount { get; set; }\r
-\r
-        GDI gdi;\r
-        ColorTableGDI colors;\r
-        Size ClientSize;\r
-        const string showStr = "D2DBenchMark";\r
-        Font font;\r
-\r
-        public void Init(Control ctrl, bool antialias)\r
-        {\r
-            ClientSize = ctrl.ClientSize;\r
-            gdi = new GDI(ctrl);\r
-            colors = new ColorTableGDI();\r
-            font = ctrl.Font;\r
-            ctrl.Show();\r
-        }\r
-\r
-        public void Exec()\r
-        {\r
-            Random rnd = new Random();\r
-            gdi.BeginDraw();\r
-            for (int j = 0; j < this.loopCount; j++)\r
-            {\r
-                Color c = colors[rnd.Next(colors.Count - 1)];\r
-                gdi.DrawString(font, showStr, c, rnd.Next(ClientSize.Width), rnd.Next(ClientSize.Height));\r
-            }\r
-            gdi.EndDraw();\r
-        }\r
-\r
-        public void Dispose()\r
-        {\r
-            colors.Clear();\r
-            gdi.Dispose();\r
-        }\r
-    }\r
-}\r
diff --git a/D2DBench/Method6.cs b/D2DBench/Method6.cs
deleted file mode 100644 (file)
index 5710827..0000000
+++ /dev/null
@@ -1,49 +0,0 @@
-using System;\r
-using System.Drawing;\r
-using System.Diagnostics;\r
-using System.Windows.Forms;\r
-\r
-namespace D2DBench\r
-{\r
-    class Method6: IBench\r
-    {\r
-        public int fillAreaSize { get { return 0; } }\r
-        public string methodName { get { return "GDI線"; } }\r
-        public int loopCount { get; set; }\r
-\r
-        GDI gdi;\r
-        ColorTableGDI colors;\r
-        Size ClientSize;\r
-        const string showStr = "D2DBenchMark";\r
-        Font font;\r
-\r
-        public void Init(Control ctrl, bool antialias)\r
-        {\r
-            ClientSize = ctrl.ClientSize;\r
-            gdi = new GDI(ctrl);\r
-            colors = new ColorTableGDI();\r
-            font = ctrl.Font;\r
-            ctrl.Show();\r
-        }\r
-\r
-        public void Exec()\r
-        {\r
-            Random rnd = new Random();\r
-            gdi.BeginDraw();\r
-            for (int j = 0; j < this.loopCount; j++)\r
-            {\r
-                Color fore = colors[rnd.Next(colors.Count - 1)];\r
-                Point from = new Point(rnd.Next(ClientSize.Width), rnd.Next(ClientSize.Height));\r
-                Point to = new Point(rnd.Next(ClientSize.Width), rnd.Next(ClientSize.Height));\r
-                gdi.DrawLine(from, to, 1, fore);\r
-            }\r
-            gdi.EndDraw();\r
-        }\r
-\r
-        public void Dispose()\r
-        {\r
-            colors.Clear();\r
-            gdi.Dispose();\r
-        }\r
-    }\r
-}\r
diff --git a/D2DBench/Method7.cs b/D2DBench/Method7.cs
deleted file mode 100644 (file)
index d91e142..0000000
+++ /dev/null
@@ -1,53 +0,0 @@
-using System;\r
-using System.Drawing;\r
-using System.Diagnostics;\r
-using System.Windows.Forms;\r
-\r
-namespace D2DBench\r
-{\r
-    class Method7 : IBench\r
-    {\r
-        public int fillAreaSize { get { return bmpSize.Width * bmpSize.Height * this.loopCount; } }\r
-        public string methodName { get { return "GDI塗りつぶし"; } }\r
-        public int loopCount { get; set; }\r
-\r
-        GDI gdi;\r
-        ColorTableGDI colors;\r
-        Size ClientSize;\r
-        const string showStr = "D2DBenchMark";\r
-        Font font;\r
-        Size bmpSize = D2DBench.Properties.Resources.image.Size;\r
-\r
-        public void Init(Control ctrl, bool antialias)\r
-        {\r
-            ClientSize = ctrl.ClientSize;\r
-            gdi = new GDI(ctrl);\r
-            colors = new ColorTableGDI();\r
-            font = ctrl.Font;\r
-            ctrl.Show();\r
-        }\r
-\r
-        public void Exec()\r
-        {\r
-            Random rnd = new Random();\r
-            gdi.BeginDraw();\r
-            for (int j = 0; j < this.loopCount; j++)\r
-            {\r
-                Color fore = colors[rnd.Next(colors.Count - 1)];\r
-                Rectangle rect = new Rectangle();\r
-                rect.X = rnd.Next(ClientSize.Width);\r
-                rect.Y = rnd.Next(ClientSize.Height);\r
-                rect.Width = bmpSize.Width;\r
-                rect.Height = bmpSize.Height;\r
-                gdi.FillRectangle(rect, fore);\r
-            }\r
-            gdi.EndDraw();\r
-        }\r
-\r
-        public void Dispose()\r
-        {\r
-            colors.Clear();\r
-            gdi.Dispose();\r
-        }\r
-    }\r
-}\r
diff --git a/D2DBench/Method8.cs b/D2DBench/Method8.cs
deleted file mode 100644 (file)
index d8fa229..0000000
+++ /dev/null
@@ -1,56 +0,0 @@
-using System;\r
-using System.Drawing;\r
-using System.Diagnostics;\r
-using System.Windows.Forms;\r
-using D2DBench.Properties;\r
-\r
-namespace D2DBench\r
-{\r
-    class Method8 : IBench\r
-    {\r
-        public int fillAreaSize { get { return bmpSize.Width * bmpSize.Height * this.loopCount; } }\r
-        public string methodName { get { return "GDIBitBlt"; } }\r
-        public int loopCount { get; set; }\r
-\r
-        GDI gdi;\r
-        ColorTableGDI colors;\r
-        Size ClientSize;\r
-        const string showStr = "D2DBenchMark";\r
-        Font font;\r
-        Size bmpSize;\r
-        GDIBitmap bmp;\r
-\r
-        public void Init(Control ctrl, bool antialias)\r
-        {\r
-            ClientSize = ctrl.ClientSize;\r
-            gdi = new GDI(ctrl);\r
-            colors = new ColorTableGDI();\r
-            font = ctrl.Font;\r
-            bmpSize = Resources.image.Size;\r
-            bmp = gdi.LoadImage(Resources.image);\r
-            ctrl.Show();\r
-        }\r
-\r
-        public void Exec()\r
-        {\r
-            Random rnd = new Random();\r
-            gdi.BeginDraw();\r
-            for (int j = 0; j < this.loopCount; j++)\r
-            {\r
-                Rectangle rect = new Rectangle();\r
-                rect.X = rnd.Next(ClientSize.Width);\r
-                rect.Y = rnd.Next(ClientSize.Height);\r
-                rect.Width = rect.X + bmpSize.Width;\r
-                rect.Height = rect.Y + bmpSize.Height;\r
-                gdi.DrawImage(rect, bmp);\r
-            }\r
-            gdi.EndDraw();\r
-        }\r
-\r
-        public void Dispose()\r
-        {\r
-            gdi.UnloadImage(bmp);\r
-            gdi.Dispose();\r
-        }\r
-    }\r
-}\r