LCOV - code coverage report
Current view: top level - packages/mask-editor/src - useBrushDrawing.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 84.6 % 136 115
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { nextTick, ref } from 'vue'
       2              : 
       3              : import { CompositionOperation, Tools } from './types'
       4              : import type { Point2D as Point } from '@comfyorg/shared-frontend-utils/geometry'
       5              : import { useMaskEditorStore } from './maskEditorStore'
       6              : 
       7              : import {
       8              :   drawMaskShape,
       9              :   drawRgbShape,
      10              :   resetDirtyRect,
      11              :   updateDirtyRect
      12              : } from './brushDrawingUtils'
      13              : import { getEffectiveBrushSize, getEffectiveHardness } from './brushUtils'
      14              : import { resampleSegment } from './splineUtils'
      15              : import { StrokeProcessor } from './StrokeProcessor'
      16              : import { useBrushAdjustment } from './useBrushAdjustment'
      17              : import { useGPUResources } from './useGPUResources'
      18              : import { useCoordinateTransform } from './useCoordinateTransform'
      19              : 
      20           29 : export function useBrushDrawing(initialSettings?: {
      21              :   useDominantAxis?: boolean
      22              :   brushAdjustmentSpeed?: number
      23              : }) {
      24           29 :   const store = useMaskEditorStore()
      25           29 :   const coordinateTransform = useCoordinateTransform()
      26           29 :   const { startBrushAdjustment, handleBrushAdjustment } =
      27              :     useBrushAdjustment(initialSettings)
      28           29 :   const gpu = useGPUResources()
      29              : 
      30           29 :   const isDrawing = ref(false)
      31           29 :   const isDrawingLine = ref(false)
      32           29 :   const lineStartPoint = ref<Point | null>(null)
      33           29 :   const lineRemainder = ref(0)
      34           29 :   const smoothingLastDrawTime = ref(new Date())
      35           29 :   const initialDraw = ref(true)
      36              : 
      37           29 :   let strokeProcessor: StrokeProcessor | null = null
      38              : 
      39              :   // ── CPU drawing path ─────────────────────────────────────────────────────────
      40              : 
      41          696 :   function drawShape(point: Point, overrideOpacity?: number) {
      42          696 :     const brush = store.brushSettings
      43          696 :     const mask_ctx = store.maskCtx
      44          696 :     const rgb_ctx = store.rgbCtx
      45              : 
      46          696 :     if (!mask_ctx || !rgb_ctx) throw new Error('Canvas contexts are required')
      47              : 
      48          696 :     const brushRadius = brush.size
      49          696 :     const hardness = brush.hardness
      50          696 :     const opacity = overrideOpacity ?? brush.opacity
      51          696 :     const effectiveRadius = getEffectiveBrushSize(brushRadius, hardness)
      52          696 :     const effectiveHardness = getEffectiveHardness(
      53              :       brushRadius,
      54              :       hardness,
      55              :       effectiveRadius
      56              :     )
      57              : 
      58          696 :     const isErasing = mask_ctx.globalCompositeOperation === 'destination-out'
      59          696 :     const currentTool = store.currentTool
      60              :     const isRgbLayer = store.activeLayer === 'rgb'
      61              : 
      62          696 :     if (
      63              :       isRgbLayer &&
      64              :       (currentTool === Tools.Eraser || currentTool === Tools.PaintPen)
      65              :     ) {
      66           38 :       drawRgbShape(
      67              :         rgb_ctx,
      68              :         point,
      69              :         brush.type,
      70              :         effectiveRadius,
      71              :         effectiveHardness,
      72              :         opacity,
      73              :         store.rgbColor
      74              :       )
      75              :     } else {
      76          658 :       drawMaskShape(
      77              :         mask_ctx,
      78              :         point,
      79              :         brush.type,
      80              :         effectiveRadius,
      81              :         effectiveHardness,
      82              :         opacity,
      83              :         isErasing,
      84              :         store.maskColor
      85              :       )
      86              :     }
      87              : 
      88          696 :     gpu.dirtyRect.value = updateDirtyRect(
      89              :       gpu.dirtyRect.value,
      90              :       point.x,
      91              :       point.y,
      92              :       effectiveRadius
      93              :     )
      94              :   }
      95              : 
      96              :   // ── Drawing orchestration ────────────────────────────────────────────────────
      97              : 
      98          209 :   function initShape(compositionOperation: CompositionOperation) {
      99          209 :     const blendMode = store.maskBlendMode
     100          209 :     const mask_ctx = store.maskCtx
     101          209 :     const rgb_ctx = store.rgbCtx
     102              : 
     103          209 :     if (!mask_ctx || !rgb_ctx) throw new Error('Canvas contexts are required')
     104              : 
     105          209 :     mask_ctx.beginPath()
     106          209 :     rgb_ctx.beginPath()
     107              : 
     108          209 :     if (compositionOperation === CompositionOperation.SourceOver) {
     109          198 :       mask_ctx.fillStyle = blendMode
     110          198 :       mask_ctx.globalCompositeOperation = CompositionOperation.SourceOver
     111          198 :       rgb_ctx.globalCompositeOperation = CompositionOperation.SourceOver
     112              :     } else {
     113           11 :       mask_ctx.globalCompositeOperation = CompositionOperation.DestinationOut
     114           11 :       rgb_ctx.globalCompositeOperation = CompositionOperation.DestinationOut
     115              :     }
     116              :   }
     117              : 
     118          209 :   async function drawWithBetterSmoothing(point: Point): Promise<void> {
     119          209 :     if (!strokeProcessor) return
     120              : 
     121          209 :     const newPoints = strokeProcessor.addPoint(point)
     122              : 
     123          209 :     if (newPoints.length === 0) {
     124           38 :       if (gpu.hasRenderer.value && initialDraw.value) {
     125              :         /* c8 ignore next */
     126              :         gpu.gpuRender([], true)
     127              :       }
     128           38 :       return
     129              :     }
     130              : 
     131          209 :     if (gpu.hasRenderer.value) {
     132              :       /* c8 ignore next */
     133              :       gpu.gpuRender(newPoints, true)
     134              :     } else {
     135          623 :       for (const p of newPoints) drawShape(p)
     136              :     }
     137              : 
     138          171 :     initialDraw.value = false
     139              :   }
     140              : 
     141            0 :   async function drawLine(
     142              :     p1: Point,
     143              :     p2: Point,
     144              :     compositionOp: CompositionOperation,
     145              :     spacing: number
     146              :   ): Promise<void> {
     147            0 :     const { points, remainder } = resampleSegment(
     148              :       [p1, p2],
     149              :       spacing,
     150              :       lineRemainder.value
     151              :     )
     152            0 :     lineRemainder.value = remainder
     153            0 :     initShape(compositionOp)
     154              : 
     155            0 :     if (gpu.hasRenderer.value) {
     156              :       /* c8 ignore next */
     157              :       gpu.gpuRender(points)
     158              :     } else {
     159            0 :       for (const point of points) drawShape(point, 1)
     160              :     }
     161              :   }
     162              : 
     163           19 :   async function startDrawing(event: PointerEvent): Promise<void> {
     164           19 :     isDrawing.value = true
     165           19 :     gpu.dirtyRect.value = resetDirtyRect()
     166              : 
     167           19 :     try {
     168           19 :       gpu.prepareStroke()
     169              : 
     170           19 :       const currentTool = store.currentTool
     171           19 :       const coords = { x: event.offsetX, y: event.offsetY }
     172           19 :       const coords_canvas = coordinateTransform.screenToCanvas(coords)
     173              : 
     174           19 :       const compositionOp =
     175              :         currentTool === 'eraser' || event.buttons === 2
     176              :           ? CompositionOperation.DestinationOut
     177              :           : CompositionOperation.SourceOver
     178              : 
     179           19 :       const stepPercentage =
     180              :         Math.pow(100, store.brushSettings.stepSize / 100) / 100
     181           19 :       const targetSpacing = Math.max(
     182              :         1.0,
     183              :         store.brushSettings.size * stepPercentage
     184              :       )
     185              : 
     186           19 :       if (event.shiftKey && lineStartPoint.value) {
     187            0 :         isDrawingLine.value = true
     188            0 :         await drawLine(
     189              :           lineStartPoint.value,
     190              :           coords_canvas,
     191              :           compositionOp,
     192              :           targetSpacing
     193              :         )
     194              :       } else {
     195           19 :         isDrawingLine.value = false
     196           19 :         initShape(compositionOp)
     197           19 :         lineRemainder.value = targetSpacing
     198              :       }
     199              : 
     200           19 :       lineStartPoint.value = coords_canvas
     201              : 
     202           19 :       if (gpu.hasRenderer.value && gpu.previewCanvas.value) {
     203              :         /* c8 ignore start */
     204              :         const isRgb = store.activeLayer === 'rgb'
     205              :         if (isRgb && store.rgbCanvas) {
     206              :           store.rgbCanvas.style.opacity = '0'
     207              :           gpu.previewCanvas.value.style.opacity = '1'
     208              :         } else if (!isRgb && store.maskCanvas) {
     209              :           store.maskCanvas.style.opacity = '0'
     210              :           gpu.previewCanvas.value.style.opacity = String(store.maskOpacity)
     211              :         }
     212              :         /* c8 ignore stop */
     213              :       }
     214              : 
     215           19 :       strokeProcessor = new StrokeProcessor(targetSpacing)
     216           19 :       await drawWithBetterSmoothing(coords_canvas)
     217           19 :       smoothingLastDrawTime.value = new Date()
     218              :     } catch (error) {
     219              :       /* c8 ignore start */
     220              :       const isRgb = store.activeLayer === 'rgb'
     221              :       if (isRgb && store.rgbCanvas) store.rgbCanvas.style.opacity = '1'
     222              :       else if (!isRgb && store.maskCanvas)
     223              :         store.maskCanvas.style.opacity = String(store.maskOpacity)
     224              :       if (gpu.previewCanvas.value) gpu.previewCanvas.value.style.opacity = '1'
     225              :       gpu.clearPreview()
     226              :       /* c8 ignore stop */
     227            0 :       console.error('[useBrushDrawing] Failed to start drawing:', error)
     228            0 :       isDrawing.value = false
     229            0 :       isDrawingLine.value = false
     230              :     }
     231              :   }
     232              : 
     233          190 :   async function handleDrawing(event: PointerEvent): Promise<void> {
     234          190 :     const diff = performance.now() - smoothingLastDrawTime.value.getTime()
     235          190 :     const coords = { x: event.offsetX, y: event.offsetY }
     236          190 :     const coords_canvas = coordinateTransform.screenToCanvas(coords)
     237          190 :     const currentTool = store.currentTool
     238              : 
     239          190 :     if (diff > 20 && !isDrawing.value) {
     240            0 :       requestAnimationFrame(async () => {
     241            0 :         if (!isDrawing.value) return
     242            0 :         try {
     243            0 :           initShape(CompositionOperation.SourceOver)
     244            0 :           if (gpu.hasRenderer.value) {
     245            0 :             await gpu.gpuDrawPoint(coords_canvas)
     246              :           } else {
     247            0 :             drawShape(coords_canvas)
     248              :           }
     249              :         } catch (error) {
     250            0 :           console.error('[useBrushDrawing] Drawing error:', error)
     251              :         }
     252              :       })
     253              :     } else {
     254          190 :       requestAnimationFrame(async () => {
     255          190 :         if (!isDrawing.value) return
     256          190 :         try {
     257          190 :           if (currentTool === 'eraser' || event.buttons === 2) {
     258           10 :             initShape(CompositionOperation.DestinationOut)
     259              :           } else {
     260          180 :             initShape(CompositionOperation.SourceOver)
     261              :           }
     262          190 :           await drawWithBetterSmoothing(coords_canvas)
     263              :         } catch (error) {
     264            0 :           console.error('[useBrushDrawing] Drawing error:', error)
     265              :         }
     266              :       })
     267              :     }
     268              : 
     269          190 :     smoothingLastDrawTime.value = new Date()
     270              :   }
     271              : 
     272           20 :   async function drawEnd(event: PointerEvent): Promise<void> {
     273           20 :     const coords = { x: event.offsetX, y: event.offsetY }
     274           20 :     const coords_canvas = coordinateTransform.screenToCanvas(coords)
     275              : 
     276           20 :     if (!isDrawing.value) return
     277              : 
     278           19 :     isDrawing.value = false
     279           19 :     lineStartPoint.value = coords_canvas
     280           19 :     initialDraw.value = true
     281              : 
     282              :     // Flush remaining stroke points
     283           19 :     if (strokeProcessor) {
     284           19 :       const finalPoints = strokeProcessor.endStroke()
     285           19 :       if (finalPoints.length > 0) {
     286           19 :         if (gpu.hasRenderer.value) {
     287              :           /* c8 ignore next */
     288              :           gpu.gpuRender(finalPoints, true)
     289              :         } else {
     290           73 :           for (const p of finalPoints) drawShape(p)
     291              :         }
     292              :       }
     293           19 :       strokeProcessor = null
     294              :     }
     295              : 
     296              :     // Composite GPU stroke into the main texture
     297           19 :     const isRgb = store.activeLayer === 'rgb'
     298           20 :     const isErasing =
     299              :       store.currentTool === 'eraser' ||
     300              :       store.maskCtx?.globalCompositeOperation === 'destination-out'
     301           20 :     gpu.compositeStroke(isRgb, isErasing)
     302              : 
     303              :     // Read back GPU result
     304           20 :     let maskData: ImageData | undefined
     305           20 :     let rgbData: ImageData | undefined
     306           20 :     if (gpu.hasRenderer.value) {
     307              :       /* c8 ignore start */
     308              :       try {
     309              :         const result = await gpu.copyGpuToCanvas()
     310              :         maskData = result.maskData
     311              :         rgbData = result.rgbData
     312              :       } catch (error) {
     313              :         console.warn('GPU readback failed, falling back to CPU:', error)
     314              :       }
     315              :       /* c8 ignore stop */
     316              :     }
     317              : 
     318              :     // Save to history
     319           19 :     gpu.isSavingHistory.value = true
     320           19 :     try {
     321           19 :       store.canvasHistory.saveState(maskData, rgbData)
     322           19 :       await nextTick()
     323              :     } finally {
     324           19 :       gpu.isSavingHistory.value = false
     325              :     }
     326              : 
     327           19 :     gpu.clearPreview()
     328              : 
     329              :     // Restore canvas visibility
     330           20 :     if (isRgb && store.rgbCanvas) {
     331            1 :       store.rgbCanvas.style.opacity = '1'
     332           18 :     } else if (!isRgb && store.maskCanvas) {
     333           18 :       store.maskCanvas.style.opacity = String(store.maskOpacity)
     334              :     }
     335           19 :     if (gpu.previewCanvas.value) {
     336            0 :       gpu.previewCanvas.value.style.opacity = '1'
     337              :     }
     338              :   }
     339              : 
     340           29 :   return {
     341              :     startDrawing,
     342              :     handleDrawing,
     343              :     drawEnd,
     344              :     startBrushAdjustment,
     345              :     handleBrushAdjustment,
     346              :     destroy: gpu.destroy,
     347              :     initGPUResources: gpu.initGPUResources,
     348              :     initPreviewCanvas: gpu.initPreviewCanvas,
     349              :     clearGPU: gpu.clearGPU
     350              :   }
     351              : }
        

Generated by: LCOV version 2.0-1