|
-
Apr 13th, 2011, 02:29 PM
#1
More graphics timing issues -Revived!!
I have now greatly altered the graphics routines I am using for drawing raceways. This relates to a series of previous threads I started on the subject, but only tangentially. Originally, I was drawing five images of five different sizes every time I redrew anything. That worked really well in most cases, but proved to be utterly unworkable in a few cases, as it meant that I was drawing some 100-200 sets of five images, and each set took up to 80ms, which meant that the total time taken to generate all the images was 8-16 seconds. Not good.
I have now changed that so that I draw only the image needed, and cache the images. If the user stays at one zoom level, the other four images will not be drawn. If the user changes the zoom level, then the current image is cached and the new zoom image is drawn. Furthermore, all zoom levels are drawn when the program starts. This has about the same performance for most uses, though it will be slightly slower if the user makes lots of changes at one zoom level, then zooms, as several of the images might have to be re-drawn in that case.
That might not mean anything to anyone, but it's also pretty much irrelevant to the question. One advantage to this design is that I can time everything pretty precisely, and that has narrowed down my issues to just a few.
The images at three of the five zoom levels take about 9ms to draw for a fairly complex image (there can be any number of items superimposed onto the base image, so that 9ms is neither the fastest, nor the slowest, for the range of possible drawing complexity, but it IS reasonable).
The image at zoom level 4 takes 13ms, and the image at zoom level 5 takes a whopping 27ms, which is three times as much time as the first three zoom levels, and twice the fourth zoom level. Obviously, that zoom level is the one for me to focus on, but the problem is probably intractable (though I might just abandon that level, which drops its time to 0).
After doing a bit of profiling, I concluded that virtually the entire issue comes down to the size of the image. At that zoom level, the base image is 256x512 pixels. This one line:
Code:
dSurface = New Bitmap(m256Base)
which takes the pre-sized base image and makes a copy of it onto which all the rest of the images will be drawn, takes about 4ms. The same step takes 1ms for zoom level 3 (which uses a base image of 128x256, so it is one quarter the size), and less than 1ms for the lower zoom levels (0-2).
That measurement is probably at the root of all of the timing. A later step rotates the image. I had a whole thread on this subject, and the only way I found to rotate the image was using this code:
Code:
grph.Dispose()
Dim d2 As Bitmap
'Now rotate the image prior to adding fish and RWPB.
If mDirection = HISInCommon.Direction.East Then
dSurface.RotateFlip(RotateFlipType.Rotate270FlipNone)
d2 = New Bitmap(lng, shrt + 16)
ElseIf mDirection = HISInCommon.Direction.West Then
dSurface.RotateFlip(RotateFlipType.Rotate90FlipNone)
d2 = New Bitmap(lng, shrt + 16)
ElseIf mDirection = HISInCommon.Direction.North Then
dSurface.RotateFlip(RotateFlipType.Rotate180FlipX)
d2 = New Bitmap(shrt, lng + 16)
Else
d2 = New Bitmap(shrt, lng + 16)
End If
grph = Drawing.Graphics.FromImage(d2)
grph.FillRectangle(mBackBrush, 0, 0, d2.Width, d2.Height)
grph.DrawImage(dSurface, 0, 0, d2.Width, d2.Height - 16)
dSurface.Dispose()
dSurface = d2
The reason for this, somewhat ugly, code can be found in this thread:
http://www.vbforums.com/showthread.php?t=642965
At the end of that, BB suggested that the hidden clipping appeared to be related to where the bitmap came from, but I have little choice in this matter, I think. I would not expect that loading the image from a file each time I need it could possibly be better than drawing it fresh.
It may be possible for me to pre-rotate the base image, though that will require some other, significant changes. Still, it might be possible. As it stands, this rotation takes no measurable time for the smallest images, but rizes to 2ms for zoom level 3, and 6ms for zoom level 4. That's nasty! It's an exponential growth, which actually does make sense, since the area of an image increases at the square of the length of one side (roughly).
The final, and most problematic issue for that largest size appears to have to do with modifying the image to make a selected version. To do that, I use some code slightly modified from what .Paul. gave me in a different thread:
Code:
Protected Function changeBrightness(ByVal outputImage As Image) As Bitmap
Dim brightness As Single = 1.25
Dim contrast As Single = 1.0F ' no change in contrast
Dim adjustedBrightness As Single = brightness - 1.0F
'create matrix that will brighten and contrast the image
Dim image_attr As New System.Drawing.Imaging.ImageAttributes
Dim cm As System.Drawing.Imaging.ColorMatrix = New System.Drawing.Imaging.ColorMatrix(New Single()() _
{ _
New Single() {contrast, 0.0, 0.0, 0.0, 0.0}, _
New Single() {0.0, contrast, 0.0, 0.0, 0.0}, _
New Single() {0.0, 0.0, contrast, 0.0, 0.0}, _
New Single() {0.0, 0.0, 0.0, 1.0, 0.0}, _
New Single() {adjustedBrightness, adjustedBrightness, adjustedBrightness, 0.0, 1.0}})
Dim rect As Rectangle = _
Rectangle.Round(outputImage.GetBounds(GraphicsUnit.Pixel))
Dim wid As Integer = outputImage.Width
Dim hgt As Integer = outputImage.Height
Dim img As New Bitmap(wid, hgt)
Dim gr As Graphics = Graphics.FromImage(img)
image_attr.SetColorMatrix(cm)
gr.DrawImage(outputImage, rect, 0, 0, wid, hgt, GraphicsUnit.Pixel, image_attr)
gr.Dispose()
Return img
End Function
That function takes only 1ms for the smaller images, rising to 2ms for zoom level 3, and an impressive 8ms for zoom level 4.
Therefore, if anybody can suggest a means to speed up any of these pieces, I would like to hear about it.
Last edited by Shaggy Hiker; Apr 20th, 2011 at 10:15 AM.
My usual boring signature: Nothing
 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|