1 module skia.GRContext; 2 3 import skia.SKObject; 4 import skia.SkiaApi; 5 import skia.Definitions; 6 import skia.GRGlInterface; 7 import skia.GRVkBackendContext; 8 import skia.GRDefinitions; 9 import skia.EnumMappings; 10 import skia.Exceptions; 11 12 class GRContext : SKObject, ISKSkipObjectRegistration 13 { 14 this (void* h, bool owns) 15 { 16 super(h,owns); 17 } 18 19 protected override void Dispose (bool disposing) 20 { 21 return super.Dispose (disposing); 22 } 23 24 25 // Create 26 27 static GRContext Create (GRBackend backend) 28 { 29 switch (backend){ 30 case GRBackend.Metal: 31 throw new NotSupportedException (); 32 case GRBackend.OpenGL: 33 return CreateGl (); 34 case GRBackend.Vulkan: 35 throw new NotSupportedException (); 36 case GRBackend.Dawn: 37 throw new NotSupportedException (); 38 default: 39 throw new ArgumentOutOfRangeException (backend.stringof); 40 } 41 } 42 43 44 static GRContext Create (GRBackend backend, GRGlInterface backendContext) 45 { 46 switch(backend){ 47 case GRBackend.Metal: 48 throw new NotSupportedException (); 49 case GRBackend.OpenGL: 50 return CreateGl (backendContext); 51 case GRBackend.Vulkan: 52 throw new NotSupportedException (); 53 case GRBackend.Dawn: 54 throw new NotSupportedException (); 55 default: 56 throw new ArgumentOutOfRangeException (backend.stringof); 57 } 58 59 } 60 61 62 static GRContext Create (GRBackend backend, void* backendContext) 63 { 64 switch(backend){ 65 case GRBackend.Metal: 66 throw new NotSupportedException (); 67 case GRBackend.OpenGL: 68 return GetObject (SkiaApi.gr_context_make_gl (cast(gr_glinterface_t*)backendContext)); 69 case GRBackend.Vulkan: 70 throw new NotSupportedException (); 71 case GRBackend.Dawn: 72 throw new NotSupportedException (); 73 default: 74 throw new ArgumentOutOfRangeException (backend.stringof); 75 } 76 } 77 78 79 // CreateGl 80 81 static GRContext CreateGl () 82 { 83 return CreateGl (null); 84 } 85 86 87 static GRContext CreateGl (GRGlInterface backendContext) 88 { 89 return GetObject (SkiaApi.gr_context_make_gl (backendContext is null ? null : cast(gr_glinterface_t*)backendContext.Handle)); 90 } 91 92 // CreateVulkan 93 94 static GRContext CreateVulkan (GRVkBackendContext backendContext) 95 { 96 if (backendContext is null) 97 throw new ArgumentNullException (backendContext.stringof); 98 99 return GetObject (SkiaApi.gr_context_make_vulkan (backendContext.ToNative ())); 100 } 101 102 // 103 104 GRBackend Backend() 105 { 106 return SkiaApi.gr_context_get_backend (cast(gr_context_t*)Handle).FromNative (); 107 } 108 109 void AbandonContext (bool releaseResources = false) 110 { 111 if (releaseResources) 112 SkiaApi.gr_context_release_resources_and_abandon_context (cast(gr_context_t*)Handle); 113 else 114 SkiaApi.gr_context_abandon_context (cast(gr_context_t*)Handle); 115 } 116 117 void GetResourceCacheLimits (out int maxResources, out long maxResourceBytes) 118 { 119 maxResources = -1; 120 maxResourceBytes = GetResourceCacheLimit (); 121 } 122 123 void SetResourceCacheLimits (int maxResources, long maxResourceBytes) 124 { 125 return SetResourceCacheLimit (maxResourceBytes); 126 } 127 128 long GetResourceCacheLimit () 129 { 130 return cast(long)SkiaApi.gr_context_get_resource_cache_limit (cast(gr_context_t*)Handle); 131 } 132 133 void SetResourceCacheLimit (long maxResourceBytes) 134 { 135 return SkiaApi.gr_context_set_resource_cache_limit (cast(gr_context_t*)Handle, cast(void*)maxResourceBytes); 136 } 137 138 // void GetResourceCacheUsage (out int maxResources, out long maxResourceBytes) 139 // { 140 // void* maxResBytes; 141 // int* maxRes = &maxResources; 142 // SkiaApi.gr_context_get_resource_cache_usage (cast(gr_context_t*)Handle, maxRes, &maxResBytes); 143 // maxResourceBytes = cast(long)maxResBytes; 144 // } 145 146 void ResetContext (GRGlBackendState state) 147 { 148 return ResetContext (cast(uint)state); 149 } 150 151 void ResetContext (GRBackendState state = GRBackendState.All) 152 { 153 return ResetContext (cast(uint)state); 154 } 155 156 void ResetContext (uint state) 157 { 158 return SkiaApi.gr_context_reset_context (cast(gr_context_t*)Handle, state); 159 } 160 161 void Flush () 162 { 163 return SkiaApi.gr_context_flush (cast(gr_context_t*)Handle); 164 } 165 166 int GetMaxSurfaceSampleCount (SKColorType colorType) 167 { 168 return SkiaApi.gr_context_get_max_surface_sample_count_for_color_type (cast(gr_context_t*)Handle, colorType.ToNative ()); 169 } 170 171 int GetRecommendedSampleCount (GRPixelConfig config, float dpi) 172 { 173 return 0; 174 } 175 176 // void DumpMemoryStatistics (SKTraceMemoryDump dump) 177 // { 178 // // return SkiaApi.gr_context_dump_memory_statistics (Handle, dump?.Handle ?? throw new ArgumentNullException (dump.stringof)); 179 // return SkiaApi.gr_context_dump_memory_statistics (cast(gr_context_t*)Handle, dump.Handle); 180 // } 181 182 // void PurgeResources () 183 // { 184 // return SkiaApi.gr_context_free_gpu_resources (cast(gr_context_t*)Handle); 185 // } 186 187 // void PurgeUnusedResources (long milliseconds) 188 // { 189 // return SkiaApi.gr_context_perform_deferred_cleanup (cast(gr_context_t*)Handle, milliseconds); 190 // } 191 192 // void PurgeUnlockedResources (bool scratchResourcesOnly) 193 // { 194 // return SkiaApi.gr_context_purge_unlocked_resources (cast(gr_context_t*)Handle, scratchResourcesOnly); 195 // } 196 197 // void PurgeUnlockedResources (long bytesToPurge, bool preferScratchResources) 198 // { 199 // return SkiaApi.gr_context_purge_unlocked_resources_bytes (cast(gr_context_t*)Handle, cast(void*)bytesToPurge, preferScratchResources); 200 // } 201 202 static GRContext GetObject (void* handle) 203 { 204 return handle is null ? null : new GRContext (handle, true); 205 } 206 }