aboutsummaryrefslogtreecommitdiffstatsci
path: root/Backends/VkvgBackend/src/Surface.cs
blob: e900478a5de552372a4ef52b051493cbfbbd70e0 (plain)
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
// Copyright (c) 2018-2021  Jean-Philippe Bruyère <jp_bruyere@hotmail.com>
//
// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)

using System;
using Drawing2D;

namespace Crow.VkvgBackend
{
	public class Surface: ISurface
	{
		internal IntPtr handle = IntPtr.Zero;
		Device vkvgDev;

		#region CTOR
		public Surface (Device device, int width, int height)
		{
			vkvgDev = device;
			if (width <= 0 || height <= 0)
				handle = NativeMethods.vkvg_surface_create (device.Handle, 1, 1);
			else
				handle = NativeMethods.vkvg_surface_create (device.Handle, (uint)width, (uint)height);
		}
		public Surface (Device device, Span<byte> data, int width, int heigth)
		{
			vkvgDev = device;
			handle = NativeMethods.vkvg_surface_create_from_bitmap (device.Handle, ref data.GetPinnableReference(), (uint)width, (uint)heigth);
		}
		public Surface (Device device, string imgPath) {
			vkvgDev = device;
			handle = NativeMethods.vkvg_surface_create_from_image (device.Handle, imgPath);
		}
		public Surface (Device device, IntPtr surfHandle) {
			vkvgDev = device;
			handle = surfHandle;
			AddReference ();
		}

		Surface (IntPtr devHandle, int width, int height)
		{
			handle = NativeMethods.vkvg_surface_create (devHandle, (uint)width, (uint)height);
		}
		#endregion
		~Surface ()
		{
			Dispose (false);
		}
		public IntPtr VkImage => NativeMethods.vkvg_surface_get_vk_image (handle);
		public int Width => NativeMethods.vkvg_surface_get_width (handle);
		public int Height => NativeMethods.vkvg_surface_get_height (handle);

		public void AddReference () => NativeMethods.vkvg_surface_reference (handle);
		public uint References () => NativeMethods.vkvg_surface_get_reference_count (handle);

		public ISurface CreateSimilar(int width, int height) => new Surface (vkvgDev, width, height);

		public void Resize(int width, int height)
		{
			NativeMethods.vkvg_surface_destroy (handle);
			handle = NativeMethods.vkvg_surface_create (vkvgDev.Handle, (uint)width, (uint)height);
		}

		public void Flush () {
			//throw new NotImplementedException ();
		}

		public void WriteToPng (string path) {
			NativeMethods.vkvg_surface_write_to_png (handle, path);
		}
		public void WriteTo (IntPtr bitmap) {
			NativeMethods.vkvg_surface_write_to_memory (handle, bitmap);
		}
		public void Clear () {
			NativeMethods.vkvg_surface_clear (handle);
		}

		#region IDisposable implementation
		public void Dispose ()
		{
			Dispose (true);
			GC.SuppressFinalize (this);
		}

		protected virtual void Dispose (bool disposing)
		{
			if (!disposing || handle == IntPtr.Zero)
				return;

			NativeMethods.vkvg_surface_destroy (handle);
			handle = IntPtr.Zero;
		}
		#endregion
	}
}