Skip to content

Instantly share code, notes, and snippets.

@psychon
Last active May 1, 2020 14:49
Show Gist options
  • Save psychon/a72b09cf94043a01621470b0f8a097b0 to your computer and use it in GitHub Desktop.
Save psychon/a72b09cf94043a01621470b0f8a097b0 to your computer and use it in GitHub Desktop.
diff --git a/tests/test-benchmark.lua b/tests/test-benchmark.lua
index 61e4c4e54..4f6a05ace 100644
--- a/tests/test-benchmark.lua
+++ b/tests/test-benchmark.lua
@@ -77,6 +77,129 @@ benchmark(relayout_textclock, "relayout textclock")
benchmark(redraw_textclock, "redraw textclock")
benchmark(e2e_tag_switch, "tag switch")
+local function sign(x)
+ if x > 0 then
+ return 1
+ elseif x < 0 then
+ return -1
+ else
+ return 0
+ end
+end
+local pow = math.pow
+local cos = math.cos
+local abs = math.abs
+local sin = math.sin
+function partial_squircle(cr, width, height, tl, tr, br, bl, rate, delta)
+ -- rate ~ 2 can be used by icon
+ -- this shape doesn't really fit clients
+ -- but you can still use with rate ~ 32
+ rate = rate or 2
+ -- smaller the delta the smoother the shape
+ -- but probably more laggy
+ delta = delta or 0.05
+
+ -- just ellipse things
+ local a = width / 2
+ local b = height / 2
+ local phi = 0
+
+ -- move to origin
+ -- the shape goes counter clock wise
+ -- start from (w h / 2)
+ cr:save()
+ cr:translate(a, b)
+ cr:move_to(a, 0)
+
+ -- draw the corner if that corner is rounded
+ local curved_corner = function()
+ local end_angle = phi + math.pi / 2
+ while phi < end_angle do
+ local cosphi = cos(phi)
+ local sinphi = sin(phi)
+ local x = a * pow(abs(cosphi), 1 / rate) * sign(cosphi)
+ local y = b * pow(abs(sinphi), 1 / rate) * sign(sinphi)
+ -- so weird, y axis is inverted
+ cr:line_to(x, -y)
+ phi = phi + delta
+ end
+ end
+
+ -- draw with polar cord
+ -- draw top right
+ if tr then
+ curved_corner()
+ else
+ cr:move_to(a, 0)
+ cr:line_to(a, -b)
+ cr:line_to(0, -b)
+ phi = math.pi * 0.5
+ end
+
+ -- draw top left
+ if tl then
+ curved_corner()
+ else
+ cr:line_to(-a, -b)
+ cr:line_to(-a, 0)
+ phi = math.pi
+ end
+
+ if bl then
+ curved_corner()
+ else
+ cr:line_to(-a, b)
+ cr:line_to( 0, b)
+ phi = math.pi * 1.5
+ end
+
+ -- bottom right
+ if br then
+ curved_corner()
+ else
+ cr:line_to(a, b)
+ cr:line_to(a, 0)
+ phi = math.pi * 2
+ end
+
+ -- it's time to stop
+ cr:close_path()
+
+ -- restore cairo context
+ cr:restore()
+end
+function squircle(cr, width, height, rate, delta)
+ partial_squircle(cr, width, height, true, true, true, true, rate, delta)
+end
+
+local cairo = require("lgi").cairo
+local s = cairo.ImageSurface(cairo.Format.ARGB32, 0, 0)
+local cr = cairo.Context(s)
+local function foo(delta)
+ cr:new_path()
+ squircle(cr, 1000, 1000, nil, delta)
+end
+sizes = {
+ 0.5,
+ 0.05,
+ 0.005,
+ 0.0005,
+ 0.00005,
+}
+print(_VERSION)
+for _, v in ipairs(sizes) do
+ foo(v)
+ local count = 0
+ for t, pts in cr:copy_path():pairs() do
+ --gears.debug.dump{ [t] = pts }
+ count = count + 1
+ end
+ print(v, count)
+end
+for _, v in ipairs(sizes) do
+ benchmark(function() foo(v) end, "squircle " .. tostring(v))
+end
+
runner.run_steps({ function() return true end })
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment