Page 1 of 1

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

Posted: Tue Oct 29, 2013 2:33 pm
by poorenglish
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()

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

Posted: Tue Oct 29, 2013 6:26 pm
by Boolsheet
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.

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

Posted: Sat Nov 02, 2013 1:58 pm
by poorenglish
Sorry,too late for replying,thanks