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 76ab008b3e
commit 7ec72862d6
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.
-- This is usually needed for loading images by file name.
function surface.load_uncached(_surface)
local file
-- Nil is not changed
if not _surface then
return nil
@ -34,21 +35,25 @@ function surface.load_uncached(_surface)
end
-- Strings are assumed to be file names and get loaded
if type(_surface) == "string" then
_surface = capi.awesome.load_image(_surface)
file = _surface
_surface = capi.awesome.load_image(file)
end
-- 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
function surface.load(_surface)
if type(_surface) == "string" then
local cache = surface_cache[_surface]
if not cache then
cache = surface.load_uncached(_surface)
surface_cache[_surface] = cache
end
if cache then
return cache
end
end
return surface.load_uncached(_surface)
end