From d334db8291c312421739712ee9701bb4b1397dc9 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Fri, 25 Oct 2024 19:25:43 +0900 Subject: [PATCH] internal/restorable: performance optimization --- internal/restorable/image.go | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/internal/restorable/image.go b/internal/restorable/image.go index a58f79cb2043..376ae67b1597 100644 --- a/internal/restorable/image.go +++ b/internal/restorable/image.go @@ -337,24 +337,39 @@ func (i *Image) DrawTriangles(srcs [graphics.ShaderSrcImageCount]*Image, vertice return } + // Use the fast path when this package is not enabled. + if !needsRestoration() || !i.needsRestoration() { + var srcImages [graphics.ShaderSrcImageCount]*graphicscommand.Image + for i, src := range srcs { + if src == nil { + continue + } + srcImages[i] = src.image + } + i.makeStale(dstRegion) + i.image.DrawTriangles(srcImages, vertices, indices, blend, dstRegion, srcRegions, shader.shader, uniforms, fillRule) + return + } + // makeStaleIfDependingOnAtRegion is not available here. // This might create cyclic dependency. theImages.makeStaleIfDependingOn(i) // TODO: Add tests to confirm this logic. var srcstale bool - for _, src := range srcs { + var srcImages [graphics.ShaderSrcImageCount]*graphicscommand.Image + for i, src := range srcs { if src == nil { continue } + srcImages[i] = src.image if src.stale || src.imageType == ImageTypeVolatile { srcstale = true - break } } // Even if the image is already stale, call makeStale to extend the stale region. - if srcstale || !needsRestoration() || !i.needsRestoration() { + if srcstale { i.makeStale(dstRegion) } else if i.stale { var overwrite bool @@ -376,14 +391,7 @@ func (i *Image) DrawTriangles(srcs [graphics.ShaderSrcImageCount]*Image, vertice i.appendDrawTrianglesHistory(srcs, vertices, indices, blend, dstRegion, srcRegions, shader, uniforms, fillRule, hint) } - var imgs [graphics.ShaderSrcImageCount]*graphicscommand.Image - for i, src := range srcs { - if src == nil { - continue - } - imgs[i] = src.image - } - i.image.DrawTriangles(imgs, vertices, indices, blend, dstRegion, srcRegions, shader.shader, uniforms, fillRule) + i.image.DrawTriangles(srcImages, vertices, indices, blend, dstRegion, srcRegions, shader.shader, uniforms, fillRule) } func (i *Image) areStaleRegionsIncludedIn(r image.Rectangle) bool {