BioGTK  6.5.0
A .NET library & program for annotating, editing various microscopy imaging formats using Bioformats supported images. Including whole slide, pyramidal, and series.
Loading...
Searching...
No Matches
BioGTK.SKSlideRenderer Class Reference

Manages slide rendering by coordinating between the SkiaStitchingPipeline and SlideGLArea Handles async updates, caching, and progressive rendering for large pyramidal images. More...

Inheritance diagram for BioGTK.SKSlideRenderer:

Classes

class  CacheStatistics
 

Public Member Functions

 SKSlideRenderer (SKDrawingArea sk)
 Initialize renderer for a specific GLArea widget.
 
void SetSource (OpenSlideGTK.OpenSlideBase openSlideSource)
 Set OpenSlide source and initialize pipeline.
 
void SetSource (SlideBase slideSource)
 Set BioLib slide source and initialize pipeline.
 
void InvalidateRenderState ()
 Marks any in-flight render as stale so the next viewport update can replace it without the old result winning the race.
 
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.
 
SKImage RenderRegion (RectangleD region, int level, ZCT coordinate, double resolution, int pxwidth, int pxheight)
 Render a specific region synchronously (for exports, small areas)
 
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 (full quality or preview)
 
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 SlideGLArea.
 
void ClearCache ()
 Clear the tile cache.
 
CacheStatistics GetCacheStatistics ()
 Get cache statistics for monitoring.
 
void UpdateConfiguration (int? maxCacheSizeMB=null, int? tileSize=null, SKSamplingOptions? sampling=null, bool? enablePrefetch=null, bool? enableProgressiveRendering=null)
 Update renderer configuration and reinitialize if needed.
 
void Dispose ()
 

Properties

bool LastRenderSkipped [get]
 True if the last UpdateViewAsync call was skipped because a render was already in progress.
 
int MaxCacheSizeMB = 512 [get, set]
 
int TileSize = 256 [get, set]
 
SKSamplingOptions Sampling = SKSamplingOptions.Default [get, set]
 
bool EnablePrefetch = true [get, set]
 
bool IsRendering [get]
 
bool HasValidImage [get]
 

Detailed Description

Manages slide rendering by coordinating between the SkiaStitchingPipeline and SlideGLArea Handles async updates, caching, and progressive rendering for large pyramidal images.

Definition at line 15 of file SKSlideRenderer.cs.

Constructor & Destructor Documentation

◆ SKSlideRenderer()

BioGTK.SKSlideRenderer.SKSlideRenderer ( SKDrawingArea sk)

Initialize renderer for a specific GLArea widget.

Definition at line 55 of file SKSlideRenderer.cs.

56 {
57 InitializeDefaults();
58 }

Member Function Documentation

◆ ClearCache()

void BioGTK.SKSlideRenderer.ClearCache ( )

Clear the tile cache.

Definition at line 429 of file SKSlideRenderer.cs.

430 {
431 _stitchingPipeline?.ClearCache();
432 }
void ClearCache()
Clear entire cache.

◆ Dispose()

void BioGTK.SKSlideRenderer.Dispose ( )

Definition at line 494 of file SKSlideRenderer.cs.

495 {
496 if (_disposed)
497 return;
498
499 _disposed = true;
500 _currentRenderedImage?.Dispose();
501 _stitchingPipeline?.Dispose();
502 _currentRenderedImage = null;
503 _stitchingPipeline = null;
504 }

◆ DrawToCanvas()

void BioGTK.SKSlideRenderer.DrawToCanvas ( SKCanvas canvas,
int canvasWidth,
int canvasHeight )

Draw the current image to a canvas with proper scaling Called during the Skia render callback from SlideGLArea.

Definition at line 349 of file SKSlideRenderer.cs.

