1 module skia.GRBackendTexture;
2 
3 import skia.Definitions;
4 import skia.GRDefinitions;
5 import skia.EnumMappings;
6 import skia.Exceptions;
7 import skia.MathTypes;
8 import skia.SKObject;
9 import skia.SkiaApi;
10 import skia.Exceptions;
11 
12 class GRBackendTexture : SKObject, ISKSkipObjectRegistration {
13 	this(void* handle, bool owns) {
14 		super(handle, owns);
15 	}
16 
17 	this(GRGlBackendTextureDesc desc) {
18 		this(null, true);
19 		auto handle = desc.TextureHandle;
20 		if (handle.Format == 0) {
21 			handle.Format = desc.Config.ToGlSizedFormat();
22 		}
23 		CreateGl(desc.Width, desc.Height, false, handle);
24 	}
25 
26 	this (GRBackendTextureDesc desc)
27 	{
28 		this (null, true);
29 	// 	GRBackendObject handlePtr = desc.TextureHandle;
30 	// 	// auto oldHandle = Marshal.PtrToStructure<GRTextureInfoObsolete> (handlePtr);
31 	// 	GRTextureInfoObsolete oldHandle = cast(GRTextureInfoObsolete)(handlePtr);
32 
33 	// 	auto handle = new GRGlTextureInfo (oldHandle.fTarget, oldHandle.fID, desc.Config.ToGlSizedFormat ());
34 	// 	CreateGl (desc.Width, desc.Height, false, handle);
35 	}
36 
37 	this(int width, int height, bool mipmapped, GRGlTextureInfo glInfo) {
38 		this(null, true);
39 		CreateGl(width, height, mipmapped, glInfo);
40 	}
41 
42 	this(int width, int height, GRVkImageInfo vkInfo) {
43 		this(null, true);
44 		CreateVulkan(width, height, vkInfo);
45 	}
46 
47 	private void CreateGl(int width, int height, bool mipmapped, GRGlTextureInfo glInfo) {
48 		Handle = SkiaApi.gr_backendtexture_new_gl(width, height, mipmapped, &glInfo);
49 
50 		if (Handle is null) {
51 			throw new InvalidOperationException("Unable to create a new GRBackendTexture instance.");
52 		}
53 	}
54 
55 	private void CreateVulkan(int width, int height, GRVkImageInfo vkInfo) {
56 		Handle = SkiaApi.gr_backendtexture_new_vulkan(width, height, &vkInfo);
57 
58 		if (Handle is null) {
59 			throw new InvalidOperationException("Unable to create a new GRBackendTexture instance.");
60 		}
61 	}
62 
63 	protected override void Dispose(bool disposing) {
64 		return super.Dispose(disposing);
65 	}
66 
67 	protected override void DisposeNative() {
68 		return SkiaApi.gr_backendtexture_delete(cast(gr_backendtexture_t*)Handle);
69 	}
70 
71 	bool IsValid() {
72 		return SkiaApi.gr_backendtexture_is_valid(cast(gr_backendtexture_t*)Handle);
73 	}
74 
75 	int Width() {
76 		return SkiaApi.gr_backendtexture_get_width(cast(gr_backendtexture_t*)Handle);
77 	}
78 
79 	int Height() {
80 		return SkiaApi.gr_backendtexture_get_height(cast(gr_backendtexture_t*)Handle);
81 	}
82 
83 	bool HasMipMaps() {
84 		return SkiaApi.gr_backendtexture_has_mipmaps(cast(gr_backendtexture_t*)Handle);
85 	}
86 
87 	GRBackend Backend() {
88 		return SkiaApi.gr_backendtexture_get_backend(cast(gr_backendtexture_t*)Handle).FromNative();
89 	}
90 
91 	SKSizeI Size() {
92 		return SKSizeI(Width, Height);
93 	}
94 
95 	SKRectI Rect() {
96 		return SKRectI(0, 0, Width, Height);
97 	}
98 
99 	//   GRGlTextureInfo GetGlTextureInfo ()
100 	//   {
101 	//     var info;
102 	//     return 	GetGlTextureInfo ( info) ? info : default;
103 	//   }
104 
105 	bool GetGlTextureInfo(out GRGlTextureInfo glInfo) {
106 		GRGlTextureInfo* g = &glInfo;
107 		return SkiaApi.gr_backendtexture_get_gl_textureinfo(cast(gr_backendtexture_t*)Handle, g);
108 	}
109 
110 	struct GRTextureInfoObsolete {
111 		uint fTarget;
112 		uint fID;
113 	}
114 }