Наследование: ImageSource, ISurfaceImageSource
 /// <summary>
 /// Initialzes a new <see cref="SurfaceImageSourceTarget"/> instance.
 /// </summary>
 /// <param name="pixelWidth">Width of the target in pixels</param>
 /// <param name="pixelHeight">Height of the target in pixels</param>
 public SurfaceImageSourceTarget(int pixelWidth, int pixelHeight)
 {
     this.pixelWidth = pixelWidth;
     this.pixelHeight = pixelHeight;
     this.surfaceImageSource = new SurfaceImageSource(pixelWidth, pixelHeight);
     surfaceImageSourceNative = ComObject.As<SharpDX.DXGI.ISurfaceImageSourceNative>(surfaceImageSource);
 }
 /// <summary>
 /// Initialzes a new <see cref="SurfaceImageSourceTarget"/> instance.
 /// </summary>
 /// <param name="pixelWidth">Width of the target in pixels</param>
 /// <param name="pixelHeight">Height of the target in pixels</param>
 public SurfaceImageSourceTarget(int pixelWidth, int pixelHeight, bool supportOpacity = false)
 {
     this.pixelWidth = pixelWidth;
     this.pixelHeight = pixelHeight;
     this.surfaceImageSource = new SurfaceImageSource(pixelWidth, pixelHeight, supportOpacity);
     surfaceImageSourceNative = Collect(ComObject.As<SharpDX.DXGI.ISurfaceImageSourceNative>(surfaceImageSource));
 }
 /// <summary>
 /// Initialzes a new <see cref="SurfaceImageSourceTarget"/> instance.
 /// </summary>
 /// <param name="pixelWidth">Width of the target in pixels</param>
 /// <param name="pixelHeight">Height of the target in pixels</param>
 public SurfaceImageSourceTarget(int pixelWidth, int pixelHeight, bool supportOpacity = false)
 {
     this.pixelWidth = pixelWidth;
     this.pixelHeight = pixelHeight;
     this.surfaceImageSource = new SurfaceImageSource(pixelWidth, pixelHeight, supportOpacity);
     surfaceImageSourceNative = ToDispose(ComObject.As<SharpDX.DXGI.ISurfaceImageSourceNative>(surfaceImageSource));
     viewDatas[0] = new SurfaceViewData();
     viewDatas[1] = new SurfaceViewData();
 }
 /// <summary>
 /// Initialzes a new <see cref="SurfaceImageSourceTarget"/> instance.
 /// </summary>
 /// <param name="pixelWidth">Width of the target in pixels</param>
 /// <param name="pixelHeight">Height of the target in pixels</param>
 /// <param name="isOpaque">Indicates whether the surface is expected to be always opaque (without transparency support).</param>
 public SurfaceImageSourceTarget(int pixelWidth, int pixelHeight, bool isOpaque = false)
 {
     this.pixelWidth = pixelWidth;
     this.pixelHeight = pixelHeight;
     this.surfaceImageSource = new SurfaceImageSource(pixelWidth, pixelHeight, isOpaque);
     surfaceImageSourceNative = ComObject.As<SharpDX.DXGI.ISurfaceImageSourceNative>(surfaceImageSource);
     viewDatas[0] = new SurfaceViewData();
     viewDatas[1] = new SurfaceViewData();
 }
        protected override void Dispose(bool disposeManagedResources)
        {
            this.surfaceImageSource = null;
            if (surfaceImageSourceNative != null) { 
                surfaceImageSourceNative.Dispose();
                surfaceImageSourceNative = null;
            }

            for (int i = 0; i < viewDatas.Length; i++)
            {
                viewDatas[i].Dispose();
                viewDatas[i] = null;
            }

            
            base.Dispose(disposeManagedResources);
        }
Пример #6
0
        public SurfaceImageSourceCanvas(SurfaceImageSource surfaceImageSource, Rect updateRect, Direct2DFactories factories = null)
            : base(factories)
        {
            sisn = ComObject.As<DXGI.ISurfaceImageSourceNative> (surfaceImageSource);
            SharpDX.Point offset;
            var surface = sisn.BeginDraw (updateRect.ToRectangle (), out offset);

            var dpi = 96.0;
            var properties = new D2D1.RenderTargetProperties (D2D1.RenderTargetType.Default, new D2D1.PixelFormat (DXGI.Format.Unknown, D2D1.AlphaMode.Unknown), (float)(dpi), (float)(dpi), D2D1.RenderTargetUsage.None, D2D1.FeatureLevel.Level_DEFAULT);
            Initialize (new D2D1.RenderTarget (this.factories.D2DFactory, surface, properties));
        }
