import { Canvas } from "skia-canvas"; /** * Configuration interface for Canvas Pool settings. */ export interface CanvasPoolConfig { /** Maximum number of canvas instances to keep in the pool */ maxPoolSize?: number; /** Initial number of canvas instances to create */ initialPoolSize?: number; /** Enable debug logging for pool operations */ debug?: boolean; } /** * Metrics interface for Canvas Pool performance monitoring. */ export interface CanvasPoolMetrics { /** Current number of available canvas instances in pool */ available: number; /** Current number of canvas instances in use */ inUse: number; /** Total number of canvas instances created */ totalCreated: number; /** Total number of acquire operations */ totalAcquires: number; /** Total number of release operations */ totalReleases: number; /** Number of times pool was exhausted and new canvas was created */ poolMisses: number; /** Number of canvas instances evicted due to pool size limits */ evictions: number; } /** * Canvas Pool implementation for reusing skia-canvas Canvas instances. * * This pool manages reusable Canvas instances to reduce memory allocation overhead * and improve performance in CAPTCHA generation. Uses LRU eviction strategy when * the pool reaches capacity. * * @example Basic usage * ```typescript * const pool = new CanvasPool({ maxPoolSize: 10 }); * * const canvas = pool.acquire(300, 100); * // Use canvas for drawing operations * pool.release(canvas); * ``` * * @example With custom configuration * ```typescript * const pool = new CanvasPool({ * maxPoolSize: 20, * initialPoolSize: 5, * debug: true * }); * ``` */ export declare class CanvasPool { private pool; private readonly maxPoolSize; private readonly debug; private metrics; /** * Creates a new Canvas Pool instance. * * @param config - Pool configuration options */ constructor(config?: CanvasPoolConfig); /** * Pre-creates canvas instances for the pool. * * @param count - Number of canvas instances to create */ private preCreateCanvases; /** * Creates a new Canvas instance with error handling. * * @param width - Canvas width in pixels * @param height - Canvas height in pixels * @returns New Canvas instance */ private createCanvas; /** * Resets canvas state to prepare it for reuse. * * @param canvas - Canvas to reset * @param width - Target width * @param height - Target height */ private resetCanvas; /** * Acquires a canvas from the pool or creates a new one. * * @param width - Required canvas width in pixels * @param height - Required canvas height in pixels * @returns Canvas instance ready for use * * @example * ```typescript * const canvas = pool.acquire(400, 150); * const ctx = canvas.getContext('2d'); * // Draw on canvas... * pool.release(canvas); * ``` */ acquire(width: number, height: number): Canvas; /** * Releases a canvas back to the pool for reuse. * * @param canvas - Canvas to release * * @example * ```typescript * const canvas = pool.acquire(300, 100); * // Use canvas... * pool.release(canvas); * ``` */ release(canvas: Canvas): void; /** * Performs LRU eviction if the pool exceeds capacity. */ private evictIfNeeded; /** * Clears all available canvases from the pool. * In-use canvases are not affected. */ clear(): void; /** * Disposes of all canvases and clears the pool completely. * This should be called when shutting down the application. */ dispose(): void; /** * Gets current pool metrics for monitoring and debugging. * * @returns Current pool metrics including usage statistics * * @example * ```typescript * const metrics = pool.getMetrics(); * console.log(`Pool usage: ${metrics.inUse}/${metrics.available + metrics.inUse}`); * console.log(`Cache hit rate: ${(metrics.totalAcquires - metrics.poolMisses) / metrics.totalAcquires * 100}%`); * ``` */ getMetrics(): CanvasPoolMetrics; }