1. rude
  2. love

Commits

Alex Szpakowski  committed db7e0e3

Improved performance when using ImageData methods in multiple threads when ImageData:encode is called

  • Participants
  • Parent commits 3c57589
  • Branches default

Comments (0)

Files changed (6)

File src/modules/image/CompressedData.h

View file
  • Ignore whitespace
 // LOVE
 #include "common/Data.h"
 #include "common/StringMap.h"
-#include "thread/threads.h"
-
-using love::thread::Mutex;
+#include "common/int.h"
 
 // STL
 #include <vector>
 	{
 		int width, height;
 		size_t size;
-		char *data;
+		uint8 *data;
 	};
 
 	CompressedData();

File src/modules/image/ImageData.cpp

View file
  • Ignore whitespace
 
 	// If the dimensions match up, copy the entire memory stream in one go
 	if (sw == getWidth() && getWidth() == src->getWidth()
-			&& sh == getHeight() && getHeight() == src->getHeight())
+		&& sh == getHeight() && getHeight() == src->getHeight())
+	{
 		memcpy(d, s, sizeof(pixel) * sw * sh);
-	else if (sw > 0)  // Otherwise, copy each row individually
+	}
+	else if (sw > 0)
 	{
+		// Otherwise, copy each row individually.
 		for (int i = 0; i < sh; i++)
-		{
 			memcpy(d + dx + (i + dy) * getWidth(), s + sx + (i + sy) * src->getWidth(), sizeof(pixel) * sw);
-		}
 	}
 }
 

File src/modules/image/magpie/FormatHandler.h

View file
  • Ignore whitespace
 		int width, height;
 		size_t size;
 		unsigned char *data;
-		DecodedImage() : width(0), height(0), size(0), data(0) {};
+		DecodedImage() : width(0), height(0), size(0), data(0) {}
 	};
 
 	// Pixel data encoded in a particular format.
 	{
 		size_t size;
 		unsigned char *data;
-		EncodedImage() : size(0), data(0) {};
+		EncodedImage() : size(0), data(0) {}
 	};
 
 	// Lets pretend we have virtual static methods...

File src/modules/image/magpie/ImageData.cpp

View file
  • Ignore whitespace
 
 void ImageData::encode(love::filesystem::File *f, ImageData::Format format)
 {
-	thread::Lock lock(mutex);
-
-	FormatHandler::DecodedImage rawimage;
-	rawimage.width = width;
-	rawimage.height = height;
-	rawimage.size = width*height*sizeof(pixel);
-	rawimage.data = data;
-
 	FormatHandler::EncodedImage encodedimage;
 
-	try
 	{
+		// We only need to lock this mutex when actually encoding the ImageData.
+		thread::Lock lock(mutex);
+
+		FormatHandler::DecodedImage rawimage;
+		rawimage.width = width;
+		rawimage.height = height;
+		rawimage.size = width*height*sizeof(pixel);
+		rawimage.data = data;
+
 		if (DevilHandler::canEncode(format))
 			encodedimage = DevilHandler::encode(rawimage, format);
 		else
 			throw love::Exception("Image format has no suitable encoder.");
+	}
 
+	try
+	{
+		
 		f->open(love::filesystem::File::WRITE);
 		f->write(encodedimage.data, encodedimage.size);
 		f->close();

File src/modules/image/magpie/ddsHandler.cpp

View file
  • Ignore whitespace
 			mip.size = img->dataSize;
 
 			// Copy the mipmap image from the FileData.
-			mip.data = new char[mip.size];
+			mip.data = new uint8[mip.size];
 			memcpy(mip.data, img->data, mip.size);
 
 			images.push_back(mip);

File src/modules/image/wrap_ImageData.cpp

View file
  • Ignore whitespace
 	lua_pushvalue(L, 2);
 
 	// Manually lock this ImageData's mutex during the entire mapPixel.
+	// Using the lock methods because lua_error won't trigger object destructors.
 	mutex->lock();
 	int ret = lua_pcall(L, 2, 0, 0);
 	mutex->unlock();