Пример #7
0
        private void DirectXRender(List<UIElement> points)
        {
            if (points.Count <= 0)
                return;

            Line initLine = points[0] as Line;

            Point
                p1 = new Point
                (
                    Math.Min(initLine.X1 - initLine.StrokeThickness / 2, initLine.X2 - initLine.StrokeThickness / 2),
                    Math.Min(initLine.Y1 - initLine.StrokeThickness / 2, initLine.Y2 - initLine.StrokeThickness / 2)
                ),
                p2 = new Point
                (
                    Math.Max(initLine.X1 + initLine.StrokeThickness / 2, initLine.X2 + initLine.StrokeThickness / 2),
                    Math.Max(initLine.Y1 + initLine.StrokeThickness / 2, initLine.Y2 + initLine.StrokeThickness / 2)
                );

            foreach (var child in points)
            {
                var line = child as Line;

                if (line == null)
                    continue;

                if (p1.X > line.X1 - line.StrokeThickness / 2)
                    p1.X = line.X1 - line.StrokeThickness / 2;
                if (p1.X > line.X2 - line.StrokeThickness / 2)
                    p1.X = line.X2 - line.StrokeThickness / 2;

                if (p2.X < line.X1 + line.StrokeThickness / 2)
                    p2.X = line.X1 + line.StrokeThickness / 2;
                if (p2.X < line.X2 + line.StrokeThickness / 2)
                    p2.X = line.X2 + line.StrokeThickness / 2;

                if (p1.Y > line.Y1 - line.StrokeThickness / 2)
                    p1.Y = line.Y1 - line.StrokeThickness / 2;
                if (p1.Y > line.Y2 - line.StrokeThickness / 2)
                    p1.Y = line.Y2 - line.StrokeThickness / 2;

                if (p2.Y < line.Y1 + line.StrokeThickness / 2)
                    p2.Y = line.Y1 + line.StrokeThickness / 2;
                if (p2.Y < line.Y2 + line.StrokeThickness / 2)
                    p2.Y = line.Y2 + line.StrokeThickness / 2;
            }

            var bndRect = new Rect(p1, p2);

            var dxTarget = new SurfaceImageSource
            (
                (int)(bndRect.Width * DisplayProperties.LogicalDpi / 96.0 + 1),
                (int)(bndRect.Height * DisplayProperties.LogicalDpi / 96.0 + 1)
            );

            SharpDX.DXGI.ISurfaceImageSourceNative dxTargetNative = SharpDX.ComObject.As<SharpDX.DXGI.ISurfaceImageSourceNative>(dxTarget);
            dxTargetNative.Device = d3dDevice.QueryInterface<SharpDX.DXGI.Device>();

            /*
             * Draw Logic
             */
            SharpDX.DrawingPoint drawingPoint;
            var surface = dxTargetNative.BeginDraw(new SharpDX.Rectangle(0, 0,
                (int)(bndRect.Width * DisplayProperties.LogicalDpi / 96.0 + 1),
                (int)(bndRect.Height * DisplayProperties.LogicalDpi / 96.0 + 1)),
                out drawingPoint);

            var dxRenderTarget = new SharpDX.Direct2D1.RenderTarget(d2dFactory, surface, new SharpDX.Direct2D1.RenderTargetProperties()
            {
                DpiX = DisplayProperties.LogicalDpi,
                DpiY = DisplayProperties.LogicalDpi,
                PixelFormat = new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied),
                Type = SharpDX.Direct2D1.RenderTargetType.Default,
                Usage = SharpDX.Direct2D1.RenderTargetUsage.None
            });

            dxRenderTarget.BeginDraw();
            dxRenderTarget.Clear(SharpDX.Color.Transparent);

            foreach (var child in points)
            {
                var line = child as Line;

                if (line == null)
                    continue;

                Color c = (line.Stroke as SolidColorBrush).Color;
                var brush = new SharpDX.Direct2D1.SolidColorBrush(dxRenderTarget, new SharpDX.Color(c.R, c.G, c.B, c.A));

                var style = new SharpDX.Direct2D1.StrokeStyleProperties();
                style.LineJoin = SharpDX.Direct2D1.LineJoin.Round;
                style.StartCap = SharpDX.Direct2D1.CapStyle.Round;
                style.EndCap = SharpDX.Direct2D1.CapStyle.Round;
                var stroke = new SharpDX.Direct2D1.StrokeStyle(d2dFactory, style);

                dxRenderTarget.DrawLine(
                    new SharpDX.DrawingPointF((float)(line.X1 - bndRect.Left), (float)(line.Y1 - bndRect.Top)),
                    new SharpDX.DrawingPointF((float)(line.X2 - bndRect.Left), (float)(line.Y2 - bndRect.Top)),
                    brush, (float)line.StrokeThickness, stroke);
            }

            dxRenderTarget.EndDraw();
            dxTargetNative.EndDraw();

            var dxImage = new Image();
            dxImage.Source = dxTarget;
            canvas.Children.Add(dxImage);
            Canvas.SetLeft(dxImage, bndRect.X);
            Canvas.SetTop(dxImage, bndRect.Y);
        }
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            MyChildRenderItem.RenderDataStruct Item = (MyChildRenderItem.RenderDataStruct)value;
            if (Item.FontSize != AppSettings.dFontSize)
            {
                Item.sizeFunc(); //cannot react this late
                Item = (MyChildRenderItem.RenderDataStruct)value;
            }
            int pixelWidth = (int)Item.Width, pixelHeight = (int)Item.Height;
            SurfaceImageSource newSource = new SurfaceImageSource(pixelWidth, pixelHeight, false);
            //SharpDX.Direct3D11.Device D3DDev = new SharpDX.Direct3D11.Device(SharpDX.Direct3D.DriverType.Hardware, SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport);
            //SharpDX.DXGI.Device DXDev = D3DDev.QueryInterface<SharpDX.DXGI.Device>();
