Debian Codegraph @

src/com/github/weisj/jsvg/util/CachedSurfaceSupplier.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2024-2025 Jannis Weis
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
7 * associated documentation files (the "Software"), to deal in the Software without restriction,
8 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
9 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all copies or
13 * substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
16 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 *
21 */
22 package com.github.weisj.jsvg.util;
23
24 import java.awt.geom.AffineTransform;
25 import java.awt.image.BufferedImage;
26 import java.lang.ref.SoftReference;
27 import java.util.ArrayList;
28 import java.util.Iterator;
29 import java.util.List;
30
31 import org.jetbrains.annotations.NotNull;
32 import org.jetbrains.annotations.Nullable;
33
34 import com.github.weisj.jsvg.renderer.RenderContext;
35 import com.github.weisj.jsvg.renderer.SVGRenderingHints;
36 import com.github.weisj.jsvg.renderer.output.Output;
37
38 public class CachedSurfaceSupplier {
39
40 private final @NotNull BlittableImage.BufferSurfaceSupplier surfaceSupplier;
41 private final @NotNull ThreadLocal<Cache> cache = ThreadLocal.withInitial(Cache::new);
42
43 public CachedSurfaceSupplier(BlittableImage.@NotNull BufferSurfaceSupplier surfaceSupplier) {
44 this.surfaceSupplier = surfaceSupplier;
45 }
46
47 public boolean useCache(@NotNull Output output, @NotNull RenderContext renderContext) {
48 return renderContext.platformSupport().isLongLived()
49 && output.renderingHint(
50 SVGRenderingHints.KEY_CACHE_OFFSCREEN_IMAGE) != SVGRenderingHints.VALUE_NO_CACHE;
51 }
52
53 @NotNull
54 public BlittableImage.BufferSurfaceSupplier surfaceSupplier(boolean useCache) {
55 if (!useCache) {
56 return surfaceSupplier;
57 }
58 return this::createBufferSurface;
59 }
60
61 public @NotNull BufferedImage createBufferSurface(@Nullable AffineTransform at, double width, double height) {
62 if (at != null) {
63 throw new UnsupportedOperationException("CachedSurfaceSupplier does not support transformations");
64 }
65 Cache c = cache.get();
66 Iterator<CachedImage> it = c.images.iterator();
67 while (it.hasNext()) {
68 CachedImage cachedImage = it.next();
69 BufferedImage img = cachedImage.image.get();
70 if (img == null) {
71 it.remove();
72 continue;
73 }
74 if (!cachedImage.inUse && img.getWidth() >= width && img.getHeight() >= height) {
75 cachedImage.inUse = true;
76 c.lastIssuedCleaner = new ResourceCleaner(null, cachedImage::free);
77 return img.getSubimage(0, 0, (int) width, (int) height);
78 }
79 }
80 BufferedImage image = surfaceSupplier.createBufferSurface(null, width, height);
81 CachedImage cachedImage = new CachedImage(image);
82 c.images.add(cachedImage);
83 c.lastIssuedCleaner = new ResourceCleaner(null, cachedImage::free);
84 return image;
85 }
86
87 public @Nullable ResourceCleaner resourceCleaner(Object owner, boolean useCache) {
88 if (!useCache) return null;
89 ResourceCleaner cleaner = cache.get().lastIssuedCleaner;
90 if (cleaner != null) {
91 return cleaner.withOwner(owner);
92 }
93 return null;
94 }
95
96 public static class ResourceCleaner {
97 private final @Nullable Object owner;
98 private @Nullable Runnable cleaner;
99
100 public ResourceCleaner(@Nullable Object owner, @Nullable Runnable cleaner) {
101 this.owner = owner;
102 this.cleaner = cleaner;
103 }
104
105 public void clean(Object owner) {
106 if (this.owner == owner) {
107 if (cleaner == null) {
108 throw new IllegalStateException("Resource already cleaned");
109 }
110 cleaner.run();
111 cleaner = null;
112 }
113 }
114
115 private @NotNull ResourceCleaner withOwner(Object owner) {
116 return new ResourceCleaner(owner, cleaner);
117 }
118 }
119
120 private static class Cache {
121 private final List<CachedImage> images = new ArrayList<>();
122 private @Nullable ResourceCleaner lastIssuedCleaner;
123 }
124
125 private static class CachedImage {
126 private final @NotNull SoftReference<BufferedImage> image;
127 private boolean inUse = true;
128
129 private CachedImage(@NotNull BufferedImage image) {
130 this.image = new SoftReference<>(image);
131 }
132
133 private void free() {
134 this.inUse = false;
135 }
136 }
137 }