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

This commit is contained in:
Alex Szpakowski
2013-06-15 22:07:16 -03:00
parent 15535517d6
commit f25efcc8af
7 changed files with 18 additions and 12 deletions
+2 -2
View File
@@ -652,10 +652,10 @@ love::image::ImageData *Canvas::getImageData(love::image::Image *image)
for (int i = 0; i < height; ++i, dst -= row, src += row) for (int i = 0; i < height; ++i, dst -= row, src += row)
memcpy(dst, 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[] pixels;
delete[] flipped;
return img; return img;
} }
+3 -3
View File
@@ -1201,7 +1201,9 @@ love::image::ImageData *Graphics::newScreenshot(love::image::Image *image, bool
love::image::ImageData *img = 0; love::image::ImageData *img = 0;
try 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 &) catch (love::Exception &)
{ {
@@ -1211,8 +1213,6 @@ love::image::ImageData *Graphics::newScreenshot(love::image::Image *image, bool
throw; throw;
} }
delete[] screenshot;
// Re-bind the active canvas, if necessary. // Re-bind the active canvas, if necessary.
if (curcanvas) if (curcanvas)
curcanvas->startGrab(curcanvas->getAttachedCanvases()); curcanvas->startGrab(curcanvas->getAttachedCanvases());
+3 -1
View File
@@ -69,9 +69,11 @@ public:
* @param The width of the ImageData. * @param The width of the ImageData.
* @param The height of the ImageData. * @param The height of the ImageData.
* @param The data to load into 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. * @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. * Creates new CompressedData from FileData.
+2 -2
View File
@@ -57,9 +57,9 @@ love::image::ImageData *Image::newImageData(int width, int height)
return new ImageData(width, 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) love::image::CompressedData *Image::newCompressedData(love::filesystem::FileData *data)
+1 -1
View File
@@ -48,7 +48,7 @@ public:
love::image::ImageData *newImageData(love::filesystem::FileData *data); love::image::ImageData *newImageData(love::filesystem::FileData *data);
love::image::ImageData *newImageData(int width, int height); 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); love::image::CompressedData *newCompressedData(love::filesystem::FileData *data);
+6 -2
View File
@@ -45,11 +45,15 @@ ImageData::ImageData(int width, int height)
memset(data, 0, width*height*sizeof(pixel)); 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->width = width;
this->height = height; this->height = height;
create(width, height, data);
if (own)
this->data = (unsigned char *) data;
else
create(width, height, data);
} }
ImageData::~ImageData() ImageData::~ImageData()
+1 -1
View File
@@ -38,7 +38,7 @@ public:
ImageData(love::filesystem::FileData *data); ImageData(love::filesystem::FileData *data);
ImageData(int width, int height); ImageData(int width, int height);
ImageData(int width, int height, void *data); ImageData(int width, int height, void *data, bool own);
virtual ~ImageData(); virtual ~ImageData();
// Implements image::ImageData. // Implements image::ImageData.