#if WINDOWS_PHONE_APP || !STORETOOLKIT
            SharpDX.DXGI.ISurfaceImageSourceNativeWithD2D surfaceImageSourceNative = SharpDX.ComObject.As<SharpDX.DXGI.ISurfaceImageSourceNativeWithD2D>(newSource);
#else
            SharpDX.DXGI.ISurfaceImageSourceNative surfaceImageSourceNative = SharpDX.ComObject.As<SharpDX.DXGI.ISurfaceImageSourceNative>(newSource);
#endif
            SharpDX.Rectangle rt = new SharpDX.Rectangle(0, 0, pixelWidth, pixelHeight);
#if WINDOWS_PHONE_APP || !STORETOOLKIT
            SharpDX.Point pt;
            IntPtr obj;
            surfaceImageSourceNative.Device = TextShaping.Dev2D;
            surfaceImageSourceNative.BeginDraw(rt, new Guid("e8f7fe7a-191c-466d-ad95-975678bda998"), out obj, out pt); //d2d1_1.h
            SharpDX.Direct2D1.DeviceContext devcxt = SharpDX.ComObject.As<SharpDX.Direct2D1.DeviceContext>(obj);
#else
            SharpDX.DrawingPoint pt;
            surfaceImageSourceNative.Device = TextShaping.DXDev;
            SharpDX.DXGI.Surface surf = surfaceImageSourceNative.BeginDraw(rt, out pt);
            SharpDX.Direct2D1.DeviceContext devcxt = new SharpDX.Direct2D1.DeviceContext(surf);
            //SharpDX.Direct2D1.Bitmap1 bmp = new SharpDX.Direct2D1.Bitmap1(devcxt, surf, new SharpDX.Direct2D1.BitmapProperties1() { DpiX = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().RawDpiX, DpiY = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().RawDpiY, PixelFormat = new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied), BitmapOptions = SharpDX.Direct2D1.BitmapOptions.CannotDraw | SharpDX.Direct2D1.BitmapOptions.Target });
            //devcxt.Target = bmp;
            devcxt.BeginDraw();
