Repository navigation
Add GRVkMemoryAllocator managed binding (#4567) - #4585
ramezgerges wants to merge 6 commits into
Conversation
📦 Try the packages from this PRWarning Do not run these scripts without first reviewing the code in this PR. Step 1 — Download the packages bash / macOS / Linux: curl -fsSL https://raw.githubusercontent.com/mono/SkiaSharp/main/scripts/get-skiasharp-pr.sh | bash -s -- 4585PowerShell / Windows: iex "& { $(irm https://raw.githubusercontent.com/mono/SkiaSharp/main/scripts/get-skiasharp-pr.ps1) } 4585"Step 2 — Add the local NuGet source dotnet nuget add source ~/.skiasharp/hives/pr-4585/packages --name skiasharp-pr-4585More options
Or download manually from Azure Pipelines — look for the Remove the source when you're done: dotnet nuget remove source skiasharp-pr-4585 |
|
/azp run |
|
Azure Pipelines will not run the associated pipelines, because the pull request was updated after the run command was issued. Review the pull request again and issue a new run command. |
1 similar comment
|
Azure Pipelines will not run the associated pipelines, because the pull request was updated after the run command was issued. Review the pull request again and issue a new run command. |
3c0bebf to
0936f91
Compare
|
Rebased onto the current |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
8 similar comments
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
📦 Artifact size reportPackages from this PR (build Total Packages
+38 package(s) unchanged (< 50.0 KB). Per-file changes
|
| File | Size |
|---|---|
runtimes/win-x64/native/libGLESv2.pdb |
66.6 MB → 66.5 MB (🟢 −144.0 KB) |
runtimes/win-arm64/native/libGLESv2.pdb |
64.5 MB → 64.4 MB (🟢 −128.0 KB) |
runtimes/win-x86/native/libGLESv2.pdb |
67.3 MB → 67.2 MB (🟢 −96.0 KB) |
Informational only — this never blocks the PR. Native binaries are labelled by os/arch.
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
6 similar comments
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
0936f91 to
6d759c5
Compare
|
Azure Pipelines will not run the associated pipelines, because the pull request was updated after the run command was issued. Review the pull request again and issue a new run command. |
1 similar comment
|
Azure Pipelines will not run the associated pipelines, because the pull request was updated after the run command was issued. Review the pull request again and issue a new run command. |
6d759c5 to
9c9ae4c
Compare
15915ad to
763cede
Compare
c610325 to
9fb0ae6
Compare
9fb0ae6 to
ea3cb18
Compare
mattleibow
left a comment
There was a problem hiding this comment.
Small PR-description cleanup: the Changes > Public API snippet still shows threadSafe = false and protectedContent = false as default parameters, but the binding exposes overloads instead. Please update the snippet to match the actual API. The docs-issue checkbox is also unchecked; if the API-docs issue was filed, please mark it, otherwise file it before merging.
Addresses the review on mono/SkiaSharp#4585. ToAllocatorBackendContext left fQueue as VK_NULL_HANDLE on the reasoning that the queue is irrelevant to allocator construction. That is true of what Make reads: neither VulkanAMDMemoryAllocator::Make nor skgpu::MakeInterface touches fQueue. But Make opens with SkASSERT(backendContext.fQueue != VK_NULL_HANDLE), so gr_vk_memory_allocator_make_default aborted on every Vulkan-enabled debug build even with valid handles. Release CI compiles the assert out and never saw it. gr_vk_allocator_default_options_t gains a required fQueue, which is passed through, and make_default now rejects a null queue alongside the other handles Make asserts on, so invalid input returns null in release builds too rather than only aborting in debug ones. The options struct is new in this PR and unreleased, so adding the field has no ABI cost. Not verified locally: no Vulkan-enabled debug build is available here. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Addresses the review on mono#4585. Pins the companion mono/skia#307. skgpu::VulkanMemoryAllocators::Make asserts that the backend context's queue is set. The C shim left it null, so CreateDefault aborted on every Vulkan-enabled debug build even with valid handles; release CI compiles the assert out. All three CreateDefault overloads now take a required vkQueue after vkDevice, validated like the other handles and passed through to the native options. This API is new in this PR and unreleased, so the signature change has no compatibility cost. GRVkMemoryAllocatorTest covers the null-queue rejection and passes the test device's graphics queue in every allocator-creating test. Co-Authored-By: Claude Opus 5.5 <[email protected]>
fd2f9a6 to
97313f7
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved critical native integration, ABI compatibility, and allocator-lifetime defects risk missing-entry-point exceptions and native crashes.
4 open issues
What changed in this PR
Adds a managed Vulkan memory allocator wrapper so callers can supply allocators to both Ganesh and Graphite, addressing #4567 alongside the companion native API.
Changes:
- Adds
GRVkMemoryAllocator.CreateDefaultoverloads and allocator properties on both Vulkan backend contexts. - Adds generated native bindings and allocator option mappings.
- Adds validation, lifetime, delegate-collection, and allocator-sharing tests.
| File | Description |
|---|---|
tests/VulkanTests/GRVkMemoryAllocatorTest.cs |
Adds allocator creation, lifetime, and rendering tests. |
tests/Tests/ApiTests.cs |
Accounts for the delegate-bearing allocator options struct. |
binding/SkiaSharp/GRVkMemoryAllocator.cs |
Introduces the reference-counted allocator wrapper and factories. |
binding/SkiaSharp/GRVkBackendContext.cs |
Passes caller-provided allocators to Ganesh. |
binding/SkiaSharp/Gpu/Graphite/SKGraphiteVkBackendContext.cs |
Passes caller-provided allocators to Graphite. |
binding/SkiaSharp/Generated/SkiaApi_include_c_gr_vk_allocator_h.generated.cs |
Adds native allocator creation and reference-management bindings. |
binding/SkiaSharp/Generated/SKGraphiteVkBackendContextNative.generated.cs |
Adds the Graphite allocator field. |
binding/SkiaSharp/Generated/GRVkAllocatorDefaultOptionsNative.generated.cs |
Defines the native allocator options layout. |
binding/libSkiaSharp.json |
Maps native allocator options to the managed interop type. |
Review effort: Balanced
Wraps the new gr_vk_memory_allocator_make_default C API so managed callers can supply a Vulkan memory allocator to both Ganesh and Graphite. Motivation: Skia m152 removes Ganesh's internal VMA fallback, after which any null allocator slot causes gr_direct_context_make_vulkan to fail. Graphite hits the same failure today via its own null check. * new GRVkMemoryAllocator : SKObject, ISKReferenceCounted with CreateDefault(vkInstance, vkPhysicalDevice, vkDevice, maxApiVersion, getProcedureAddress, threadSafe, protectedContent) — the get-proc delegate is only used synchronously during Make, so its GCHandle is released before the method returns * GRVkBackendContext.MemoryAllocator + SKGraphiteVkBackendContext. MemoryAllocator setters plumbed through ToNative() * Context takes its own internal ref via sk_ref_sp; managed wrapper can be disposed immediately after CreateVulkan returns Tests (tests/VulkanTests/GRVkMemoryAllocatorTest.cs) cover: * argument validation (null instance/phys-device/device/getProc) * CreateDefault succeeds on a real Vulkan context * Ganesh: dispose wrapper after CreateVulkan → context keeps working * Graphite: same pass-through smoke test, plus a Sync submit Co-Authored-By: Claude Opus 4.7 <[email protected]>
Co-authored-by: Copilot App <[email protected]>
Prove the allocator only uses getProc during creation, survives collection of the getProc delegate, and that a thread-safe allocator can be shared by multiple Ganesh contexts. Co-authored-by: Copilot App <[email protected]>
Addresses the review on mono#4585. Pins the companion mono/skia#307. skgpu::VulkanMemoryAllocators::Make asserts that the backend context's queue is set. The C shim left it null, so CreateDefault aborted on every Vulkan-enabled debug build even with valid handles; release CI compiles the assert out. All three CreateDefault overloads now take a required vkQueue after vkDevice, validated like the other handles and passed through to the native options. This API is new in this PR and unreleased, so the signature change has no compatibility cost. GRVkMemoryAllocatorTest covers the null-queue rejection and passes the test device's graphics queue in every allocator-creating test. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Addresses the review on mono/SkiaSharp#4585. ToAllocatorBackendContext left fQueue as VK_NULL_HANDLE on the reasoning that the queue is irrelevant to allocator construction. That is true of what Make reads: neither VulkanAMDMemoryAllocator::Make nor skgpu::MakeInterface touches fQueue. But Make opens with SkASSERT(backendContext.fQueue != VK_NULL_HANDLE), so gr_vk_memory_allocator_make_default aborted on every Vulkan-enabled debug build even with valid handles. Release CI compiles the assert out and never saw it. gr_vk_allocator_default_options_t gains a required fQueue, which is passed through, and make_default now rejects a null queue alongside the other handles Make asserts on, so invalid input returns null in release builds too rather than only aborting in debug ones. The options struct is new in this PR and unreleased, so adding the field has no ABI cost. Not verified locally: no Vulkan-enabled debug build is available here. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1672de4 to
9c0091b
Compare
…ono#4567) ToNative() copies raw handles (the memory allocator, the extensions) out of wrappers that only backendContext references, and neither branch used backendContext after that. A temporary allocator's finalizer could then release the native allocator before the factory took its own reference. Call GC.KeepAlive(backendContext) after the native call in both branches, as CreateGl does. SKGraphiteContext.CreateVulkan already uses the backend context after its native call, so it needs no change. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Skia falls back to its own default allocator when none is forwarded, so the rendering-only assertions passed even if the caller's allocator never reached the native context. Check that creating the Ganesh and Graphite contexts raises this allocator's native ref count. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
The Public API snippet in the description now shows the three |


Description
Wraps the new
gr_vk_memory_allocator_make_defaultC API so managed callers can supply a Vulkan memory allocator to both Ganesh and Graphite.Motivation: Skia m152 removes Ganesh's internal
SK_USE_VMAfallback (upstream commit5e183e5aea), after which any null allocator slot causesgr_direct_context_make_vulkanto fail. Graphite hits the same failure today via its own null check. Without a managed way to construct an allocator, SkiaSharp callers cannot fill the slot even though the C struct already exposes it.Related issues
Fixes #4567
Required skia PR
Requires mono/skia#307
Areas affected
binding/)externals/skia/src/c,include/c)Changes
Public API
ABI-stable additive changes only.
Behavior
sk_ref_spon the supplied allocator, so callers canDispose()the managed wrapper immediately afterGRContext.CreateVulkan/SKGraphiteContext.CreateVulkanreturns.MemoryAllocatoris left null, the native shim continues to auto-create Skia's default VMA-backed allocator (same behavior as before this PR for Graphite; new safety net for Ganesh once m152 lands).vkQueueis required.skgpu::VulkanMemoryAllocators::Makeasserts the queue is set even though it does not currently read it; an earlier revision left it null, which aborted every Vulkan-enabled debug build (release CI compiles the assert out, so it never showed). The native side now also rejects a null queue, so invalid input returnsnullin release builds rather than only aborting in debug ones.GRContext.CreateVulkankeeps the backend context (and so its allocator wrapper) alive withGC.KeepAliveuntil the native factory has taken its own ref, matchingCreateGl.CreateDefaultonly — VMA caches resolved Vulkan function pointers, so theGCHandleis released before the method returns.Testing
New
tests/VulkanTests/GRVkMemoryAllocatorTest.cs:CreateDefaultRejectsNullHandlesQueueAndGetProc: argument validation, including the required queueCreateDefaultReturnsAllocator: happy path on a real Vulkan deviceGaneshContextRetainsAllocatorAfterWrapperDispose: asserts the context raises this allocator's native ref count (Skia would otherwise fall back to its own default allocator unnoticed), then disposes the wrapper and draws + flushesGraphiteContextAcceptsCustomAllocator: the same ref-count check and pass-through on Graphite, plus aSyncsubmitAllocatorGetProcIsOnlyUsedDuringCreation: the get-proc delegate is not called afterCreateDefaultreturnsAllocatorSurvivesGetProcCollection: the allocator stays usable after the delegate is collectedThreadSafeAllocatorCanBeSharedByContexts: one thread-safe allocator shared by several contextsCI covers the current head on all Vulkan-enabled platforms.
Checklist
Changesabove lists all public API and behavioral changes (or "None.")mono/skiaPR linked above and bindings regenerated🤖 Generated with Claude Code