4using System.Threading.Tasks;
7using SkiaSharp.Views.Gtk;
17 #region Fields and Properties
21 private SKImage _currentRenderedImage;
22 private PointD _lastOrigin;
23 private double _lastResolution;
24 private ZCT _lastCoordinate;
25 private int _lastWidth;
26 private int _lastHeight;
29 private OpenSlideGTK.OpenSlideBase _openSlideSource;
30 private SlideBase _slideSource;
31 private bool _isOpenSlide;
32 private bool _hasSource;
34 public int MaxCacheSizeMB {
get;
set; } = 512;
35 public int TileSize {
get;
set; } = 256;
36 public SKSamplingOptions Sampling {
get;
set; } = SKSamplingOptions.Default;
37 public bool EnablePrefetch {
get;
set; } =
true;
40 public bool IsRendering {
get;
private set; }
41 public bool HasValidImage => _currentRenderedImage !=
null;
55 private void InitializeDefaults()
57 _lastOrigin =
new PointD(0, 0);
58 _lastResolution = 1.0;
59 _lastCoordinate =
new ZCT(0, 0, 0);
66 #region Source Management
71 public void SetSource(OpenSlideGTK.OpenSlideBase openSlideSource)
73 if (openSlideSource ==
null)
74 throw new ArgumentNullException(nameof(openSlideSource));
78 _openSlideSource = openSlideSource;
82 InitializeStitchingPipeline();
90 if (slideSource ==
null)
91 throw new ArgumentNullException(nameof(slideSource));
95 _slideSource = slideSource;
99 InitializeStitchingPipeline();
105 private void InitializeStitchingPipeline()
107 _stitchingPipeline?.Dispose();
115 _stitchingPipeline =
new SkiaStitchingPipeline(_slideSource, MaxCacheSizeMB);
119 _stitchingPipeline.TileSize = TileSize;
121 _stitchingPipeline.EnablePrefetch = EnablePrefetch;
127 private void ClearCurrentSource()
129 _currentRenderedImage?.Dispose();
130 _currentRenderedImage =
null;
132 _stitchingPipeline?.Dispose();
133 _stitchingPipeline =
null;
140 #region Async View Update
152 if (!_hasSource || _stitchingPipeline ==
null)
174 _lastOrigin = origin;
175 _lastResolution = resolution;
176 _lastCoordinate = coordinate;
178 _lastHeight = height;
187 await RenderFullQualityAsync(origin, resolution, coordinate, width, height);
191 Console.WriteLine($
"Error during view update: {ex.Message}");
202 private async Task RenderLowResPreviewAsync(
212 double previewResolution = resolution * 4.0;
213 int previewWidth = width / 2;
214 int previewHeight = height / 2;
237 Console.WriteLine($
"Preview render error: {ex.Message}");
244 private async Task RenderFullQualityAsync(
262 if (stitchedImage !=
null)
264 _currentRenderedImage?.Dispose();
265 _currentRenderedImage = stitchedImage;
273 Console.WriteLine($
"Full quality render error: {ex.Message}");
279 #region Synchronous Rendering Methods
287 ZCT coordinate,
double resolution,
int pxwidth,
int pxheight)
289 if (!_hasSource || _stitchingPipeline ==
null)
292 return _stitchingPipeline.
StitchRegion(region, level, coordinate, resolution, pxwidth, pxheight);
300 if (!_hasSource || _stitchingPipeline ==
null)
314 #region Image Access for Rendering
323 return _currentRenderedImage;
330 public void DrawToCanvas(SKCanvas canvas,
int canvasWidth,
int canvasHeight)
340 using (var paint =
new SKPaint
343 BlendMode = SKBlendMode.SrcOver
347 var destRect =
new SKRect(0, 0, canvasWidth, canvasHeight);
348 canvas.DrawImage(image, destRect, paint);
353 Console.WriteLine($
"Error drawing to canvas: {ex.Message}");
359 #region Update Management
364 private bool NeedsUpdate(
371 const double EPSILON = 0.0001;
373 return Math.Abs(origin.X - _lastOrigin.X) > EPSILON ||
374 Math.Abs(origin.Y - _lastOrigin.Y) > EPSILON ||
375 Math.Abs(resolution - _lastResolution) > EPSILON ||
376 width != _lastWidth ||
377 height != _lastHeight ||
378 coordinate.Z != _lastCoordinate.Z ||
379 coordinate.C != _lastCoordinate.C ||
380 coordinate.T != _lastCoordinate.T;
386 private bool ShouldRenderPreview(
double resolution)
389 return resolution < 1.0;
395 private void RequestRedraw()
397 Gtk.Application.Invoke((sender, args) =>
399 App.viewer.RequestDeferredRender();
405 #region Cache Management
423 IsEnabled = _stitchingPipeline !=
null,
424 MaxSizeMB = MaxCacheSizeMB,
431 public bool IsEnabled {
get;
set; }
432 public int MaxSizeMB {
get;
set; }
433 public int TileSize {
get;
set; }
438 #region Configuration Updates
444 int? maxCacheSizeMB =
null,
445 int? tileSize =
null,
446 SKSamplingOptions? sampling =
null,
447 bool? enablePrefetch =
null,
448 bool? enableProgressiveRendering =
null)
450 bool needsReinit =
false;
452 if (maxCacheSizeMB.HasValue && maxCacheSizeMB.Value != MaxCacheSizeMB)
454 MaxCacheSizeMB = maxCacheSizeMB.Value;
458 if (tileSize.HasValue && tileSize.Value != TileSize)
460 TileSize = tileSize.Value;
465 if (needsReinit && _hasSource)
467 InitializeStitchingPipeline();
475 public void Dispose()
477 _currentRenderedImage?.Dispose();
478 _stitchingPipeline?.Dispose();
479 _currentRenderedImage =
null;
480 _stitchingPipeline =
null;
Manages slide rendering by coordinating between the SkiaStitchingPipeline and SlideGLArea Handles asy...
SKSlideRenderer(SKDrawingArea sk)
Initialize renderer for a specific GLArea widget.
async Task UpdateViewAsync(PointD origin, double resolution, ZCT coordinate, int width=600, int height=400)
Update the rendered view asynchronously - main entry point for rendering.
CacheStatistics GetCacheStatistics()
Get cache statistics for monitoring.
void SetSource(OpenSlideGTK.OpenSlideBase openSlideSource)
Set OpenSlide source and initialize pipeline.
void ClearCache()
Clear the tile cache.
SKImage RenderRegion(RectangleD region, int level, ZCT coordinate, double resolution, int pxwidth, int pxheight)
Render a specific region synchronously (for exports, small areas)
void DrawToCanvas(SKCanvas canvas, int canvasWidth, int canvasHeight)
Draw the current image to a canvas with proper scaling Called during the Skia render callback from Sl...
void SetSource(SlideBase slideSource)
Set BioLib slide source and initialize pipeline.
void UpdateConfiguration(int? maxCacheSizeMB=null, int? tileSize=null, SKSamplingOptions? sampling=null, bool? enablePrefetch=null, bool? enableProgressiveRendering=null)
Update renderer configuration and reinitialize if needed.
SKImage RenderCurrentViewSync()
Render current viewport synchronously (blocking)
SKImage GetCurrentImage()
Get the current image for rendering - called by GLArea during draw Returns the best available image (...
SkiaSharp-based tile stitching pipeline for compositing large pyramidal images.
void ClearCache()
Clear entire cache.
SKImage StitchRegion(RectangleD region, int level, ZCT coordinate, double resolution, int pxwidth, int pxheight)
Stitch a specific region synchronously (for smaller areas)
async Task< SKImage > StitchViewportAsync(PointD origin, double resolution, ZCT coordinate, int viewportWidth=600, int viewportHeight=400)
Stitch tiles for a given viewport asynchronously.