[QT]about canvas:getImageData() and memory usage

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
poorenglish
Prole
Posts: 47
Joined: Thu Sep 24, 2009 1:49 pm

[QT]about canvas:getImageData() and memory usage

Post by poorenglish » Tue Oct 29, 2013 2:33 pm

I use canvas:getImageData() in the function love.mousepressed(x,y,button),found the memory usage will increase too faster,it seems the memory will not be release.

Code: Select all

-- the image size is 1024*768,the app size is 1024*768 too

function love.load()
	assert(love.graphics.isSupported('pixeleffect'), 'Pixel effects are not supported on your hardware. Sorry about that.')

	effect = love.graphics.newPixelEffect [[
		extern Image img;
        vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords)
        {
			vec4 pixel1 = Texel(img,texture_coords);
			vec4 pixel	= Texel(texture, texture_coords);
			if (pixel1.r >0)
			{
				pixel.a = 0;
			}
		return pixel;
        }
    ]]

	ColourBackGround = love.graphics.newImage("back.png")
	BlackWhitebackGround = love.graphics.newImage("backwb.png")
	Mask = love.graphics.newImage("amask.png")

	canvasMask = love.graphics.newCanvas(1024,768)

	--	canvasMask:clear()
    --    love.graphics.setBlendMode('alpha')
	--	love.graphics.setCanvas(canvasMask)
		love.graphics.draw(Mask,0,0)
		--love.graphics.setColor(0,0,0,255)
	--	love.graphics.circle("fill",100,100,50,100)
	love.graphics.setCanvas()
	effect:send("img",canvasMask)


	canvas = love.graphics.newCanvas(1024,768)
	love.graphics.setCanvas(canvas)
	love.graphics.draw(ColourBackGround,0,0)
	love.graphics.setCanvas()

end


function love.draw()
	--love.graphics.draw(canvasMask, 0, 0)

	love.graphics.draw(canvas, 0, 0)

	love.graphics.setPixelEffect(effect)
	love.graphics.draw(BlackWhitebackGround, 0, 0)
	love.graphics.setPixelEffect()
--]]
	love.graphics.print( 'fps: '..love.timer.getFPS(), 10,10 )
end

function love.mousepressed(x,y,button)
	--effect:send('area',{x,})
	canvasMask:renderTo(function()
		--canvasMask:clear()
        love.graphics.setBlendMode('alpha')
		love.graphics.setCanvas(canvasMask)
		love.graphics.draw(Mask,0,0)
		--love.graphics.setColor(0,0,0,255)
		love.graphics.circle("fill",x,768-y,50,100)
	end)
	local data = canvasMask:getImageData()
	Mask = love.graphics.newImage(data)
	--effect:send("img",canvasMask)

end
Is this the wrong method for canvas:getImageData()

User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: [QT]about canvas:getImageData() and memory usage

Post by Boolsheet » Tue Oct 29, 2013 6:26 pm

It will be released, eventually. The garbage collector will run and remove it once Lua hits a certain memory limit.

Lua doesn't know about the size of the raw data of the ImageData. All it knows is the size of the userdata which is a few bytes. You can create quite a few ImageData before the garbage collector runs. If you know you have garbage that you want to be removed now, call [manual]collectgarbage[/manual]("collect") to force a full cycle and remove everything that isn't referenced anymore.

Edit:

Code: Select all

canvasMask:renderTo(function()
      love.graphics.setCanvas(canvasMask)
This is not necessary. renderTo already selects the canvas as a render target for you.
Shallow indentations.

poorenglish
Prole
Posts: 47
Joined: Thu Sep 24, 2009 1:49 pm

Re: [QT]about canvas:getImageData() and memory usage

Post by poorenglish » Sat Nov 02, 2013 1:58 pm

Sorry,too late for replying,thanks

Post Reply

Who is online

Users browsing this forum: No registered users and 24 guests