Use gears.matrix instead of cairo.Matrix everywhere

This has some positive results on the "benchmark test". Each single number is
the best one out of three runs.

Before:
        create wibox: 0.0826502  sec/iter ( 13 iters, 1.157 sec for benchmark)
    update textclock: 0.0186952  sec/iter ( 57 iters, 2.473 sec for benchmark)
  relayout textclock: 0.0158112  sec/iter ( 64 iters, 1.028 sec for benchmark)
    redraw textclock: 0.0015197  sec/iter (662 iters, 1.861 sec for benchmark)

After:
        create wibox: 0.0825672  sec/iter ( 13 iters, 1.154 sec for benchmark)
    update textclock: 0.00378412 sec/iter (277 iters, 4.216 sec for benchmark)
  relayout textclock: 0.00259056 sec/iter (420 iters, 1.09 sec for benchmark)
    redraw textclock: 0.00105128 sec/iter (958 iters, 1.79 sec for benchmark)

We see no significant change in the creation of wiboxes (99.9% compared to
before). Update (20% of the previous run time), relayout (16%) and redraw (69%)
are all sped up by this change.

Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2015-09-16 13:57:38 +02:00
parent 34927b187d
commit b134318f19
6 changed files with 58 additions and 78 deletions

View File

@ -11,16 +11,6 @@ local cairo = require("lgi").cairo
local debug = require("gears.debug") local debug = require("gears.debug")
local matrix = {} local matrix = {}
--- Copy a cairo matrix
-- @param matrix The matrix to copy.
-- @return A copy of the given cairo matrix.
function matrix.copy(matrix)
debug.assert(cairo.Matrix:is_type_of(matrix), "Argument should be a cairo matrix")
local ret = cairo.Matrix()
ret:init(matrix.xx, matrix.yx, matrix.xy, matrix.yy, matrix.x0, matrix.y0)
return ret
end
-- Metatable for matrix instances. This is set up near the end of the file. -- Metatable for matrix instances. This is set up near the end of the file.
local matrix_mt = {} local matrix_mt = {}

View File

