gears.surface: Handle the cache more intelligently

It doesn't make sense for surface.load_uncached() to load a file without
inserting into the cache. The next "cached" load will have to load it again.

So move cache insertion into surface.load_uncached() and the only thing that
surface.load() does differently is checking if we have a suitable cache entry
before calling load_uncached().

So load_uncached() does the cache insertion and load() reads from the cache.

Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2014-04-09 21:55:07 +02:00
parent 88643eb0cc
commit 96c49e6c6a
1 changed files with 11 additions and 6 deletions

View File

@ -22,6 +22,7 @@ local surface_cache = setmetatable({}, { __mode = 'v' })
--- Try to convert the argument into an lgi cairo surface. --- Try to convert the argument into an lgi cairo surface.
-- This is usually needed for loading images by file name. -- This is usually needed for loading images by file name.
function surface.load_uncached(_surface) function surface.load_uncached(_surface)
local file
-- Nil is not changed -- Nil is not changed
if not _surface then if not _surface then
return nil return nil
@ -34,21 +35,25 @@ function surface.load_uncached(_surface)
end end
-- Strings are assumed to be file names and get loaded -- Strings are assumed to be file names and get loaded
if type(_surface) == "string" then if type(_surface) == "string" then
_surface = capi.awesome.load_image(_surface) file = _surface
_surface = capi.awesome.load_image(file)
end end
-- Everything else gets forced into a surface -- Everything else gets forced into a surface
return cairo.Surface(_surface, true) _surface = cairo.Surface(_surface, true)
-- If we loaded a file, cache it
if file then
surface_cache[file] = _surface
end
return _surface
end end
function surface.load(_surface) function surface.load(_surface)
if type(_surface) == "string" then if type(_surface) == "string" then
local cache = surface_cache[_surface] local cache = surface_cache[_surface]
if not cache then if cache then
cache = surface.load_uncached(_surface)
surface_cache[_surface] = cache
end
return cache return cache
end end
end
return surface.load_uncached(_surface) return surface.load_uncached(_surface)
end end