gngrOrg / gngr

a cross-platform browser focussed on privacy.
https://gngr.info
285 stars 61 forks source link

Use strokes for drawing borders #39

Open hrj opened 9 years ago

hrj commented 9 years ago

GUITasks.drawDashed() draws each dash individually!

Using strokes from Graphics2D would be much more optimal.

hrj commented 9 years ago

Note that:

The corners need to dove-tail into each other. Especially noticeable when the colors of each side are different.

hrj commented 9 years ago

Aside, There is an interesting article on the web about low performance in Java with stroked graphics (can't find the link now), and I played with the benchmark a bit.

The result was (after warmup):

Test Time (microseconds)
1. stroked 52213.7
2. stroked (with overhang outside of clip bounds) 151627.5
3. hard-coded dashes in a loop 14884.0
4. simple line (for reference) 454.2

Since in our use-case, we don't need to worry about overhanging strokes, the relevant comparison is in between 1 and 3, that is about 3.5x difference.

However, there is a catch. The current code draws the dashes in a loop across the thickness of the border. This to get the nice dove-tail effect at the corners. The same effect could be achieved with a single stroked line, with a trapezoidal clip. The trapezoidal clipping might introduce an overhead (not measured yet).

Conclusion

If the thickness of the border is greater than 3.5 pixels, it will be faster to use the stroked line, and for smaller pixel thickness, the current algorithm is faster. (Assuming no overhead for trapezoidal clip)

hrj commented 9 years ago

Further to that, if adjoining borders have the same style, color, thickness, etc, then they could be drawn with a single path drawing operation. Especially when all four sides are the same, then a draw rectangle call can be used. This case will be common and hence worth optimising for.