1 module skia.GRBackendRenderTarget;
2 
3 import skia.SKObject;
4 import skia.SkiaApi;
5 import skia.Definitions;
6 import skia.MathTypes;
7 import skia.EnumMappings;
8 import skia.GRDefinitions;
9 import skia.Exceptions;
10 import skia.GRDefinitions;
11 
12 
13 class GRBackendRenderTarget : SKObject, ISKSkipObjectRegistration
14 {
15 	this (void* handle, bool owns)
16 	{
17     super (handle, owns);
18 	}
19 
20 	this (GRBackend backend, GRBackendRenderTargetDesc desc)
21 	{
22     this (null, true);
23 		switch (backend) {
24 			case GRBackend.Metal:
25 				throw new NotSupportedException ();
26 			case GRBackend.OpenGL:
27 			GRGlFramebufferInfo glInfo = GRGlFramebufferInfo(cast(uint)desc.RenderTargetHandle, desc.Config.ToGlSizedFormat ());
28 				// auto glInfo = new GRGlFramebufferInfo (cast(uint)desc.RenderTargetHandle, desc.Config.ToGlSizedFormat ());
29 				CreateGl (desc.Width, desc.Height, desc.SampleCount, desc.StencilBits, glInfo);
30 				break;
31 			case GRBackend.Vulkan:
32 				throw new NotSupportedException ();
33 			case GRBackend.Dawn:
34 				throw new NotSupportedException ();
35 			default:
36 				throw new ArgumentOutOfRangeException (backend.stringof);
37 		}
38 	}
39 
40 	this (int width, int height, int sampleCount, int stencilBits, GRGlFramebufferInfo glInfo)
41 	{
42     this (null, true);
43 		CreateGl (width, height, sampleCount, stencilBits, glInfo);
44 	}
45 
46 	this (int width, int height, int sampleCount, GRVkImageInfo vkImageInfo)
47 	{
48     this (null, true);
49 		CreateVulkan (width, height, sampleCount, vkImageInfo);
50 	}
51 
52 	private void CreateGl (int width, int height, int sampleCount, int stencilBits, GRGlFramebufferInfo glInfo)
53 	{
54 		Handle = SkiaApi.gr_backendrendertarget_new_gl (width, height, sampleCount, stencilBits, &glInfo);
55 
56 		if (Handle is null) {
57 			throw new InvalidOperationException ("Unable to create a new GRBackendRenderTarget instance.");
58 		}
59 	}
60 
61 	private void CreateVulkan (int width, int height, int sampleCount, GRVkImageInfo vkImageInfo)
62 	{
63 		Handle = SkiaApi.gr_backendrendertarget_new_vulkan (width, height, sampleCount, &vkImageInfo);
64 
65 		if (Handle is null) {
66 			throw new InvalidOperationException ("Unable to create a new GRBackendRenderTarget instance.");
67 		}
68 	}
69 
70 	protected override void Dispose (bool disposing)
71   {
72     return super.Dispose (disposing);
73   }
74 		
75 
76 	protected override void DisposeNative ()
77   {
78     return SkiaApi.gr_backendrendertarget_delete (cast(gr_backendrendertarget_t*)Handle);
79   }
80 		
81 
82 	bool IsValid()
83   {
84     return SkiaApi.gr_backendrendertarget_is_valid (cast(gr_backendrendertarget_t*)Handle);
85   }
86   
87 
88 	int Width()
89   {
90     return SkiaApi.gr_backendrendertarget_get_width (cast(gr_backendrendertarget_t*)Handle);
91   }
92   
93 
94 	int Height()
95   {
96     return SkiaApi.gr_backendrendertarget_get_height (cast(gr_backendrendertarget_t*)Handle);
97   }
98   
99 
100 	int SampleCount()
101   {
102     return  SkiaApi.gr_backendrendertarget_get_samples (cast(gr_backendrendertarget_t*)Handle);
103   }
104  
105 
106 	int StencilBits()
107   {
108     return SkiaApi.gr_backendrendertarget_get_stencils (cast(gr_backendrendertarget_t*)Handle);
109   }
110   
111 
112 	GRBackend Backend()
113   {
114     return SkiaApi.gr_backendrendertarget_get_backend (cast(gr_backendrendertarget_t*)Handle).FromNative ();
115   }
116    
117 
118 	SKSizeI Size()
119   {
120     return SKSizeI (Width, Height);
121   }
122   
123 
124 	SKRectI Rect()
125   {
126     return SKRectI (0, 0, Width, Height);
127   }
128   
129 
130 	GRGlFramebufferInfo GetGlFramebufferInfo ()
131   {
132 	  GRGlFramebufferInfo info;
133     return GetGlFramebufferInfo (info) ? info : GRGlFramebufferInfo(0);
134   }
135 		
136 
137 	bool GetGlFramebufferInfo (ref GRGlFramebufferInfo glInfo)
138 	{
139 		GRGlFramebufferInfo* g = &glInfo;
140 		return SkiaApi.gr_backendrendertarget_get_gl_framebufferinfo (cast(gr_backendrendertarget_t*)Handle, g);
141 	}
142 }