Browse Source

Improved performance and reduced temporary memory usage of love.graphics.newScreenshot and Canvas:getImageData

Alex Szpakowski 12 years ago
parent
commit
f25efcc8af

+ 2 - 2
src/modules/graphics/opengl/Canvas.cpp

@@ -652,10 +652,10 @@ love::image::ImageData *Canvas::getImageData(love::image::Image *image)
 	for (int i = 0; i < height; ++i, dst -= row, src += row)
 		memcpy(dst, src, row);
 
-	love::image::ImageData *img = image->newImageData(width, height, (void *)flipped);
+	love::image::ImageData *img = image->newImageData(width, height, (void *)flipped, true);
 
+	// The new ImageData now owns the flipped data, so we don't delete it here.
 	delete[] pixels;
-	delete[] flipped;
 
 	return img;
 }

+ 3 - 3
src/modules/graphics/opengl/Graphics.cpp

@@ -1201,7 +1201,9 @@ love::image::ImageData *Graphics::newScreenshot(love::image::Image *image, bool
 	love::image::ImageData *img = 0;
 	try
 	{
-		img = image->newImageData(w, h, (void *) screenshot);
+		// Tell the new ImageData that it owns the screenshot data, so we don't
+		// need to delete it here.
+		img = image->newImageData(w, h, (void *) screenshot, true);
 	}
 	catch (love::Exception &)
 	{
@@ -1211,8 +1213,6 @@ love::image::ImageData *Graphics::newScreenshot(love::image::Image *image, bool
 		throw;
 	}
 
-	delete[] screenshot;
-
 	// Re-bind the active canvas, if necessary.
 	if (curcanvas)
 		curcanvas->startGrab(curcanvas->getAttachedCanvases());

+ 3 - 1
src/modules/image/Image.h

@@ -69,9 +69,11 @@ public:
 	 * @param The width of the ImageData.
 	 * @param The height of the ImageData.
 	 * @param The data to load into the ImageData.
+	 * @param Whether the new ImageData should take ownership of the data or
+	 *        copy it.
 	 * @return The new ImageData.
 	 **/
-	virtual ImageData *newImageData(int width, int height, void *data) = 0;
+	virtual ImageData *newImageData(int width, int height, void *data, bool own = false) = 0;
 
 	/**
 	 * Creates new CompressedData from FileData.

+ 2 - 2
src/modules/image/magpie/Image.cpp

@@ -57,9 +57,9 @@ love::image::ImageData *Image::newImageData(int width, int height)
 	return new ImageData(width, height);
 }
 
-love::image::ImageData *Image::newImageData(int width, int height, void *data)
+love::image::ImageData *Image::newImageData(int width, int height, void *data, bool own)
 {
-	return new ImageData(width, height, data);
+	return new ImageData(width, height, data, own);
 }
 
 love::image::CompressedData *Image::newCompressedData(love::filesystem::FileData *data)

+ 1 - 1
src/modules/image/magpie/Image.h

@@ -48,7 +48,7 @@ public:
 
 	love::image::ImageData *newImageData(love::filesystem::FileData *data);
 	love::image::ImageData *newImageData(int width, int height);
-	love::image::ImageData *newImageData(int width, int height, void *data);
+	love::image::ImageData *newImageData(int width, int height, void *data, bool own = false);
 
 	love::image::CompressedData *newCompressedData(love::filesystem::FileData *data);
 

+ 6 - 2
src/modules/image/magpie/ImageData.cpp

@@ -45,11 +45,15 @@ ImageData::ImageData(int width, int height)
 	memset(data, 0, width*height*sizeof(pixel));
 }
 
-ImageData::ImageData(int width, int height, void *data)
+ImageData::ImageData(int width, int height, void *data, bool own)
 {
 	this->width = width;
 	this->height = height;
-	create(width, height, data);
+
+	if (own)
+		this->data = (unsigned char *) data;
+	else
+		create(width, height, data);
 }
 
 ImageData::~ImageData()

+ 1 - 1
src/modules/image/magpie/ImageData.h

@@ -38,7 +38,7 @@ public:
 
 	ImageData(love::filesystem::FileData *data);
 	ImageData(int width, int height);
-	ImageData(int width, int height, void *data);
+	ImageData(int width, int height, void *data, bool own);
 	virtual ~ImageData();
 
 	// Implements image::ImageData.