@ -37,11 +37,8 @@ local function hierarchy_new(context, widget, width, height, redraw_callback, la
widget:weak_connect_signal("widget::layout_changed", result._layout) widget:weak_connect_signal("widget::layout_changed", result._layout)
for _, w in ipairs(children or {}) do for _, w in ipairs(children or {}) do
local to_dev = cairo.Matrix.create_identity()
to_dev:multiply(matrix_to_device, w._matrix)
local r = hierarchy_new(context, w._widget, w._width, w._height, redraw_callback, layout_callback, local r = hierarchy_new(context, w._widget, w._width, w._height, redraw_callback, layout_callback,
callback_arg, matrix.copy(w._matrix), to_dev) callback_arg, w._matrix, matrix_to_device * w._matrix)
table.insert(result._children, r) table.insert(result._children, r)
-- Update our drawing extents -- Update our drawing extents
@ -82,7 +79,7 @@ end
-- @return A new widget hierarchy -- @return A new widget hierarchy
function hierarchy.new(context, widget, width, height, redraw_callback, layout_callback, callback_arg) function hierarchy.new(context, widget, width, height, redraw_callback, layout_callback, callback_arg)
return hierarchy_new(context, widget, width, height, redraw_callback, layout_callback, callback_arg, return hierarchy_new(context, widget, width, height, redraw_callback, layout_callback, callback_arg,
cairo.Matrix.create_identity(), cairo.Matrix.create_identity()) matrix.identity, matrix.identity)
end end
--- Get the widget that this hierarchy manages. --- Get the widget that this hierarchy manages.
@ -90,38 +87,36 @@ function hierarchy:get_widget()
return self._widget return self._widget
end end
--- Get a cairo matrix that transforms to the parent's coordinate space from --- Get a matrix that transforms to the parent's coordinate space from this
-- this hierarchy's coordinate system. -- hierarchy's coordinate system.
-- @return A cairo matrix describing the transformation. -- @return A matrix describing the transformation.
function hierarchy:get_matrix_to_parent() function hierarchy:get_matrix_to_parent()
return matrix.copy(self._matrix) return self._matrix
end end
--- Get a cairo matrix that transforms to the base of this hierarchy's --- Get a matrix that transforms to the base of this hierarchy's coordinate
-- coordinate system (aka the coordinate system of the device that this -- system (aka the coordinate system of the device that this
-- hierarchy is applied upon) from this hierarchy's coordinate system. -- hierarchy is applied upon) from this hierarchy's coordinate system.
-- @return A cairo matrix describing the transformation. -- @return A matrix describing the transformation.
function hierarchy:get_matrix_to_device() function hierarchy:get_matrix_to_device()
return matrix.copy(self._matrix_to_device) return self._matrix_to_device
end end
--- Get a cairo matrix that transforms from the parent's coordinate space into --- Get a matrix that transforms from the parent's coordinate space into this
-- this hierarchy's coordinate system. -- hierarchy's coordinate system.
-- @return A cairo matrix describing the transformation. -- @return A matrix describing the transformation.
function hierarchy:get_matrix_from_parent() function hierarchy:get_matrix_from_parent()
local m = self:get_matrix_to_parent() local m = self:get_matrix_to_parent()
m:invert() return m:invert()
return m
end end
--- Get a cairo matrix that transforms from the base of this hierarchy's --- Get a matrix that transforms from the base of this hierarchy's coordinate
-- coordinate system (aka the coordinate system of the device that this -- system (aka the coordinate system of the device that this
-- hierarchy is applied upon) into this hierarchy's coordinate system. -- hierarchy is applied upon) into this hierarchy's coordinate system.
-- @return A cairo matrix describing the transformation. -- @return A matrix describing the transformation.
function hierarchy:get_matrix_from_device() function hierarchy:get_matrix_from_device()
local m = self:get_matrix_to_device() local m = self:get_matrix_to_device()
m:invert() return m:invert()
return m
end end
--- Get the extents that this hierarchy possibly draws to (in the current coordinate space). --- Get the extents that this hierarchy possibly draws to (in the current coordinate space).
@ -198,7 +193,7 @@ function hierarchy:draw(context, cr)
end end
cr:save() cr:save()
cr:transform(self:get_matrix_to_parent()) cr:transform(self:get_matrix_to_parent():to_cairo_matrix())
-- Clip to the draw extents -- Clip to the draw extents
cr:rectangle(self:get_draw_extents()) cr:rectangle(self:get_draw_extents())

View File

@ -11,7 +11,7 @@ local pairs = pairs
local ipairs = ipairs local ipairs = ipairs
local setmetatable = setmetatable local setmetatable = setmetatable
local base = require("wibox.widget.base") local base = require("wibox.widget.base")
local Matrix = require("lgi").cairo.Matrix local matrix = require("gears.matrix")
local mirror = { mt = {} } local mirror = { mt = {} }
@ -23,7 +23,7 @@ function mirror:layout(context, cr, width, height)
return return
end end
local m = Matrix.create_identity() local m = matrix.identity
local t = { x = 0, y = 0 } -- translation local t = { x = 0, y = 0 } -- translation
local s = { x = 1, y = 1 } -- scale local s = { x = 1, y = 1 } -- scale
if self.horizontal then if self.horizontal then
@ -34,8 +34,8 @@ function mirror:layout(context, cr, width, height)
t.x = width t.x = width
s.x = -1 s.x = -1
end end
m:translate(t.x, t.y) m = m:translate(t.x, t.y)
m:scale(s.x, s.y) m = m:scale(s.x, s.y)
return base.place_widget_via_matrix(widget, m, width, height) return base.place_widget_via_matrix(widget, m, width, height)
end end

View File

@ -12,7 +12,7 @@ local type = type
local setmetatable = setmetatable local setmetatable = setmetatable
local tostring = tostring local tostring = tostring
local base = require("wibox.widget.base") local base = require("wibox.widget.base")
local Matrix = require("lgi").cairo.Matrix local matrix = require("gears.matrix")
local rotate = { mt = {} } local rotate = { mt = {} }
@ -32,16 +32,16 @@ function rotate:layout(context, width, height)
local dir = self:get_direction() local dir = self:get_direction()
local m = Matrix.create_identity() local m = matrix.identity
if dir == "west" then if dir == "west" then
m:rotate(pi / 2) m = m:rotate(pi / 2)
m:translate(0, -width) m = m:translate(0, -width)
elseif dir == "south" then elseif dir == "south" then
m:rotate(pi) m = m:rotate(pi)
m:translate(-width, -height) m = m:translate(-width, -height)
elseif dir == "east" then elseif dir == "east" then
m:rotate(3 * pi / 2) m = m:rotate(3 * pi / 2)
m:translate(-height, 0) m = m:translate(-height, 0)
end end
-- Since we rotated, we might have to swap width and height. -- Since we rotated, we might have to swap width and height.

View File

@ -9,7 +9,6 @@ local debug = require("gears.debug")
local object = require("gears.object") local object = require("gears.object")
local cache = require("gears.cache") local cache = require("gears.cache")
local matrix = require("gears.matrix") local matrix = require("gears.matrix")
local Matrix = require("lgi").cairo.Matrix
local setmetatable = setmetatable local setmetatable = setmetatable
local pairs = pairs local pairs = pairs
local type = type local type = type
@ -204,8 +203,8 @@ end
--- Create widget placement information. This should be used for a widget's --- Create widget placement information. This should be used for a widget's
-- `:layout()` callback. -- `:layout()` callback.
-- @param widget The widget that should be placed. -- @param widget The widget that should be placed.
-- @param mat A cairo matrix transforming from the parent widget's coordinate -- @param mat A matrix transforming from the parent widget's coordinate
-- system. For example, use cairo.Matrix.create_translate(1, 2) to draw a -- system. For example, use matrix.create_translate(1, 2) to draw a
-- widget at position (1, 2) relative to the parent widget. -- widget at position (1, 2) relative to the parent widget.
-- @param width The width of the widget in its own coordinate system. That is, -- @param width The width of the widget in its own coordinate system. That is,
-- after applying the transformation matrix. -- after applying the transformation matrix.
@ -217,7 +216,7 @@ function base.place_widget_via_matrix(widget, mat, width, height)
_widget = widget, _widget = widget,
_width = width, _width = width,
_height = height, _height = height,
_matrix = matrix.copy(mat) _matrix = mat
} }
end end
@ -232,7 +231,7 @@ end
-- after applying the transformation matrix. -- after applying the transformation matrix.
-- @return An opaque object that can be returned from :layout() -- @return An opaque object that can be returned from :layout()
function base.place_widget_at(widget, x, y, width, height) function base.place_widget_at(widget, x, y, width, height)
return base.place_widget_via_matrix(widget, Matrix.create_translate(x, y), width, height) return base.place_widget_via_matrix(widget, matrix.create_translate(x, y), width, height)
end end
--[[-- --[[--

View File

@ -5,7 +5,7 @@
local hierarchy = require("wibox.hierarchy") local hierarchy = require("wibox.hierarchy")
local cairo = require("lgi").cairo local Region = require("lgi").cairo.Region
local matrix = require("gears.matrix") local matrix = require("gears.matrix")
local object = require("gears.object") local object = require("gears.object")
local utils = require("wibox.test_utils") local utils = require("wibox.test_utils")
@ -37,23 +37,19 @@ describe("wibox.hierarchy", function()
end) end)
it("get_matrix_to_parent", function() it("get_matrix_to_parent", function()
assert.is_true(matrix.equals(cairo.Matrix.create_identity(), assert.is.equal(matrix.identity, instance:get_matrix_to_parent())
instance:get_matrix_to_parent()))
end) end)
it("get_matrix_to_device", function() it("get_matrix_to_device", function()
assert.is_true(matrix.equals(cairo.Matrix.create_identity(), assert.is.equal(matrix.identity, instance:get_matrix_to_device())
instance:get_matrix_to_device()))
end) end)
it("get_matrix_from_parent", function() it("get_matrix_from_parent", function()
assert.is_true(matrix.equals(cairo.Matrix.create_identity(), assert.is.equal(matrix.identity, instance:get_matrix_from_parent())
instance:get_matrix_from_parent()))
end) end)
it("get_matrix_from_device", function() it("get_matrix_from_device", function()
assert.is_true(matrix.equals(cairo.Matrix.create_identity(), assert.is.equal(matrix.identity, instance:get_matrix_from_device())
instance:get_matrix_from_device()))
end) end)
it("get_draw_extents", function() it("get_draw_extents", function()
@ -72,7 +68,7 @@ describe("wibox.hierarchy", function()
it("disconnect works", function() it("disconnect works", function()
local child = make_widget(nil) local child = make_widget(nil)
local parent = make_widget({ local parent = make_widget({
make_child(child, 2, 5, cairo.Matrix.create_translate(10, 0)) make_child(child, 2, 5, matrix.create_translate(10, 0))
}) })
local extra_arg = {} local extra_arg = {}
@ -129,10 +125,10 @@ describe("wibox.hierarchy", function()
before_each(function() before_each(function()
child = make_widget(nil) child = make_widget(nil)
intermediate = make_widget({ intermediate = make_widget({
make_child(child, 10, 20, cairo.Matrix.create_translate(0, 5)) make_child(child, 10, 20, matrix.create_translate(0, 5))
}) })
parent = make_widget({ parent = make_widget({
make_child(intermediate, 5, 2, cairo.Matrix.create_translate(4, 0)) make_child(intermediate, 5, 2, matrix.create_translate(4, 0))
}) })
local function nop() end local function nop() end
@ -156,27 +152,27 @@ describe("wibox.hierarchy", function()
end) end)
it("get_matrix_to_parent", function() it("get_matrix_to_parent", function()
assert.is_true(matrix.equals(hierarchy_child:get_matrix_to_parent(), cairo.Matrix.create_translate(0, 5))) assert.is.equal(hierarchy_child:get_matrix_to_parent(), matrix.create_translate(0, 5))
assert.is_true(matrix.equals(hierarchy_intermediate:get_matrix_to_parent(), cairo.Matrix.create_translate(4, 0))) assert.is.equal(hierarchy_intermediate:get_matrix_to_parent(), matrix.create_translate(4, 0))
assert.is_true(matrix.equals(hierarchy_parent:get_matrix_to_parent(), cairo.Matrix.create_identity())) assert.is.equal(hierarchy_parent:get_matrix_to_parent(), matrix.identity)
end) end)
it("get_matrix_to_device", function() it("get_matrix_to_device", function()
assert.is_true(matrix.equals(hierarchy_child:get_matrix_to_device(), cairo.Matrix.create_translate(4, 5))) assert.is.equal(hierarchy_child:get_matrix_to_device(), matrix.create_translate(4, 5))
assert.is_true(matrix.equals(hierarchy_intermediate:get_matrix_to_device(), cairo.Matrix.create_translate(4, 0))) assert.is.equal(hierarchy_intermediate:get_matrix_to_device(), matrix.create_translate(4, 0))
assert.is_true(matrix.equals(hierarchy_parent:get_matrix_to_device(), cairo.Matrix.create_identity())) assert.is.equal(hierarchy_parent:get_matrix_to_device(), matrix.identity)
end) end)
it("get_matrix_from_parent", function() it("get_matrix_from_parent", function()
assert.is_true(matrix.equals(hierarchy_child:get_matrix_from_parent(), cairo.Matrix.create_translate(0, -5))) assert.is.equal(hierarchy_child:get_matrix_from_parent(), matrix.create_translate(0, -5))
assert.is_true(matrix.equals(hierarchy_intermediate:get_matrix_from_parent(), cairo.Matrix.create_translate(-4, 0))) assert.is.equal(hierarchy_intermediate:get_matrix_from_parent(), matrix.create_translate(-4, 0))
assert.is_true(matrix.equals(hierarchy_parent:get_matrix_from_parent(), cairo.Matrix.create_identity())) assert.is.equal(hierarchy_parent:get_matrix_from_parent(), matrix.identity)
end) end)
it("get_matrix_from_device", function() it("get_matrix_from_device", function()
assert.is_true(matrix.equals(hierarchy_child:get_matrix_from_device(), cairo.Matrix.create_translate(-4, -5))) assert.is.equal(hierarchy_child:get_matrix_from_device(), matrix.create_translate(-4, -5))
assert.is_true(matrix.equals(hierarchy_intermediate:get_matrix_from_device(), cairo.Matrix.create_translate(-4, 0))) assert.is.equal(hierarchy_intermediate:get_matrix_from_device(), matrix.create_translate(-4, 0))
assert.is_true(matrix.equals(hierarchy_parent:get_matrix_from_device(), cairo.Matrix.create_identity())) assert.is.equal(hierarchy_parent:get_matrix_from_device(), matrix.identity)
end) end)
it("get_draw_extents", function() it("get_draw_extents", function()
@ -199,10 +195,10 @@ describe("wibox.hierarchy", function()
before_each(function() before_each(function()
child = make_widget(nil) child = make_widget(nil)
intermediate = make_widget({ intermediate = make_widget({
make_child(child, 10, 20, cairo.Matrix.create_translate(0, 5)) make_child(child, 10, 20, matrix.create_translate(0, 5))
}) })
parent = make_widget({ parent = make_widget({
make_child(intermediate, 5, 2, cairo.Matrix.create_translate(4, 0)) make_child(intermediate, 5, 2, matrix.create_translate(4, 0))
}) })
local context = {} local context = {}
@ -218,7 +214,7 @@ describe("wibox.hierarchy", function()
it("child moved", function() it("child moved", function()
intermediate.layout = function() intermediate.layout = function()
return { make_child(child, 10, 20, cairo.Matrix.create_translate(0, 4)) } return { make_child(child, 10, 20, matrix.create_translate(0, 4)) }
end end
local context = {} local context = {}
local instance2 = hierarchy.new(context, parent, 15, 16, nop, nop) local instance2 = hierarchy.new(context, parent, 15, 16, nop, nop)