feat(android): Vulkan graphics backend support for Android custom layers (#4348)

Co-authored-by: Vladyslav Odobesku <VladyslavOdobesku@intellias.com>
Co-authored-by: Claude Opus 4 <noreply@anthropic.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Vladyslav Odobesku
2026-06-26 19:53:00 +03:00
committed by GitHub
parent 9f6f8e3f1b
commit aa90185d42
25 changed files with 791 additions and 67 deletions

View File

@@ -1,59 +1,14 @@
#pragma once
#include <mbgl/style/layer.hpp>
#include <mbgl/style/layers/custom_layer_render_parameters.hpp>
#include <mbgl/style/layers/custom_layer_host.hpp>
#include <array>
#include <memory>
namespace mbgl {
namespace style {
class CustomLayerHost {
public:
virtual ~CustomLayerHost() = default;
/**
* Initialize any GL/Metal state needed by the custom layer. This method is called
* once, from the main thread, at a point when the GL context is active but
* before rendering for the first time.
*
* Resources that are acquired in this method must be released in the
* `deinitialize` function.
*/
virtual void initialize() = 0;
/**
* Render the layer. This method is called once per frame. The
* implementation should not make any assumptions about the GL/Metal state (other
* than that the correct context is active). It may make changes to the
* state, and is not required to reset values such as the depth mask,
* stencil mask, and corresponding test flags to their original values. Make
* sure that you are drawing your fragments with a z value of 1 to take
* advantage of the opaque fragment culling in case there are opaque layers
* above your custom layer.
*/
virtual void render(const mbgl::style::CustomLayerRenderParameters&) = 0;
/**
* Called when the system has destroyed the underlying GL/Metal context. The
* `deinitialize` function will not be called in this case, however
* `initialize` will be called instead to prepare for a new render.
*
*/
virtual void contextLost() = 0;
/**
* Destroy any GL/Metal state needed by the custom layer, and deallocate context,
* if necessary. This method is called once, from the main thread, at a
* point when the GL/Metal context is active.
*
* Note that it may be called even when the `initialize` function has not
* been called.
*/
virtual void deinitialize() = 0;
};
class CustomLayer final : public Layer {
public:
CustomLayer(const std::string& id, std::unique_ptr<CustomLayerHost> host);

View File

@@ -0,0 +1,59 @@
#pragma once
#include <mbgl/style/layers/custom_layer_init_parameters.hpp>
#include <mbgl/style/layers/custom_layer_render_parameters.hpp>
namespace mbgl {
namespace style {
class CustomLayerHost {
public:
virtual ~CustomLayerHost() = default;
/**
* Initialize any GL/Metal/Vulkan state needed by the custom layer. This method
* is called once, from the main thread, at a point when the graphics context
* is active but before rendering for the first time.
*
* The parameters argument provides backend-specific device handles:
* - Vulkan: cast to mbgl::style::vulkan::CustomLayerInitParameters for
* access to vk::Device, vk::PhysicalDevice, and the dispatcher.
* - Metal/GL: base CustomLayerInitParameters (no extra handles currently).
*
* Resources that are acquired in this method must be released in the
* `deinitialize` function.
*/
virtual void initialize(const CustomLayerInitParameters&) = 0;
/**
* Render the layer. This method is called once per frame. The
* implementation should not make any assumptions about the GL/Metal state (other
* than that the correct context is active). It may make changes to the
* state, and is not required to reset values such as the depth mask,
* stencil mask, and corresponding test flags to their original values. Make
* sure that you are drawing your fragments with a z value of 1 to take
* advantage of the opaque fragment culling in case there are opaque layers
* above your custom layer.
*/
virtual void render(const mbgl::style::CustomLayerRenderParameters&) = 0;
/**
* Called when the system has destroyed the underlying GL/Metal context. The
* `deinitialize` function will not be called in this case, however
* `initialize` will be called instead to prepare for a new render.
*
*/
virtual void contextLost() = 0;
/**
* Destroy any GL/Metal state needed by the custom layer, and deallocate context,
* if necessary. This method is called once, from the main thread, at a
* point when the GL/Metal context is active.
*
* Note that it may be called even when the `initialize` function has not
* been called.
*/
virtual void deinitialize() = 0;
};
} // namespace style
} // namespace mbgl

View File

@@ -0,0 +1,16 @@
#pragma once
namespace mbgl {
namespace style {
/**
* Base parameters passed to CustomLayerHost::initialize().
* Backend-specific subclasses provide device handles needed for
* resource creation (pipelines, buffers, etc.).
*/
struct CustomLayerInitParameters {
virtual ~CustomLayerInitParameters() = default;
};
} // namespace style
} // namespace mbgl

View File

@@ -0,0 +1,39 @@
#pragma once
#include <mbgl/style/layers/custom_layer_init_parameters.hpp>
#include <vulkan/vulkan.hpp>
namespace mbgl {
namespace style {
namespace vulkan {
/**
* Vulkan-specific initialization parameters.
* Provides device handles so that custom layers can create pipelines,
* allocate buffers, and compile shaders during initialize().
*
* Note: vk::RenderPass is NOT available here because it depends on the
* active framebuffer which may not exist yet. Use the render pass from
* CustomLayerRenderParameters during the first render() call if needed
* for pipeline creation.
*/
struct CustomLayerInitParameters : mbgl::style::CustomLayerInitParameters {
/// Dynamic dispatcher for Vulkan function calls.
const vk::detail::DispatchLoaderDynamic& dispatcher;
/// Logical device handle.
vk::Device device;
/// Physical device handle — useful for querying memory properties, limits, etc.
vk::PhysicalDevice physicalDevice;
CustomLayerInitParameters(const vk::detail::DispatchLoaderDynamic& dispatcher_,
vk::Device device_,
vk::PhysicalDevice physicalDevice_)
: dispatcher(dispatcher_),
device(device_),
physicalDevice(physicalDevice_) {}
};
} // namespace vulkan
} // namespace style
} // namespace mbgl

View File

@@ -0,0 +1,48 @@
#pragma once
#include <mbgl/style/layers/custom_layer_render_parameters.hpp>
#include <vulkan/vulkan.hpp>
namespace mbgl {
class PaintParameters;
namespace style {
namespace vulkan {
struct CustomLayerRenderParameters : mbgl::style::CustomLayerRenderParameters {
/// Dynamic dispatcher.
const vk::detail::DispatchLoaderDynamic& dispatcher;
/// Vulkan device
vk::Device device;
/// Compatible render pass handle — use when creating VkPipeline.
vk::RenderPass renderPass;
/// Active command buffer for the current render pass — record all draw calls here.
vk::CommandBuffer commandBuffer;
/// @brief Screen pre-rotation related to VkSurfaceTransformFlagBitsKHR.
/// This field is going to be non 0.0 when renderer decides to do the rotation inside the rendering pipeline instead
/// of relying on compositor. Custom layers must use this parameter to do the rotation in clip space.
///
/// @code{.cpp}
/// if(screenPreRotationRadiansClockwise != 0.0)
/// {
/// glm::vec3 rotation_axis = glm::vec3(0.0f, 0.0f, 1.0f);
/// glm::mat4 pre_rotate_mat = glm::rotate(screenPreRotationRadiansClockwise, rotation_axis);
/// MVP = pre_rotate_mat * MVP;
/// }
/// @endcode
///
/// @note For more info see https://developer.android.com/games/optimize/vulkan-prerotation.
/// @attention dFdx, dFdy in shaders also need adjustments if used.
float screenPreRotationRadiansClockwise = 0.0f;
explicit CustomLayerRenderParameters(const PaintParameters&);
};
} // namespace vulkan
} // namespace style
} // namespace mbgl

View File

@@ -18,7 +18,7 @@ public:
~RenderPass() override;
vulkan::CommandEncoder& getEncoder() { return commandEncoder; }
const gfx::RenderPassDescriptor& getDescriptor() { return descriptor; }
const gfx::RenderPassDescriptor& getDescriptor() const { return descriptor; }
void endEncoding();
void clearStencil(uint32_t value = 0) const;

View File

@@ -24,6 +24,7 @@ public:
const vk::Extent2D& getExtent() const { return extent; }
const vk::UniqueRenderPass& getRenderPass() const { return renderPass; }
virtual const vk::UniqueFramebuffer& getFramebuffer() const = 0;
virtual float getRotation() const { return 0.0f; }
protected:
RendererBackend& backend;
@@ -70,7 +71,7 @@ public:
bool didSurfaceTransformUpdate() const;
// rotation needed to align framebuffer contents with device surface
float getRotation();
float getRotation() const override;
void setSurfaceTransformPollingInterval(int32_t value) { surfaceTransformPollingInterval = value; }
int32_t getSurfaceTransformPollingInterval() const { return surfaceTransformPollingInterval; }