All files / core/src/RenderingEngine WebGLContextPool.ts

68.18% Statements 15/22
75% Branches 3/4
62.5% Functions 5/8
66.66% Lines 14/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96                  428x 428x 428x             428x   2996x 2996x 2996x   2996x 2996x                         599682x 599682x                           702x                 598980x               488x                                                  
import { vtkOffscreenMultiRenderWindow } from './vtkClasses';
import type { VtkOffscreenMultiRenderWindow } from '../types';
 
/**
 * Manages a pool of WebGL contexts for parallel rendering.
 * Enables us distribute viewports across multiple contexts
 * for improved performance.
 */
class WebGLContextPool {
  private contexts: VtkOffscreenMultiRenderWindow[] = [];
  private offScreenCanvasContainers: HTMLDivElement[] = [];
  private viewportToContext: Map<string, number> = new Map();
 
  /**
   * Creates a pool with the specified number of WebGL contexts
   * @param count - Number of contexts to create
   */
  constructor(count: number) {
    for (let i = 0; i < count; i++) {
      const offscreenMultiRenderWindow =
        vtkOffscreenMultiRenderWindow.newInstance();
      const container = document.createElement('div');
      offscreenMultiRenderWindow.setContainer(container);
 
      this.contexts.push(offscreenMultiRenderWindow);
      this.offScreenCanvasContainers.push(container);
    }
  }
 
  /**
   * Gets the context and container at the specified index
   * @param index - Context index
   * @returns Context and container, or null if index is invalid
   */
  getContextByIndex(index: number): {
    context: VtkOffscreenMultiRenderWindow;
    container: HTMLDivElement;
  } | null {
    Eif (index >= 0 && index < this.contexts.length) {
      return {
        context: this.contexts[index],
        container: this.offScreenCanvasContainers[index],
      };
    }
    return null;
  }
 
  /**
   * Associates a viewport with a specific context index
   * @param viewportId - ID of the viewport
   * @param contextIndex - Index of the context to assign
   */
  assignViewportToContext(viewportId: string, contextIndex: number): void {
    this.viewportToContext.set(viewportId, contextIndex);
  }
 
  /**
   * Gets the context index assigned to a viewport
   * @param viewportId - ID of the viewport
   * @returns Context index, or undefined if not assigned
   */
  getContextIndexForViewport(viewportId: string): number | undefined {
    return this.viewportToContext.get(viewportId);
  }
 
  /**
   * Gets all contexts in the pool
   * @returns Array of all contexts
   */
  getAllContexts(): VtkOffscreenMultiRenderWindow[] {
    return this.contexts;
  }
 
  /**
   * Gets the number of contexts in the pool
   * @returns Number of contexts
   */
  getContextCount(): number {
    return this.contexts.length;
  }
 
  /**
   * Cleans up all contexts and releases resources
   */
  destroy(): void {
    this.contexts.forEach((context: VtkOffscreenMultiRenderWindow) => {
      context.delete();
    });
    this.contexts = [];
    this.offScreenCanvasContainers = [];
    this.viewportToContext.clear();
  }
}
 
export default WebGLContextPool;