aboutsummaryrefslogtreecommitdiffstatsci
path: root/Backends/CairoBackend/src/ImageBackend.cs
blob: 33a7dbd1084fac42128f2526887ec805d6ee3616 (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
96
97
using System;
using System.IO;
using Drawing2D;
using Glfw;

namespace Crow.CairoBackend
{
	public class DefaultBackend : CairoBackendBase {
		/// <summary>
		/// Create a new generic backend bound to the application surface
		/// </summary>
		/// <param name="width">backend surface width</param>
		/// <param name="height">backend surface height</param>
		/// <param name="nativeWindoPointer"></param>
		public DefaultBackend (int width, int height, IntPtr nativeWindoPointer)
		: base (width, height, nativeWindoPointer) {
			if (nativeWindoPointer == IntPtr.Zero) {
				Glfw3.Init ();
				Glfw3.WindowHint (WindowAttribute.ClientApi, 0);
				Glfw3.WindowHint (WindowAttribute.Resizable, 1);
				Glfw3.WindowHint (WindowAttribute.Decorated, 1);

				hWin = Glfw3.CreateWindow (width, height, "win name", MonitorHandle.Zero, IntPtr.Zero);
				if (hWin == IntPtr.Zero)
					throw new Exception ("[GLFW3] Unable to create Window");
			}
			switch (Environment.OSVersion.Platform) {
			case PlatformID.Unix:
				IntPtr disp = Glfw3.GetX11Display ();
				IntPtr nativeWin = Glfw3.GetX11Window (hWin);
				Int32 scr = Glfw3.GetX11DefaultScreen (disp);
				IntPtr visual = Glfw3.GetX11DefaultVisual (disp, scr);
				surf = new XlibSurface (disp, nativeWin, visual, width, height);
				break;
			case PlatformID.Win32NT:
			case PlatformID.Win32S:
			case PlatformID.Win32Windows:
				IntPtr hWin32 = Glfw3.GetWin32Window (hWin);
				IntPtr hdc = Glfw3.GetWin32DC (hWin32);
				surf = new Win32Surface (hdc);
				break;
			default:
				throw new PlatformNotSupportedException ("Unable to create cairo image backend.");
			}
		}
		/// <summary>
		/// Create a new offscreen backend, used in perfTests
		/// </summary>
		/// <param name="width">backend surface width</param>
		/// <param name="height">backend surface height</param>
		public DefaultBackend (int width, int height)
		: base (width, height, IntPtr.Zero) {
			surf = new ImageSurface (Format.ARGB32, width, height);
		}
		public DefaultBackend (IntPtr surfaceBitmapData, int width, int height, int stride)
		 : base (width, height, IntPtr.Zero) {
			surf = new ImageSurface (surfaceBitmapData, Format.ARGB32, width, height, stride);
		}

		public override ISurface CreateSurface(int width, int height)
			=> new ImageSurface (Format.ARGB32, width, height);
		public override ISurface CreateSurface(byte[] data, int width, int height)
			=> new ImageSurface (data, Format.ARGB32, width, height, 4 * width);
		public override IContext PrepareUIFrame(IContext existingContext, IRegion clipping)
		{
			IContext ctx = base.PrepareUIFrame (existingContext, clipping);

			clear(ctx);

			for (int i = 0; i < clipping.NumRectangles; i++)
				ctx.Rectangle (clipping.GetRectangle (i));
			ctx.Clip ();

			//ctx.PushGroup ();

			return ctx;
		}
		public override void FlushUIFrame(IContext ctx)
		{
			/*ctx.PopGroupToSource ();
			ctx.Paint ();*/
			surf.Flush ();

			base.FlushUIFrame (ctx);
		}
		public override void ResizeMainSurface(int width, int height)
		{
			if (surf is ImageSurface) {
				surf?.Dispose();
				surf = new ImageSurface (Format.ARGB32, width, height);
			} else {
				base.ResizeMainSurface(width, height);
			}
		}
	}
}