Compare commits

...

4 Commits

Author SHA1 Message Date
Hajime Hoshi
d334db8291 internal/restorable: performance optimization 2024-10-25 19:30:38 +09:00
Hajime Hoshi
0da99e2c37 text/v2: add comments to CacheGlyphs 2024-10-25 17:41:24 +09:00
Hajime Hoshi
9449e0a8a6 Revert "text/v2: remove faceWithCache's mutex"
This reverts commit d5c6fd174f.

Reason: CacheGlyphs and Measure should be concurrent safe.
2024-10-25 14:52:02 +09:00
Hajime Hoshi
d5c6fd174f text/v2: remove faceWithCache's mutex
faceWithCache is not exported, and the functions using faceWithCache
are not concurrent safe. Thus, it doesn't make sense to protect
faceWithCache by mutex. Rather, this affects performance.
2024-10-25 13:58:18 +09:00
2 changed files with 22 additions and 11 deletions

View File

@ -337,24 +337,39 @@ func (i *Image) DrawTriangles(srcs [graphics.ShaderSrcImageCount]*Image, vertice
return 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. // makeStaleIfDependingOnAtRegion is not available here.
// This might create cyclic dependency. // This might create cyclic dependency.
theImages.makeStaleIfDependingOn(i) theImages.makeStaleIfDependingOn(i)
// TODO: Add tests to confirm this logic. // TODO: Add tests to confirm this logic.
var srcstale bool var srcstale bool
for _, src := range srcs { var srcImages [graphics.ShaderSrcImageCount]*graphicscommand.Image
for i, src := range srcs {
if src == nil { if src == nil {
continue continue
} }
srcImages[i] = src.image
if src.stale || src.imageType == ImageTypeVolatile { if src.stale || src.imageType == ImageTypeVolatile {
srcstale = true srcstale = true
break
} }
} }
// Even if the image is already stale, call makeStale to extend the stale region. // Even if the image is already stale, call makeStale to extend the stale region.
if srcstale || !needsRestoration() || !i.needsRestoration() { if srcstale {
i.makeStale(dstRegion) i.makeStale(dstRegion)
} else if i.stale { } else if i.stale {
var overwrite bool 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) i.appendDrawTrianglesHistory(srcs, vertices, indices, blend, dstRegion, srcRegions, shader, uniforms, fillRule, hint)
} }
var imgs [graphics.ShaderSrcImageCount]*graphicscommand.Image i.image.DrawTriangles(srcImages, vertices, indices, blend, dstRegion, srcRegions, shader.shader, uniforms, fillRule)
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)
} }
func (i *Image) areStaleRegionsIncludedIn(r image.Rectangle) bool { func (i *Image) areStaleRegionsIncludedIn(r image.Rectangle) bool {

View File

@ -255,6 +255,9 @@ func Measure(text string, face Face, lineSpacingInPixels float64) (width, height
// Draw and AppendGlyphs automatically create and cache necessary glyphs, so usually you don't have to call CacheGlyphs explicitly. // Draw and AppendGlyphs automatically create and cache necessary glyphs, so usually you don't have to call CacheGlyphs explicitly.
// If you really care about the performance, CacheGlyphs might be useful. // If you really care about the performance, CacheGlyphs might be useful.
// //
// CacheGlyphs is pretty heavy since it creates all the possible variations of glyphs.
// Call CacheGlyphs only when you really need it.
//
// CacheGlyphs is concurrent-safe. // CacheGlyphs is concurrent-safe.
func CacheGlyphs(text string, face Face) { func CacheGlyphs(text string, face Face) {
var x, y float64 var x, y float64