jingwood / d2dlib

A .NET library for hardware-accelerated, high performance, immediate mode rendering via Direct2D.
MIT License
234 stars 40 forks source link

Adding multiple D2DControls is too slow #51

Closed MineEric64 closed 3 years ago

MineEric64 commented 3 years ago

Hi, I'm trying to add multiple (a hundred) d2dcontrols in my form.

but it's too slow. It took 4100ms for adding D2DControl and my custom control D2DButton. But it just took 100ms for adding Button class. (in System.Windows.Forms)

Why it is too slow? Also I used suspend & resume layout, but it didn't work for me.

I tested in this code (vb.net):

Public Sub TestOnButton(testCount As Integer, useLayout As Boolean)
        Dim rand As New Random()

        If useLayout Then
            Me.SuspendLayout()
        End If

        For i = 1 To testCount
            Dim btn As New TestButton() With {
                    .Text = i.ToString(),
                    .Location = New Point(rand.Next(0, Me.Width), rand.Next(0, Me.Height))
                    }

            Me.Controls.Add(btn)
        Next

        If useLayout Then
            Me.ResumeLayout()
        End If
    End Sub
Imports TestButton = unvell.D2DLib.WinForm.D2DControl
TestOnButton(100, False)

4100ms elapsed

Imports TestButton = System.Windows.Forms.Button
TestOnButton(100, False)

100ms elapsed

thanks.

MineEric64 commented 3 years ago

also, I tested one more thing.

Imports TestButton = unvell.D2DLib.WinForm.D2DControl
TestOnButton(1, False)

130ms elapsed

Imports TestButton = System.Windows.Forms.Button
TestOnButton(1, False)

1ms elapsed

jingwood commented 3 years ago

I don't think it is a good idea to create many D2DControl instances and add them onto a Windows Form. The D2DControl is designed to individually hold a hardware associated handler (device), which is expensive. In this case, you might not able to get any performance benefit.

I am not sure what is your final goal, but if you can create a single D2DForm window and your own button components to handle the user's mouse event, such as click or hover, I think that would be a better implementation.

MineEric64 commented 3 years ago

oh I got it. thanks for reply this issue :)

MineEric64 commented 3 years ago

I fixed this issue using d2d form instead of winform.

thanks!!