public void Render(IDrawingContextImpl context) { if (context is not ISkiaDrawingContextImpl skia) { return; } if (!skia.SkCanvas.TotalMatrix.TryInvert(out var currentInvertedTransform)) { return; } // One or both dimensions has zero size. // Skia doesnt like that. if (_bounds.Size.AspectRatio == 0) { return; } using var backgroundSnapshot = skia.SkSurface.Snapshot(); using var preBlur = DrawingContextHelper.CreateDrawingContext( new Size(backgroundSnapshot.Width, backgroundSnapshot.Height), new Vector(96, 96), skia.GrContext); using (var filter = SKImageFilter.CreateBlur((int)_blurRadius.X, (int)_blurRadius.Y, SKShaderTileMode.Clamp)) using (var blurPaint = new SKPaint { ImageFilter = filter }) { var canvas = preBlur.SkSurface.Canvas; canvas.DrawSurface(skia.SkSurface, 0f, 0f, blurPaint); canvas.Flush(); } using var preBlurredSnapshot = preBlur.SkSurface.Snapshot(); using var backdropShader = SKShader.CreateImage(preBlurredSnapshot, SKShaderTileMode.Clamp, SKShaderTileMode.Clamp, currentInvertedTransform); using var blurred = DrawingContextHelper.CreateDrawingContext(_bounds.Size, new Vector(96, 96), skia.GrContext); using (var blurPaint = new SKPaint { Shader = backdropShader }) { blurred.SkSurface.Canvas.DrawRect(0, 0, (float)_bounds.Width, (float)_bounds.Height, blurPaint); } blurred.DrawTo(skia); }
internal override void UpdatePaint(SKPaint fillPaint, SKRect bounds) { if (Surface is SkiaCompositionSurface scs) { var imageShader = SKShader.CreateImage(scs.Image, SKShaderTileMode.Repeat, SKShaderTileMode.Repeat, TransformMatrix.ToSKMatrix()); var opacity = 255 * Compositor.CurrentOpacity; var filteredImageShader = SKShader.CreateColorFilter(imageShader, SKColorFilter.CreateBlendMode(new SKColor(0xFF, 0xFF, 0xFF, (byte)opacity), SKBlendMode.Modulate)); fillPaint.Shader = filteredImageShader; fillPaint.IsAntialias = true; } else { fillPaint.Shader = null; } }
public bool Draw(SKCanvas canvas, IReadOnlyViewport viewport, ILayer layer, IFeature feature, IStyle istyle, ISymbolCache symbolCache) { var style = ((TiledBitmapStyle)istyle); if (style.image == null) { return(false); } var position = feature.Geometry.BoundingBox.Centroid; var dest = viewport.WorldToScreen(position); var zoom = 1 / (float)viewport.Resolution; canvas.Translate((float)dest.X, (float)dest.Y); canvas.Scale(zoom, zoom); canvas.RotateDegrees((float)viewport.Rotation, 0.0f, 0.0f); if (style.rotation != 0) { canvas.RotateDegrees(style.rotation, 0.0f, 0.0f); } //#TODO store paint with shader in the style using (SKPaint paint = new SKPaint()) { if (style.rotation == 0) //Weird artifacting on 0 rotation, no idea why. Seems Skia bug. { style.rotation = 180; } SKMatrix shaderTransform = SKMatrix.CreateScale((float)viewport.Resolution, (float)viewport.Resolution); if (style.rotation != 0) { shaderTransform = SKMatrix.Concat(shaderTransform, SKMatrix.CreateRotationDegrees(-style.rotation)); } paint.Shader = SKShader.CreateImage(style.image, SKShaderTileMode.Repeat, SKShaderTileMode.Repeat, shaderTransform); paint.ColorFilter = style.colorFilter; //style.image.Encode().SaveTo(File.Create($"P:/{style.image.UniqueId}.png")); if (style.ellipse) { canvas.DrawOval(0, 0, style.rect.Right, style.rect.Bottom, paint); } else { canvas.DrawRect(style.rect, paint); } if (style.border) { var borderPaint = new SKPaint { Style = SKPaintStyle.Stroke, Color = style.color }; if (style.ellipse) { canvas.DrawOval(0, 0, style.rect.Right, style.rect.Bottom, borderPaint); } else { canvas.DrawRect(style.rect, borderPaint); } } } return(true); }