forked from cogneco/Kean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Control.cs
163 lines (157 loc) · 4.36 KB
/
Control.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//
// Control.cs
//
// Author:
// Simon Mika <smika@hx.se>
//
// Copyright (c) 2013 Simon Mika
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using Kean.Extension;
using Geometry2D = Kean.Math.Geometry2D;
using GL = OpenTK.Graphics.OpenGL.GL;
using Parallel = Kean.Parallel;
using Error = Kean.Error;
namespace Kean.Draw.OpenGL.Backend
{
public abstract class Control :
OpenTK.GLControl
{
public event Action Initialized;
public event Action<Renderer> Draw;
public new Context Context { get; private set; }
ThreadPool threadPool;
public Parallel.ThreadPool ThreadPool { get { return this.threadPool; } }
bool designMode;
protected new bool DesignMode
{
get { return this.designMode || base.DesignMode || System.Diagnostics.Process.GetCurrentProcess().ProcessName.StartsWith("devenv") || System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Designtime; }
set { this.designMode = value; }
}
#region AutoStart
[System.ComponentModel.DefaultValue(true)]
public bool AutoStart { get; set; }
#endregion
bool loaded;
protected Control(Context context) :
base()
{
this.Context = context;
}
protected Control()
{
this.AutoStart = true;
this.SuspendLayout();
//
// Viewer
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.Name = "Viewer";
this.ResumeLayout(false);
this.DesignMode = this.DesignMode;
}
public bool Initialize()
{
bool result;
if (result = !this.DesignMode && !this.loaded)
{
OpenTK.Platform.IWindowInfo window;
try
{
this.loaded = true;
if (!this.IsHandleCreated)
this.CreateHandle();
window = this.WindowInfo;
}
catch
{
window = null;
this.loaded = false;
}
if (result = this.loaded)
{
Backend.Context.Current = this.Context = this.Context ?? Backend.Context.Current ?? this.CreateContext();
this.threadPool = this.CreateThreadPool("OpenGL", 8);
this.SetupViewport();
this.Initialized.Call();
}
}
return result;
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
base.OnPaint(e);
Error.Log.Wrap(() =>
{
if (!this.DesignMode)
{
if (!this.loaded && this.AutoStart)
this.Initialize();
if (this.loaded)
{
this.Clear();
this.Draw.Call(this.CreateRenderer());
this.SwapBuffers();
}
}
})();
}
protected abstract Context CreateContext();
protected abstract Renderer CreateRenderer();
protected abstract ThreadPool CreateThreadPool(string name, int workers);
protected abstract void SetupViewport();
protected abstract void Clear();
public Draw.Image Read()
{
Draw.Image result;
Renderer renderer = this.CreateRenderer();
using (Surface surface = new Surface(renderer))
{
surface.Use();
result = surface.Read();
surface.Unuse();
}
return result;
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
Error.Log.Wrap(() =>
{
if (this.loaded)
this.SetupViewport();
})();
}
protected override void Dispose(bool disposing)
{
if (this.threadPool.NotNull())
{
this.threadPool.Delete();
this.threadPool = null;
}
if (this.Context.NotNull()) // TODO: only dispose context if this is the last OpenGL control in the current context.
{
this.Context.Dispose();
Backend.Context.Current = this.Context = null;
}
base.Dispose(disposing);
}
public static Control Create()
{
// TODO: select OpenGL implementation
return new OpenGL21.Control();
}
}
}