#endif
            devcxt.Clear(new SharpDX.Color4(Windows.UI.Colors.White.R / 255.0f, Windows.UI.Colors.White.G / 255.0f, Windows.UI.Colors.White.B / 255.0f, Windows.UI.Colors.Transparent.A / 255.0f));
            SharpDX.Direct2D1.Layer lyr = new SharpDX.Direct2D1.Layer(devcxt);
            //SharpDX.RectangleF.Infinite
            devcxt.PushLayer(new SharpDX.Direct2D1.LayerParameters1(new SharpDX.RectangleF(float.NegativeInfinity, float.NegativeInfinity, float.PositiveInfinity, float.PositiveInfinity), null, SharpDX.Direct2D1.AntialiasMode.PerPrimitive, SharpDX.Matrix3x2.Identity, 1.0f, null, SharpDX.Direct2D1.LayerOptions1.None), lyr);
            devcxt.PushAxisAlignedClip(new SharpDX.RectangleF(pt.X, pt.Y, pt.X + pixelWidth, pt.Y + pixelHeight), SharpDX.Direct2D1.AntialiasMode.PerPrimitive);
            devcxt.Transform = SharpDX.Matrix3x2.Translation(pt.X, pt.Y);
            SharpDX.DirectWrite.GlyphRun gr = new SharpDX.DirectWrite.GlyphRun();
            gr.FontFace = TextShaping.DWFontFace;
            gr.FontSize = (float)TopLevelFontSize;
            gr.BidiLevel = -1;
            int curlen = 0;
            for (int ct = 0; ct < Item.ItemRuns.Count(); ct++)
            {
                int newlen = curlen + Item.ItemRuns[ct].ItemText.Length;
                gr.Indices = Item.indices.Skip(Item.clusters[curlen]).TakeWhile((indice, idx) => ct == Item.ItemRuns.Count() - 1 || idx < Item.clusters[newlen]).ToArray();
                gr.Offsets = Item.offsets.Skip(Item.clusters[curlen]).TakeWhile((offset, idx) => ct == Item.ItemRuns.Count() - 1 || idx < Item.clusters[newlen]).ToArray();
                gr.Advances = Item.advances.Skip(Item.clusters[curlen]).TakeWhile((advance, idx) => ct == Item.ItemRuns.Count() - 1 || idx < Item.clusters[newlen]).ToArray();
                if (Item.ItemRuns[ct].ItemText[0] == XMLRender.ArabicData.ArabicEndOfAyah) gr.Advances[0] = 0;
                SharpDX.Direct2D1.SolidColorBrush brsh = new SharpDX.Direct2D1.SolidColorBrush(devcxt, new SharpDX.Color4(XMLRender.Utility.ColorR(Item.ItemRuns[ct].Clr) / 255.0f, XMLRender.Utility.ColorG(Item.ItemRuns[ct].Clr) / 255.0f, XMLRender.Utility.ColorB(Item.ItemRuns[ct].Clr) / 255.0f, 0xFF / 255.0f));
                devcxt.DrawGlyphRun(new SharpDX.Vector2((float) pt.X + (float)pixelWidth + Item.offsets[0].AdvanceOffset - (Item.clusters[curlen] == 0 ? 0 : Item.advances.Take(Item.clusters[curlen]).Sum()), Item.BaseLine), gr, brsh, SharpDX.Direct2D1.MeasuringMode.Natural);
                brsh.Dispose();
                curlen = newlen;
            }
            devcxt.Transform = SharpDX.Matrix3x2.Identity;
            devcxt.PopAxisAlignedClip();
            devcxt.PopLayer();
#if WINDOWS_PHONE_APP || !STORETOOLKIT
            gr.Dispose();
#else
            gr.FontFace = null;
#endif
#if WINDOWS_PHONE_APP || !STORETOOLKIT
#else
            devcxt.EndDraw();
            devcxt.Target = null;
            //bmp.Dispose();
#endif
            devcxt.Dispose();
#if WINDOWS_PHONE_APP || !STORETOOLKIT
#else
            surf.Dispose();
#endif
            surfaceImageSourceNative.EndDraw();
            surfaceImageSourceNative.Device = null;
            surfaceImageSourceNative.Dispose();
            //DXDev.Dispose();
            //D3DDev.Dispose();
            return newSource;
        }