350 {
351 if (canvas == null)
352 return;
353
354 var image = GetCurrentImage();
355 if (image == null)
356 return;
357 try
358 {
359 using (var paint = new SKPaint
360 {
361 IsAntialias = true,
362 BlendMode = SKBlendMode.SrcOver
363 })
364 {
365 // Draw image to fill canvas
366 var destRect = new SKRect(0, 0, canvasWidth, canvasHeight);
367 canvas.DrawImage(image, destRect, paint);
368 }
369 }
370 catch (Exception ex)
371 {
372 Console.WriteLine($"Error drawing to canvas: {ex.Message}");
373 }
374 }
SKImage GetCurrentImage()
Get the current image for rendering - called by GLArea during draw Returns the best available image (...

◆ GetCacheStatistics()

CacheStatistics BioGTK.SKSlideRenderer.GetCacheStatistics ( )

Get cache statistics for monitoring.

Definition at line 437 of file SKSlideRenderer.cs.

438 {
439 // Could be extended to expose actual stats from pipeline
440 return new CacheStatistics
441 {
442 IsEnabled = _stitchingPipeline != null,
443 MaxSizeMB = MaxCacheSizeMB,
444 TileSize = TileSize
445 };
446 }

◆ GetCurrentImage()

SKImage BioGTK.SKSlideRenderer.GetCurrentImage ( )

Get the current image for rendering - called by GLArea during draw Returns the best available image (full quality or preview)

Definition at line 339 of file SKSlideRenderer.cs.

340 {
341 // Return full quality if available, otherwise preview
342 return _currentRenderedImage;
343 }

◆ InvalidateRenderState()

void BioGTK.SKSlideRenderer.InvalidateRenderState ( )

Marks any in-flight render as stale so the next viewport update can replace it without the old result winning the race.

Definition at line 148 of file SKSlideRenderer.cs.

149 {
150 Interlocked.Increment(ref _renderStateVersion);
151 }

◆ RenderCurrentViewSync()

SKImage BioGTK.SKSlideRenderer.RenderCurrentViewSync ( )

Render current viewport synchronously (blocking)

Definition at line 317 of file SKSlideRenderer.cs.

318 {
319 if (!_hasSource || _stitchingPipeline == null)
320 return null;
321
322 return _stitchingPipeline.StitchViewportAsync(
323 _lastOrigin,
324 _lastResolution,
325 _lastCoordinate,
326 _lastWidth,
327 _lastHeight
328 ).Result;
329 }
async Task< SKImage > StitchViewportAsync(PointD origin, double resolution, ZCT coordinate, int viewportWidth=600, int viewportHeight=400)
Stitch tiles for a given viewport asynchronously.

◆ RenderRegion()

SKImage BioGTK.SKSlideRenderer.RenderRegion ( RectangleD region,
int level,
ZCT coordinate,
double resolution,
int pxwidth,
int pxheight )

Render a specific region synchronously (for exports, small areas)

Definition at line 303 of file SKSlideRenderer.cs.

307 {
308 if (!_hasSource || _stitchingPipeline == null)
309 return null;
310
311 return _stitchingPipeline.StitchRegion(region, level, coordinate, resolution, pxwidth, pxheight);
312 }
SKImage StitchRegion(RectangleD region, int level, ZCT coordinate, double resolution, int pxwidth, int pxheight)
Stitch a specific region synchronously (for smaller areas)

◆ SetSource() [1/2]

void BioGTK.SKSlideRenderer.SetSource ( OpenSlideGTK.OpenSlideBase openSlideSource)

Set OpenSlide source and initialize pipeline.

Definition at line 76 of file SKSlideRenderer.cs.

77 {
78 if (openSlideSource == null)
79 throw new ArgumentNullException(nameof(openSlideSource));
80
81 ClearCurrentSource();
82
83 _openSlideSource = openSlideSource;
84 _isOpenSlide = true;
85 _hasSource = true;
86
87 InitializeStitchingPipeline();
88 }

◆ SetSource() [2/2]

void BioGTK.SKSlideRenderer.SetSource ( SlideBase slideSource)

Set BioLib slide source and initialize pipeline.

Definition at line 93 of file SKSlideRenderer.cs.

94 {
95 if (slideSource == null)
96 throw new ArgumentNullException(nameof(slideSource));
97
98 ClearCurrentSource();
99
100 _slideSource = slideSource;
101 _isOpenSlide = false;
102 _hasSource = true;
103
104 InitializeStitchingPipeline();
105 }

◆ UpdateConfiguration()

void BioGTK.SKSlideRenderer.UpdateConfiguration ( int? maxCacheSizeMB = null,
int? tileSize = null,
SKSamplingOptions? sampling = null,
bool? enablePrefetch = null,
bool? enableProgressiveRendering = null )

Update renderer configuration and reinitialize if needed.

Definition at line 462 of file SKSlideRenderer.cs.

468 {
469 bool needsReinit = false;
470
471 if (maxCacheSizeMB.HasValue && maxCacheSizeMB.Value != MaxCacheSizeMB)
472 {
473 MaxCacheSizeMB = maxCacheSizeMB.Value;
474 needsReinit = true;
475 }
476
477 if (tileSize.HasValue && tileSize.Value != TileSize)
478 {
479 TileSize = tileSize.Value;
480 needsReinit = true;
481 }
482
483 // Reinitialize pipeline if structural changes were made
484 if (needsReinit && _hasSource)
485 {
486 InitializeStitchingPipeline();
487 }
488 }

◆ UpdateViewAsync()

async Task BioGTK.SKSlideRenderer.UpdateViewAsync ( PointD origin,
double resolution,
ZCT coordinate,
int width = 600,
int height = 400 )

Update the rendered view asynchronously - main entry point for rendering.

Definition at line 160 of file SKSlideRenderer.cs.

166 {
167 if (_disposed || !_hasSource || _stitchingPipeline == null)
168 return;
169
170 // If a render is already in progress, record the skip and return.
171 // The finally block of the running render will re-schedule via RequestDeferredRender.
172 if (!_renderSemaphore.Wait(0))
173 {
174 LastRenderSkipped = true;
175 return;
176 }
177
178 // We hold the semaphore — clear the skip flag and run.
179 LastRenderSkipped = false;
180 int renderVersion = _renderStateVersion;
181
182 try
183 {
184 IsRendering = true;
185
186 // Store current parameters
187 _lastOrigin = origin;
188 _lastResolution = resolution;
189 _lastCoordinate = coordinate;
190 _lastWidth = width;
191 _lastHeight = height;
192
193 await RenderFullQualityAsync(origin, resolution, coordinate, width, height, renderVersion);
194 }
195 catch (Exception ex)
196 {
197 Console.WriteLine($"Error during view update: {ex.Message}");
198 }
199 finally
200 {
201 IsRendering = false;
202 _renderSemaphore.Release();
203
204 // If a request was skipped while we were running, reschedule so the
205 // view always ends up showing the latest coordinates.
206 if (!_disposed && LastRenderSkipped)
207 Gtk.Application.Invoke((s, a) => App.viewer?.RequestDeferredRender());
208 }
209 }
bool LastRenderSkipped
True if the last UpdateViewAsync call was skipped because a render was already in progress.

Property Documentation

◆ EnablePrefetch

bool BioGTK.SKSlideRenderer.EnablePrefetch = true
getset

Definition at line 42 of file SKSlideRenderer.cs.

42{ get; set; } = true;

◆ HasValidImage

bool BioGTK.SKSlideRenderer.HasValidImage
get

Definition at line 46 of file SKSlideRenderer.cs.

◆ IsRendering

bool BioGTK.SKSlideRenderer.IsRendering
get

Definition at line 45 of file SKSlideRenderer.cs.

45{ get; private set; }

◆ LastRenderSkipped

bool BioGTK.SKSlideRenderer.LastRenderSkipped
get

True if the last UpdateViewAsync call was skipped because a render was already in progress.

Definition at line 20 of file SKSlideRenderer.cs.

20{ get; private set; }

◆ MaxCacheSizeMB

int BioGTK.SKSlideRenderer.MaxCacheSizeMB = 512
getset

Definition at line 39 of file SKSlideRenderer.cs.

39{ get; set; } = 512;

◆ Sampling

SKSamplingOptions BioGTK.SKSlideRenderer.Sampling = SKSamplingOptions.Default
getset

Definition at line 41 of file SKSlideRenderer.cs.

41{ get; set; } = SKSamplingOptions.Default;

◆ TileSize

int BioGTK.SKSlideRenderer.TileSize = 256
getset

Definition at line 40 of file SKSlideRenderer.cs.

40{ get; set; } = 256;

The documentation for this class was generated from the following file: