forked from cogneco/Kean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Texture.cs
167 lines (163 loc) · 4.68 KB
/
Texture.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
164
165
166
167
//
// Texture.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 Kean.Collection.Extension;
using System;
using Collection = Kean.Collection;
using Error = Kean.Error;
using GL = OpenTK.Graphics.OpenGL.GL;
using Geometry2D = Kean.Math.Geometry2D;
using Raster = Kean.Draw.Raster;
using Kean.Extension;
namespace Kean.Draw.OpenGL.Backend
{
public abstract class Texture :
Resource,
ITexture,
IData
{
int identifier;
internal int Identifier
{
get { return this.identifier; }
private set { this.identifier = value; }
}
public Geometry2D.Integer.Size Size { get; protected set; }
public TextureType Type { get; protected set; }
public abstract bool Wrap { get; set; }
Composition composition;
public Composition Composition
{
get
{
if (this.composition.IsNull())
this.composition = this.Context.CreateComposition(this);
return this.composition;
}
internal set { this.composition = value; }
}
protected Texture(Context context) :
base(context)
{
this.Identifier = this.CreateIdentifier();
}
protected Texture(Texture original) :
base(original)
{
this.Identifier = original.Identifier;
original.Identifier = 0;
this.Size = original.Size;
this.Type = original.Type;
}
protected override void Dispose(bool disposing)
{
if (this.Context.NotNull())
{
if (this.composition.NotNull()) // If composition exists recycle as composition.
this.composition.Dispose();
else
this.Context.Recycle(this.Refurbish());
}
}
public void Create(TextureType type, Geometry2D.Integer.Size size)
{
this.SetFormat(type, size);
this.Use();
this.Create(IntPtr.Zero);
this.UnUse();
}
public void Create(Raster.Image image)
{
this.SetFormat(image);
this.Use();
this.Create(image.Pointer);
this.UnUse();
}
public void Load(Raster.Image image, Geometry2D.Integer.Point offset)
{
TextureType type = this.Context.GetTextureType(image);
this.Use();
this.Load(image.Pointer, new Geometry2D.Integer.Box(offset, image.Size), type);
this.UnUse();
}
public Raster.Image Read()
{
Raster.Image result = null;
switch (this.Type)
{
default:
case TextureType.Rgba:
result = new Raster.Bgra(this.Size);
break;
case TextureType.Rgb:
result = new Raster.Bgr(this.Size);
break;
case TextureType.Monochrome:
result = new Raster.Monochrome(this.Size);
break;
}
this.Use();
this.Read(result.Pointer);
this.UnUse();
return result;
}
public void Flush()
{
this.Context.Flush();
}
public bool Finish()
{
return this.Context.Finish();
}
public abstract void Render(Geometry2D.Single.Point leftTop, Geometry2D.Single.Point rightTop, Geometry2D.Single.Point leftBottom, Geometry2D.Single.Point rightBottom, Geometry2D.Single.Box rectangle);
public override string ToString()
{
return this.Identifier.ToString();
}
void SetFormat(Raster.Image image)
{
TextureType type;
if (image is Raster.Bgra)
type = TextureType.Rgba;
else if (image is Raster.Bgr)
type = TextureType.Rgb;
else
type = TextureType.Monochrome;
this.SetFormat(type, image.Size);
}
#region Implementors Interface
protected abstract int CreateIdentifier();
public abstract void Use();
public abstract void UnUse();
protected abstract void SetFormat(TextureType type, Geometry2D.Integer.Size size);
protected abstract void Create(IntPtr data);
protected abstract void Load(IntPtr data, Geometry2D.Integer.Box region, TextureType type);
protected abstract void Read(IntPtr data);
public abstract void CopyFromTarget();
public abstract void CopyFromTarget(Geometry2D.Integer.Size offset);
protected internal abstract Texture Refurbish();
protected internal override void Delete()
{
this.Identifier = 0;
base.Delete();
}
#endregion
}
}