Skip to content

Commit

Permalink
Fiddle with CBB toggling to try to avoid weird Android corner-cases (#…
Browse files Browse the repository at this point in the history
…1263)

* Handle inversion in BB4:fill

Not that it changes much in practice, apparently.

* Rejig the CBB toggle to allow greater flexibility in front

Most notably, allowing to disable it on startup in reader.lua,
without having to do an extra jit.flush, because it has a tendency to
make the already complicated mcode allocation situation worse on Android.
  • Loading branch information
NiLuJe committed Dec 23, 2020
1 parent 2da6869 commit 11de43f
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions ffi/blitbuffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ void BB_color_blit_from(BlitBuffer * restrict dest, const BlitBuffer * restrict

-- We'll load it later
local cblitbuffer
local use_cblitbuffer = false
local use_cblitbuffer

-- color value types
local Color4U = ffi.typeof("Color4U")
Expand Down Expand Up @@ -1327,7 +1327,11 @@ function BB_mt.__index:fill(value)
end

function BB4_mt.__index:fill(value)
local v = value:getColor4L().a
-- Handle invert...
local v = value:getColor8()
if self:getInverse() == 1 then v = v:invert() end

v = v:getColor4L().a
v = bor(lshift(v, 4), v)
ffi.fill(self.data, self.stride*self.h, v)
end
Expand Down Expand Up @@ -2163,12 +2167,22 @@ BB.TYPE_TO_BPP = {
}

BB.has_cblitbuffer = false
if not os.getenv("KO_NO_CBB") then
-- Load C blit buffer, we'll decide whether to use it later on
BB.has_cblitbuffer, cblitbuffer = pcall(ffi.load, "blitbuffer")
-- Load the C blitter, and default to using it if available
BB.has_cblitbuffer, cblitbuffer = pcall(ffi.load, "blitbuffer")
if BB.has_cblitbuffer then
-- If we can, assume we'll want to use it
use_cblitbuffer = true
else
use_cblitbuffer = false
end

-- Allow front to update the use_cblitbuffer flag directly, with no checks and no JIT tweaks
function BB:setUseCBB(enabled)
use_cblitbuffer = enabled
end

-- Set the actual enable/disable CBB flag. Returns the flag of whether it is (actually) enabled.
-- Set the actual enable/disable CBB flag and tweak JIT opts accordingly.
-- Returns the actual state.
function BB:enableCBB(enabled)
local old = use_cblitbuffer
use_cblitbuffer = enabled and self.has_cblitbuffer
Expand All @@ -2177,14 +2191,14 @@ function BB:enableCBB(enabled)
-- which'd obviously *murder* performance (to the effect of a soft-lock, essentially).
-- c.f., koreader/koreader#4137, koreader/koreader#4752, koreader/koreader#4782,
-- koreader/koreader#6736, #1233
-- 15 is LuaJIT's default
local val = use_cblitbuffer and 15 or 45
jit.opt.start("loopunroll="..tostring(val))
jit.flush()
end
return use_cblitbuffer
end

-- By default it's on (if not blacklisted). But frontend may still decide otherwise before anything is ever drawn.
BB:enableCBB(true)
-- NOTE: reader.lua will update the flag on startup, with the least amount of JIT tweaking possible.

return BB

0 comments on commit 11de43f

Please sign in to comment.