Properly dispose of all GDI+ entities used in VectorRenderModule for dynamic textures.

The convention is that if an object implements IDiposable() the code must explicitly call Dispose() or call it via the using statement.
This may be particularly important for GDI+ objects since they encapsulate native code entities.
0.7.3-extended
Justin Clark-Casey (justincc) 2012-08-03 00:00:54 +01:00
parent 0e9e60d55a
commit f35e94f168
1 changed files with 322 additions and 259 deletions

View File

@ -98,16 +98,18 @@ namespace OpenSim.Region.CoreModules.Scripting.VectorRender
public void GetDrawStringSize(string text, string fontName, int fontSize, public void GetDrawStringSize(string text, string fontName, int fontSize,
out double xSize, out double ySize) out double xSize, out double ySize)
{ {
Font myFont = new Font(fontName, fontSize); using (Font myFont = new Font(fontName, fontSize))
SizeF stringSize = new SizeF(); {
lock (m_graph) { SizeF stringSize = new SizeF();
stringSize = m_graph.MeasureString(text, myFont); lock (m_graph)
xSize = stringSize.Width; {
ySize = stringSize.Height; stringSize = m_graph.MeasureString(text, myFont);
xSize = stringSize.Width;
ySize = stringSize.Height;
}
} }
} }
#endregion #endregion
#region IRegionModule Members #region IRegionModule Members
@ -121,6 +123,8 @@ namespace OpenSim.Region.CoreModules.Scripting.VectorRender
if (m_graph == null) if (m_graph == null)
{ {
// We won't dispose of these explicitly since this module is only removed when the entire simulator
// is shut down.
Bitmap bitmap = new Bitmap(1024, 1024, PixelFormat.Format32bppArgb); Bitmap bitmap = new Bitmap(1024, 1024, PixelFormat.Format32bppArgb);
m_graph = Graphics.FromImage(bitmap); m_graph = Graphics.FromImage(bitmap);
} }
@ -299,53 +303,64 @@ namespace OpenSim.Region.CoreModules.Scripting.VectorRender
} }
} }
Bitmap bitmap; Bitmap bitmap = null;
Graphics graph = null;
if (alpha == 256)
{
bitmap = new Bitmap(width, height, PixelFormat.Format32bppRgb);
}
else
{
bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
}
Graphics graph = Graphics.FromImage(bitmap);
// this is really just to save people filling the
// background color in their scripts, only do when fully opaque
if (alpha >= 255)
{
graph.FillRectangle(new SolidBrush(bgColor), 0, 0, width, height);
}
for (int w = 0; w < bitmap.Width; w++)
{
if (alpha <= 255)
{
for (int h = 0; h < bitmap.Height; h++)
{
bitmap.SetPixel(w, h, Color.FromArgb(alpha, bitmap.GetPixel(w, h)));
}
}
}
GDIDraw(data, graph, altDataDelim);
byte[] imageJ2000 = new byte[0];
try try
{ {
imageJ2000 = OpenJPEG.EncodeFromImage(bitmap, true); if (alpha == 256)
} bitmap = new Bitmap(width, height, PixelFormat.Format32bppRgb);
catch (Exception e) else
{ bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
m_log.ErrorFormat(
"[VECTORRENDERMODULE]: OpenJpeg Encode Failed. Exception {0}{1}",
e.Message, e.StackTrace);
}
m_textureManager.ReturnData(id, imageJ2000); graph = Graphics.FromImage(bitmap);
// this is really just to save people filling the
// background color in their scripts, only do when fully opaque
if (alpha >= 255)
{
using (SolidBrush bgFillBrush = new SolidBrush(bgColor))
{
graph.FillRectangle(bgFillBrush, 0, 0, width, height);
}
}
for (int w = 0; w < bitmap.Width; w++)
{
if (alpha <= 255)
{
for (int h = 0; h < bitmap.Height; h++)
{
bitmap.SetPixel(w, h, Color.FromArgb(alpha, bitmap.GetPixel(w, h)));
}
}
}
GDIDraw(data, graph, altDataDelim);
byte[] imageJ2000 = new byte[0];
try
{
imageJ2000 = OpenJPEG.EncodeFromImage(bitmap, true);
}
catch (Exception e)
{
m_log.ErrorFormat(
"[VECTORRENDERMODULE]: OpenJpeg Encode Failed. Exception {0}{1}",
e.Message, e.StackTrace);
}
m_textureManager.ReturnData(id, imageJ2000);
}
finally
{
if (graph != null)
graph.Dispose();
if (bitmap != null)
bitmap.Dispose();
}
} }
private int parseIntParam(string strInt) private int parseIntParam(string strInt)
@ -407,237 +422,284 @@ namespace OpenSim.Region.CoreModules.Scripting.VectorRender
{ {
Point startPoint = new Point(0, 0); Point startPoint = new Point(0, 0);
Point endPoint = new Point(0, 0); Point endPoint = new Point(0, 0);
Pen drawPen = new Pen(Color.Black, 7); Pen drawPen = null;
string fontName = m_fontName; Font myFont = null;
float fontSize = 14; SolidBrush myBrush = null;
Font myFont = new Font(fontName, fontSize);
SolidBrush myBrush = new SolidBrush(Color.Black);
char[] lineDelimiter = {dataDelim}; try
char[] partsDelimiter = {','};
string[] lines = data.Split(lineDelimiter);
foreach (string line in lines)
{ {
string nextLine = line.Trim(); drawPen = new Pen(Color.Black, 7);
//replace with switch, or even better, do some proper parsing string fontName = m_fontName;
if (nextLine.StartsWith("MoveTo")) float fontSize = 14;
myFont = new Font(fontName, fontSize);
myBrush = new SolidBrush(Color.Black);
char[] lineDelimiter = {dataDelim};
char[] partsDelimiter = {','};
string[] lines = data.Split(lineDelimiter);
foreach (string line in lines)
{ {
float x = 0; string nextLine = line.Trim();
float y = 0; //replace with switch, or even better, do some proper parsing
GetParams(partsDelimiter, ref nextLine, 6, ref x, ref y); if (nextLine.StartsWith("MoveTo"))
startPoint.X = (int) x;
startPoint.Y = (int) y;
}
else if (nextLine.StartsWith("LineTo"))
{
float x = 0;
float y = 0;
GetParams(partsDelimiter, ref nextLine, 6, ref x, ref y);
endPoint.X = (int) x;
endPoint.Y = (int) y;
graph.DrawLine(drawPen, startPoint, endPoint);
startPoint.X = endPoint.X;
startPoint.Y = endPoint.Y;
}
else if (nextLine.StartsWith("Text"))
{
nextLine = nextLine.Remove(0, 4);
nextLine = nextLine.Trim();
graph.DrawString(nextLine, myFont, myBrush, startPoint);
}
else if (nextLine.StartsWith("Image"))
{
float x = 0;
float y = 0;
GetParams(partsDelimiter, ref nextLine, 5, ref x, ref y);
endPoint.X = (int) x;
endPoint.Y = (int) y;
Image image = ImageHttpRequest(nextLine);
if (image != null)
{ {
graph.DrawImage(image, (float)startPoint.X, (float)startPoint.Y, x, y); float x = 0;
float y = 0;
GetParams(partsDelimiter, ref nextLine, 6, ref x, ref y);
startPoint.X = (int) x;
startPoint.Y = (int) y;
} }
else else if (nextLine.StartsWith("LineTo"))
{ {
graph.DrawString("URL couldn't be resolved or is", new Font(m_fontName,6), float x = 0;
myBrush, startPoint); float y = 0;
graph.DrawString("not an image. Please check URL.", new Font(m_fontName, 6), GetParams(partsDelimiter, ref nextLine, 6, ref x, ref y);
myBrush, new Point(startPoint.X, 12 + startPoint.Y)); endPoint.X = (int) x;
endPoint.Y = (int) y;
graph.DrawLine(drawPen, startPoint, endPoint);
startPoint.X = endPoint.X;
startPoint.Y = endPoint.Y;
}
else if (nextLine.StartsWith("Text"))
{
nextLine = nextLine.Remove(0, 4);
nextLine = nextLine.Trim();
graph.DrawString(nextLine, myFont, myBrush, startPoint);
}
else if (nextLine.StartsWith("Image"))
{
float x = 0;
float y = 0;
GetParams(partsDelimiter, ref nextLine, 5, ref x, ref y);
endPoint.X = (int) x;
endPoint.Y = (int) y;
using (Image image = ImageHttpRequest(nextLine))
{
if (image != null)
{
graph.DrawImage(image, (float)startPoint.X, (float)startPoint.Y, x, y);
}
else
{
using (Font errorFont = new Font(m_fontName,6))
{
graph.DrawString("URL couldn't be resolved or is", errorFont,
myBrush, startPoint);
graph.DrawString("not an image. Please check URL.", errorFont,
myBrush, new Point(startPoint.X, 12 + startPoint.Y));
}
graph.DrawRectangle(drawPen, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
}
}
startPoint.X += endPoint.X;
startPoint.Y += endPoint.Y;
}
else if (nextLine.StartsWith("Rectangle"))
{
float x = 0;
float y = 0;
GetParams(partsDelimiter, ref nextLine, 9, ref x, ref y);
endPoint.X = (int) x;
endPoint.Y = (int) y;
graph.DrawRectangle(drawPen, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y); graph.DrawRectangle(drawPen, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
startPoint.X += endPoint.X;
startPoint.Y += endPoint.Y;
} }
startPoint.X += endPoint.X; else if (nextLine.StartsWith("FillRectangle"))
startPoint.Y += endPoint.Y;
}
else if (nextLine.StartsWith("Rectangle"))
{
float x = 0;
float y = 0;
GetParams(partsDelimiter, ref nextLine, 9, ref x, ref y);
endPoint.X = (int) x;
endPoint.Y = (int) y;
graph.DrawRectangle(drawPen, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
startPoint.X += endPoint.X;
startPoint.Y += endPoint.Y;
}
else if (nextLine.StartsWith("FillRectangle"))
{
float x = 0;
float y = 0;
GetParams(partsDelimiter, ref nextLine, 13, ref x, ref y);
endPoint.X = (int) x;
endPoint.Y = (int) y;
graph.FillRectangle(myBrush, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
startPoint.X += endPoint.X;
startPoint.Y += endPoint.Y;
}
else if (nextLine.StartsWith("FillPolygon"))
{
PointF[] points = null;
GetParams(partsDelimiter, ref nextLine, 11, ref points);
graph.FillPolygon(myBrush, points);
}
else if (nextLine.StartsWith("Polygon"))
{
PointF[] points = null;
GetParams(partsDelimiter, ref nextLine, 7, ref points);
graph.DrawPolygon(drawPen, points);
}
else if (nextLine.StartsWith("Ellipse"))
{
float x = 0;
float y = 0;
GetParams(partsDelimiter, ref nextLine, 7, ref x, ref y);
endPoint.X = (int)x;
endPoint.Y = (int)y;
graph.DrawEllipse(drawPen, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
startPoint.X += endPoint.X;
startPoint.Y += endPoint.Y;
}
else if (nextLine.StartsWith("FontSize"))
{
nextLine = nextLine.Remove(0, 8);
nextLine = nextLine.Trim();
fontSize = Convert.ToSingle(nextLine, CultureInfo.InvariantCulture);
myFont = new Font(fontName, fontSize);
}
else if (nextLine.StartsWith("FontProp"))
{
nextLine = nextLine.Remove(0, 8);
nextLine = nextLine.Trim();
string[] fprops = nextLine.Split(partsDelimiter);
foreach (string prop in fprops)
{ {
float x = 0;
float y = 0;
GetParams(partsDelimiter, ref nextLine, 13, ref x, ref y);
endPoint.X = (int) x;
endPoint.Y = (int) y;
graph.FillRectangle(myBrush, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
startPoint.X += endPoint.X;
startPoint.Y += endPoint.Y;
}
else if (nextLine.StartsWith("FillPolygon"))
{
PointF[] points = null;
GetParams(partsDelimiter, ref nextLine, 11, ref points);
graph.FillPolygon(myBrush, points);
}
else if (nextLine.StartsWith("Polygon"))
{
PointF[] points = null;
GetParams(partsDelimiter, ref nextLine, 7, ref points);
graph.DrawPolygon(drawPen, points);
}
else if (nextLine.StartsWith("Ellipse"))
{
float x = 0;
float y = 0;
GetParams(partsDelimiter, ref nextLine, 7, ref x, ref y);
endPoint.X = (int)x;
endPoint.Y = (int)y;
graph.DrawEllipse(drawPen, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
startPoint.X += endPoint.X;
startPoint.Y += endPoint.Y;
}
else if (nextLine.StartsWith("FontSize"))
{
nextLine = nextLine.Remove(0, 8);
nextLine = nextLine.Trim();
fontSize = Convert.ToSingle(nextLine, CultureInfo.InvariantCulture);
switch (prop) myFont.Dispose();
myFont = new Font(fontName, fontSize);
}
else if (nextLine.StartsWith("FontProp"))
{
nextLine = nextLine.Remove(0, 8);
nextLine = nextLine.Trim();
string[] fprops = nextLine.Split(partsDelimiter);
foreach (string prop in fprops)
{ {
case "B":
if (!(myFont.Bold)) switch (prop)
myFont = new Font(myFont, myFont.Style | FontStyle.Bold); {
break; case "B":
case "I": if (!(myFont.Bold))
if (!(myFont.Italic)) {
myFont = new Font(myFont, myFont.Style | FontStyle.Italic); Font newFont = new Font(myFont, myFont.Style | FontStyle.Bold);
break; myFont.Dispose();
case "U": myFont = newFont;
if (!(myFont.Underline)) }
myFont = new Font(myFont, myFont.Style | FontStyle.Underline); break;
break; case "I":
case "S": if (!(myFont.Italic))
if (!(myFont.Strikeout)) {
myFont = new Font(myFont, myFont.Style | FontStyle.Strikeout); Font newFont = new Font(myFont, myFont.Style | FontStyle.Italic);
break; myFont.Dispose();
case "R": myFont = newFont;
myFont = new Font(myFont, FontStyle.Regular); }
break; break;
case "U":
if (!(myFont.Underline))
{
Font newFont = new Font(myFont, myFont.Style | FontStyle.Underline);
myFont.Dispose();
myFont = newFont;
}
break;
case "S":
if (!(myFont.Strikeout))
{
Font newFont = new Font(myFont, myFont.Style | FontStyle.Strikeout);
myFont.Dispose();
myFont = newFont;
}
break;
case "R":
Font newFont = new Font(myFont, FontStyle.Regular);
myFont.Dispose();
myFont = newFont;
break;
}
} }
} }
} else if (nextLine.StartsWith("FontName"))
else if (nextLine.StartsWith("FontName"))
{
nextLine = nextLine.Remove(0, 8);
fontName = nextLine.Trim();
myFont = new Font(fontName, fontSize);
}
else if (nextLine.StartsWith("PenSize"))
{
nextLine = nextLine.Remove(0, 7);
nextLine = nextLine.Trim();
float size = Convert.ToSingle(nextLine, CultureInfo.InvariantCulture);
drawPen.Width = size;
}
else if (nextLine.StartsWith("PenCap"))
{
bool start = true, end = true;
nextLine = nextLine.Remove(0, 6);
nextLine = nextLine.Trim();
string[] cap = nextLine.Split(partsDelimiter);
if (cap[0].ToLower() == "start")
end = false;
else if (cap[0].ToLower() == "end")
start = false;
else if (cap[0].ToLower() != "both")
return;
string type = cap[1].ToLower();
if (end)
{ {
switch (type) nextLine = nextLine.Remove(0, 8);
fontName = nextLine.Trim();
myFont.Dispose();
myFont = new Font(fontName, fontSize);
}
else if (nextLine.StartsWith("PenSize"))
{
nextLine = nextLine.Remove(0, 7);
nextLine = nextLine.Trim();
float size = Convert.ToSingle(nextLine, CultureInfo.InvariantCulture);
drawPen.Width = size;
}
else if (nextLine.StartsWith("PenCap"))
{
bool start = true, end = true;
nextLine = nextLine.Remove(0, 6);
nextLine = nextLine.Trim();
string[] cap = nextLine.Split(partsDelimiter);
if (cap[0].ToLower() == "start")
end = false;
else if (cap[0].ToLower() == "end")
start = false;
else if (cap[0].ToLower() != "both")
return;
string type = cap[1].ToLower();
if (end)
{ {
case "arrow": switch (type)
drawPen.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor; {
break; case "arrow":
case "round": drawPen.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
drawPen.EndCap = System.Drawing.Drawing2D.LineCap.RoundAnchor; break;
break; case "round":
case "diamond": drawPen.EndCap = System.Drawing.Drawing2D.LineCap.RoundAnchor;
drawPen.EndCap = System.Drawing.Drawing2D.LineCap.DiamondAnchor; break;
break; case "diamond":
case "flat": drawPen.EndCap = System.Drawing.Drawing2D.LineCap.DiamondAnchor;
drawPen.EndCap = System.Drawing.Drawing2D.LineCap.Flat; break;
break; case "flat":
drawPen.EndCap = System.Drawing.Drawing2D.LineCap.Flat;
break;
}
}
if (start)
{
switch (type)
{
case "arrow":
drawPen.StartCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
break;
case "round":
drawPen.StartCap = System.Drawing.Drawing2D.LineCap.RoundAnchor;
break;
case "diamond":
drawPen.StartCap = System.Drawing.Drawing2D.LineCap.DiamondAnchor;
break;
case "flat":
drawPen.StartCap = System.Drawing.Drawing2D.LineCap.Flat;
break;
}
} }
} }
if (start) else if (nextLine.StartsWith("PenColour") || nextLine.StartsWith("PenColor"))
{ {
switch (type) nextLine = nextLine.Remove(0, 9);
nextLine = nextLine.Trim();
int hex = 0;
Color newColor;
if (Int32.TryParse(nextLine, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out hex))
{ {
case "arrow": newColor = Color.FromArgb(hex);
drawPen.StartCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
break;
case "round":
drawPen.StartCap = System.Drawing.Drawing2D.LineCap.RoundAnchor;
break;
case "diamond":
drawPen.StartCap = System.Drawing.Drawing2D.LineCap.DiamondAnchor;
break;
case "flat":
drawPen.StartCap = System.Drawing.Drawing2D.LineCap.Flat;
break;
} }
else
{
// this doesn't fail, it just returns black if nothing is found
newColor = Color.FromName(nextLine);
}
myBrush.Color = newColor;
drawPen.Color = newColor;
} }
} }
else if (nextLine.StartsWith("PenColour") || nextLine.StartsWith("PenColor")) }
{ finally
nextLine = nextLine.Remove(0, 9); {
nextLine = nextLine.Trim(); if (drawPen != null)
int hex = 0; drawPen.Dispose();
Color newColor; if (myFont != null)
if (Int32.TryParse(nextLine, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out hex)) myFont.Dispose();
{
newColor = Color.FromArgb(hex);
}
else
{
// this doesn't fail, it just returns black if nothing is found
newColor = Color.FromName(nextLine);
}
myBrush.Color = newColor; if (myBrush != null)
drawPen.Color = newColor; myBrush.Dispose();
}
} }
} }
@ -691,7 +753,7 @@ namespace OpenSim.Region.CoreModules.Scripting.VectorRender
{ {
try try
{ {
WebRequest request = HttpWebRequest.Create(url); WebRequest request = HttpWebRequest.Create(url);
//Ckrinke: Comment out for now as 'str' is unused. Bring it back into play later when it is used. //Ckrinke: Comment out for now as 'str' is unused. Bring it back into play later when it is used.
//Ckrinke Stream str = null; //Ckrinke Stream str = null;
HttpWebResponse response = (HttpWebResponse)(request).GetResponse(); HttpWebResponse response = (HttpWebResponse)(request).GetResponse();
@ -702,6 +764,7 @@ namespace OpenSim.Region.CoreModules.Scripting.VectorRender
} }
} }
catch { } catch { }
return null; return null;
} }
} }