source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/ElemStyles.java@ 7081

Last change on this file since 7081 was 7059, checked in by bastiK, 10 years ago

mapcss: use multiple threads for style creation (see #9691)

significant speed-up on multi core processors
(factor ~3.5 on my 4 core Intel Core i7)

This makes a lot of code run parallel which wasn't written with
multithreading in mind, so keep fingers crossed. :)

  • Property svn:eol-style set to native
File size: 18.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint;
3
4import java.awt.Color;
5import java.util.ArrayList;
6import java.util.Collection;
7import java.util.Collections;
8import java.util.Iterator;
9import java.util.List;
10import java.util.Map.Entry;
11
12import org.openstreetmap.josm.data.osm.Node;
13import org.openstreetmap.josm.data.osm.OsmPrimitive;
14import org.openstreetmap.josm.data.osm.Relation;
15import org.openstreetmap.josm.data.osm.Way;
16import org.openstreetmap.josm.data.osm.visitor.paint.relations.Multipolygon;
17import org.openstreetmap.josm.data.osm.visitor.paint.relations.MultipolygonCache;
18import org.openstreetmap.josm.gui.NavigatableComponent;
19import org.openstreetmap.josm.gui.mappaint.StyleCache.StyleList;
20import org.openstreetmap.josm.tools.Pair;
21import org.openstreetmap.josm.tools.Utils;
22
23public class ElemStyles {
24 private List<StyleSource> styleSources;
25 private boolean drawMultipolygon;
26
27 private int cacheIdx = 1;
28
29 private boolean defaultNodes, defaultLines;
30 private int defaultNodesIdx, defaultLinesIdx;
31
32 /**
33 * Constructs a new {@code ElemStyles}.
34 */
35 public ElemStyles() {
36 styleSources = new ArrayList<>();
37 }
38
39 public void clearCached() {
40 cacheIdx++;
41 }
42
43 public List<StyleSource> getStyleSources() {
44 return Collections.<StyleSource>unmodifiableList(styleSources);
45 }
46
47 /**
48 * Create the list of styles for one primitive.
49 *
50 * @param osm the primitive
51 * @param scale the scale (in meters per 100 pixel)
52 * @param nc display component
53 * @return list of styles
54 */
55 public StyleList get(OsmPrimitive osm, double scale, NavigatableComponent nc) {
56 return getStyleCacheWithRange(osm, scale, nc).a;
57 }
58
59 /**
60 * Create the list of styles and its valid scale range for one primitive.
61 *
62 * Automatically adds default styles in case no proper style was found.
63 * Uses the cache, if possible, and saves the results to the cache.
64 */
65 public Pair<StyleList, Range> getStyleCacheWithRange(OsmPrimitive osm, double scale, NavigatableComponent nc) {
66 if (osm.mappaintStyle == null || osm.mappaintCacheIdx != cacheIdx || scale <= 0) {
67 osm.mappaintStyle = StyleCache.EMPTY_STYLECACHE;
68 } else {
69 Pair<StyleList, Range> lst = osm.mappaintStyle.getWithRange(scale);
70 if (lst.a != null)
71 return lst;
72 }
73 Pair<StyleList, Range> p = getImpl(osm, scale, nc);
74 if (osm instanceof Node && isDefaultNodes()) {
75 if (p.a.isEmpty()) {
76 if (TextElement.AUTO_LABEL_COMPOSITION_STRATEGY.compose(osm) != null) {
77 p.a = NodeElemStyle.DEFAULT_NODE_STYLELIST_TEXT;
78 } else {
79 p.a = NodeElemStyle.DEFAULT_NODE_STYLELIST;
80 }
81 } else {
82 boolean hasNonModifier = false;
83 boolean hasText = false;
84 for (ElemStyle s : p.a) {
85 if (s instanceof BoxTextElemStyle) {
86 hasText = true;
87 } else {
88 if (!s.isModifier) {
89 hasNonModifier = true;
90 }
91 }
92 }
93 if (!hasNonModifier) {
94 p.a = new StyleList(p.a, NodeElemStyle.SIMPLE_NODE_ELEMSTYLE);
95 if (!hasText) {
96 if (TextElement.AUTO_LABEL_COMPOSITION_STRATEGY.compose(osm) != null) {
97 p.a = new StyleList(p.a, BoxTextElemStyle.SIMPLE_NODE_TEXT_ELEMSTYLE);
98 }
99 }
100 }
101 }
102 } else if (osm instanceof Way && isDefaultLines()) {
103 boolean hasProperLineStyle = false;
104 for (ElemStyle s : p.a) {
105 if (s.isProperLineStyle()) {
106 hasProperLineStyle = true;
107 break;
108 }
109 }
110 if (!hasProperLineStyle) {
111 AreaElemStyle area = Utils.find(p.a, AreaElemStyle.class);
112 LineElemStyle line = area == null ? LineElemStyle.UNTAGGED_WAY : LineElemStyle.createSimpleLineStyle(area.color, true);
113 p.a = new StyleList(p.a, line);
114 }
115 }
116 StyleCache style = osm.mappaintStyle != null ? osm.mappaintStyle : StyleCache.EMPTY_STYLECACHE;
117 try {
118 osm.mappaintStyle = style.put(p.a, p.b);
119 } catch (StyleCache.RangeViolatedError e) {
120 AssertionError error = new AssertionError("Range violated. object: " + osm.getPrimitiveId() + ", current style: "+osm.mappaintStyle
121 + ", scale: " + scale + ", new stylelist: " + p.a + ", new range: " + p.b);
122 error.initCause(e);
123 throw error;
124 }
125 osm.mappaintCacheIdx = cacheIdx;
126 return p;
127 }
128
129 /**
130 * Create the list of styles and its valid scale range for one primitive.
131 *
132 * This method does multipolygon handling.
133 *
134 *
135 * There are different tagging styles for multipolygons, that have to be respected:
136 * - tags on the relation
137 * - tags on the outer way
138 * - tags on both, the outer and the inner way (very old style)
139 *
140 * If the primitive is a way, look for multipolygon parents. In case it
141 * is indeed member of some multipolygon as role "outer", all area styles
142 * are removed. (They apply to the multipolygon area.)
143 * Outer ways can have their own independent line styles, e.g. a road as
144 * boundary of a forest. Otherwise, in case, the way does not have an
145 * independent line style, take a line style from the multipolygon.
146 * If the multipolygon does not have a line style either, at least create a
147 * default line style from the color of the area.
148 *
149 * Now consider the case that the way is not an outer way of any multipolygon,
150 * but is member of a multipolygon as "inner".
151 * First, the style list is regenerated, considering only tags of this way
152 * minus the tags of outer way of the multipolygon (to care for the "very
153 * old style").
154 * Then check, if the way describes something in its own right. (linear feature
155 * or area) If not, add a default line style from the area color of the multipolygon.
156 *
157 */
158 private Pair<StyleList, Range> getImpl(OsmPrimitive osm, double scale, NavigatableComponent nc) {
159 if (osm instanceof Node)
160 return generateStyles(osm, scale, null, false);
161 else if (osm instanceof Way)
162 {
163 Pair<StyleList, Range> p = generateStyles(osm, scale, null, false);
164
165 boolean isOuterWayOfSomeMP = false;
166 Color wayColor = null;
167
168 for (OsmPrimitive referrer : osm.getReferrers()) {
169 Relation r = (Relation) referrer;
170 if (!drawMultipolygon || !r.isMultipolygon() || !r.isUsable()) {
171 continue;
172 }
173 Multipolygon multipolygon = MultipolygonCache.getInstance().get(nc, r);
174
175 if (multipolygon.getOuterWays().contains(osm)) {
176 boolean hasIndependentLineStyle = false;
177 if (!isOuterWayOfSomeMP) { // do this only one time
178 List<ElemStyle> tmp = new ArrayList<>(p.a.size());
179 for (ElemStyle s : p.a) {
180 if (s instanceof AreaElemStyle) {
181 wayColor = ((AreaElemStyle) s).color;
182 } else {
183 tmp.add(s);
184 if (s.isProperLineStyle()) {
185 hasIndependentLineStyle = true;
186 }
187 }
188 }
189 p.a = new StyleList(tmp);
190 isOuterWayOfSomeMP = true;
191 }
192
193 if (!hasIndependentLineStyle) {
194 Pair<StyleList, Range> mpElemStyles;
195 synchronized(r) {
196 mpElemStyles = getStyleCacheWithRange(r, scale, nc);
197 }
198 ElemStyle mpLine = null;
199 for (ElemStyle s : mpElemStyles.a) {
200 if (s.isProperLineStyle()) {
201 mpLine = s;
202 break;
203 }
204 }
205 p.b = Range.cut(p.b, mpElemStyles.b);
206 if (mpLine != null) {
207 p.a = new StyleList(p.a, mpLine);
208 break;
209 } else if (wayColor == null && isDefaultLines()) {
210 AreaElemStyle mpArea = Utils.find(mpElemStyles.a, AreaElemStyle.class);
211 if (mpArea != null) {
212 wayColor = mpArea.color;
213 }
214 }
215 }
216 }
217 }
218 if (isOuterWayOfSomeMP) {
219 if (isDefaultLines()) {
220 boolean hasLineStyle = false;
221 for (ElemStyle s : p.a) {
222 if (s.isProperLineStyle()) {
223 hasLineStyle = true;
224 break;
225 }
226 }
227 if (!hasLineStyle) {
228 p.a = new StyleList(p.a, LineElemStyle.createSimpleLineStyle(wayColor, true));
229 }
230 }
231 return p;
232 }
233
234 if (!isDefaultLines()) return p;
235
236 for (OsmPrimitive referrer : osm.getReferrers()) {
237 Relation ref = (Relation) referrer;
238 if (!drawMultipolygon || !ref.isMultipolygon() || !ref.isUsable()) {
239 continue;
240 }
241 final Multipolygon multipolygon = MultipolygonCache.getInstance().get(nc, ref);
242
243 if (multipolygon.getInnerWays().contains(osm)) {
244 Iterator<Way> it = multipolygon.getOuterWays().iterator();
245 p = generateStyles(osm, scale, it.hasNext() ? it.next() : null, false);
246 boolean hasIndependentElemStyle = false;
247 for (ElemStyle s : p.a) {
248 if (s.isProperLineStyle() || s instanceof AreaElemStyle) {
249 hasIndependentElemStyle = true;
250 break;
251 }
252 }
253 if (!hasIndependentElemStyle && !multipolygon.getOuterWays().isEmpty()) {
254 Color mpColor = null;
255 StyleList mpElemStyles = null;
256 synchronized (ref) {
257 mpElemStyles = get(ref, scale, nc);
258 }
259 for (ElemStyle mpS : mpElemStyles) {
260 if (mpS instanceof AreaElemStyle) {
261 mpColor = ((AreaElemStyle) mpS).color;
262 break;
263 }
264 }
265 p.a = new StyleList(p.a, LineElemStyle.createSimpleLineStyle(mpColor, true));
266 }
267 return p;
268 }
269 }
270 return p;
271 }
272 else if (osm instanceof Relation)
273 {
274 Pair<StyleList, Range> p = generateStyles(osm, scale, null, true);
275 if (drawMultipolygon && ((Relation)osm).isMultipolygon()) {
276 if (!Utils.exists(p.a, AreaElemStyle.class)) {
277 // look at outer ways to find area style
278 Multipolygon multipolygon = MultipolygonCache.getInstance().get(nc, (Relation) osm);
279 for (Way w : multipolygon.getOuterWays()) {
280 Pair<StyleList, Range> wayStyles = generateStyles(w, scale, null, false);
281 p.b = Range.cut(p.b, wayStyles.b);
282 ElemStyle area = Utils.find(wayStyles.a, AreaElemStyle.class);
283 if (area != null) {
284 p.a = new StyleList(p.a, area);
285 break;
286 }
287 }
288 }
289 }
290 return p;
291 }
292 return null;
293 }
294
295 /**
296 * Create the list of styles and its valid scale range for one primitive.
297 *
298 * Loops over the list of style sources, to generate the map of properties.
299 * From these properties, it generates the different types of styles.
300 *
301 * @param osm the primitive to create styles for
302 * @param scale the scale (in meters per 100 px), must be &gt; 0
303 * @param multipolyOuterWay support for a very old multipolygon tagging style
304 * where you add the tags both to the outer and the inner way.
305 * However, independent inner way style is also possible.
306 * @param pretendWayIsClosed For styles that require the way to be closed,
307 * we pretend it is. This is useful for generating area styles from the (segmented)
308 * outer ways of a multipolygon.
309 * @return the generated styles and the valid range as a pair
310 */
311 public Pair<StyleList, Range> generateStyles(OsmPrimitive osm, double scale, OsmPrimitive multipolyOuterWay, boolean pretendWayIsClosed) {
312
313 List<ElemStyle> sl = new ArrayList<>();
314 MultiCascade mc = new MultiCascade();
315 Environment env = new Environment(osm, mc, null, null);
316
317 for (StyleSource s : styleSources) {
318 if (s.active) {
319 s.apply(mc, osm, scale, multipolyOuterWay, pretendWayIsClosed);
320 }
321 }
322
323 for (Entry<String, Cascade> e : mc.getLayers()) {
324 if ("*".equals(e.getKey())) {
325 continue;
326 }
327 env.layer = e.getKey();
328 Cascade c = e.getValue();
329 if (osm instanceof Way) {
330 addIfNotNull(sl, AreaElemStyle.create(c));
331 addIfNotNull(sl, RepeatImageElemStyle.create(env));
332 addIfNotNull(sl, LineElemStyle.createLine(env));
333 addIfNotNull(sl, LineElemStyle.createLeftCasing(env));
334 addIfNotNull(sl, LineElemStyle.createRightCasing(env));
335 addIfNotNull(sl, LineElemStyle.createCasing(env));
336 addIfNotNull(sl, LineTextElemStyle.create(env));
337 } else if (osm instanceof Node) {
338 NodeElemStyle nodeStyle = NodeElemStyle.create(env);
339 if (nodeStyle != null) {
340 sl.add(nodeStyle);
341 addIfNotNull(sl, BoxTextElemStyle.create(env, nodeStyle.getBoxProvider()));
342 } else {
343 addIfNotNull(sl, BoxTextElemStyle.create(env, NodeElemStyle.SIMPLE_NODE_ELEMSTYLE.getBoxProvider()));
344 }
345 } else if (osm instanceof Relation) {
346 if (((Relation)osm).isMultipolygon()) {
347 addIfNotNull(sl, AreaElemStyle.create(c));
348 addIfNotNull(sl, RepeatImageElemStyle.create(env));
349 addIfNotNull(sl, LineElemStyle.createLine(env));
350 addIfNotNull(sl, LineElemStyle.createCasing(env));
351 addIfNotNull(sl, LineTextElemStyle.create(env));
352 } else if ("restriction".equals(osm.get("type"))) {
353 addIfNotNull(sl, NodeElemStyle.create(env));
354 }
355 }
356 }
357 return new Pair<>(new StyleList(sl), mc.range);
358 }
359
360 private static <T> void addIfNotNull(List<T> list, T obj) {
361 if (obj != null) {
362 list.add(obj);
363 }
364 }
365
366 /**
367 * Draw a default node symbol for nodes that have no style?
368 */
369 private boolean isDefaultNodes() {
370 if (defaultNodesIdx == cacheIdx)
371 return defaultNodes;
372 defaultNodes = fromCanvas("default-points", true, Boolean.class);
373 defaultNodesIdx = cacheIdx;
374 return defaultNodes;
375 }
376
377 /**
378 * Draw a default line for ways that do not have an own line style?
379 */
380 private boolean isDefaultLines() {
381 if (defaultLinesIdx == cacheIdx)
382 return defaultLines;
383 defaultLines = fromCanvas("default-lines", true, Boolean.class);
384 defaultLinesIdx = cacheIdx;
385 return defaultLines;
386 }
387
388 private <T> T fromCanvas(String key, T def, Class<T> c) {
389 MultiCascade mc = new MultiCascade();
390 Relation r = new Relation();
391 r.put("#canvas", "query");
392
393 for (StyleSource s : styleSources) {
394 if (s.active) {
395 s.apply(mc, r, 1, null, false);
396 }
397 }
398 return mc.getCascade("default").get(key, def, c);
399 }
400
401 public boolean isDrawMultipolygon() {
402 return drawMultipolygon;
403 }
404
405 public void setDrawMultipolygon(boolean drawMultipolygon) {
406 this.drawMultipolygon = drawMultipolygon;
407 }
408
409 /**
410 * remove all style sources; only accessed from MapPaintStyles
411 */
412 void clear() {
413 styleSources.clear();
414 }
415
416 /**
417 * add a style source; only accessed from MapPaintStyles
418 */
419 void add(StyleSource style) {
420 styleSources.add(style);
421 }
422
423 /**
424 * set the style sources; only accessed from MapPaintStyles
425 */
426 void setStyleSources(Collection<StyleSource> sources) {
427 styleSources.clear();
428 styleSources.addAll(sources);
429 }
430
431 /**
432 * Returns the first AreaElemStyle for a given primitive.
433 * @param p the OSM primitive
434 * @param pretendWayIsClosed For styles that require the way to be closed,
435 * we pretend it is. This is useful for generating area styles from the (segmented)
436 * outer ways of a multipolygon.
437 * @return first AreaElemStyle found or {@code null}.
438 */
439 public static AreaElemStyle getAreaElemStyle(OsmPrimitive p, boolean pretendWayIsClosed) {
440 if (MapPaintStyles.getStyles() == null)
441 return null;
442 for (ElemStyle s : MapPaintStyles.getStyles().generateStyles(p, 1.0, null, pretendWayIsClosed).a) {
443 if (s instanceof AreaElemStyle)
444 return (AreaElemStyle) s;
445 }
446 return null;
447 }
448
449 /**
450 * Determines whether primitive has an AreaElemStyle.
451 * @param p the OSM primitive
452 * @param pretendWayIsClosed For styles that require the way to be closed,
453 * we pretend it is. This is useful for generating area styles from the (segmented)
454 * outer ways of a multipolygon.
455 * @return {@code true} if primitive has an AreaElemStyle
456 */
457 public static boolean hasAreaElemStyle(OsmPrimitive p, boolean pretendWayIsClosed) {
458 return getAreaElemStyle(p, pretendWayIsClosed) != null;
459 }
460}
Note: See TracBrowser for help on using the repository browser.