Style resources and rendering
Martin treats a map style as a named JSON resource and uses it as the input for both style delivery and server-side image rendering. The rendering path reuses loaded map state and fetched style resources while keeping tile and free-camera work separate.
Style resource model
The style resource model connects a client-facing style ID to one JSON file. The same registry supplies the style document endpoint and the render requests, so both paths use the same name resolution.
Style identity and catalog
martin-core/…/mod.rs represents a style as an ID mapped to a file path. A style ID may be requested with or without the .json suffix. The catalog exposes the path and reserves optional fields for style kind, revision, layer count, colors, and modification time. These fields describe a style for discovery; they do not change how the renderer loads it.
The server reads and validates the JSON when it serves the style document. It expands relative resource URLs into absolute URLs based on the request and server path settings. This lets a map client fetch tiles, sprites, and glyphs through usable URLs even when the style file uses relative paths.
References: martin-core/…/mod.rs• L82-107 martin-core/…/mod.rs• L132-163 martin/…/styles.rs• L47-95
Style inputs
The render path accepts either a file-backed style or a style JSON document already in memory. Server-managed styles normally use the file-backed form. Static rendering also carries camera, output size, pixel ratio, orientation, and optional one-request overlays as one render request.
The input shape keeps the base style separate from temporary map content. A static POST can add GeoJSON features for one image, but those features do not become part of the cached base style.
References: martin-core/…/render_pool.rs• L20-42 martin-core/…/render_pool.rs• L45-127 martin/…/styles_static.rs• L276-343
Render targets
Server-side rendering turns a named style into raster images. It provides two targets with different camera models and separate work pools.
Tile rendering
Tile rendering produces an XYZ image for a style at z/x/y. The tile size is 256 or 512 logical pixels before scaling. The default is 512 pixels because that is the size used by MapLibre; 256 pixels is available for clients that expect the more common raster tile size.
The output dimensions are the logical tile size multiplied by the requested pixel ratio. A request without a ratio is @1x. Only configured whole-number ratios from 1 through 4 are served, which prevents the server from creating renderer variants that clients do not need.
References: martin-core/…/tile_size.rs• L5-16 martin-core/…/pixel_ratio.rs• L6-10 martin/…/styles_rendering.rs• L184-267
Static image rendering
Static rendering uses a free camera instead of a tile coordinate. A request can name a center and zoom with optional bearing and pitch, or give a bounding box that the server fits into the requested image. The image width and height are logical dimensions; the pixel ratio scales the rendered pixels.
GET returns the base style. POST adds an inline GeoJSON FeatureCollection for that render only. The renderer removes the temporary overlay after the request so the next request sees the unchanged base style.
References: martin/…/styles_static.rs• L40-73 martin/…/styles_static.rs• L218-317 martin/…/styles_static.rs• L346-517
Renderer reuse and resource caches
Martin does not cache completed images. It caches the expensive state used to make them: loaded renderers and resources fetched by the MapLibre runtime. This reduces setup work while allowing every request to use its own camera and output size.
Renderer pools
The server starts one pool for tiles and one pool for static images. Each pool has worker threads, and each worker renders one image at a time. A worker owns its graphics device and a small set of loaded renderers.
The renderer cache has different keys for the two targets:
| Pool | Reused renderer state | Size changes |
|---|---|---|
| Tile | Style plus tile geometry, including tile size and pixel ratio | Use a renderer for each geometry |
| Static | Style plus pixel ratio | Resize the held renderer when width or height changes |
Each worker keeps the most recently used entries up to its configured capacity. When the capacity is full, the least recently used entry is dropped or replaced. A render or timeout error also removes the active entry so a later request starts with a fresh renderer.
References: martin-core/…/render_pool.rs• L130-175 martin-core/…/render_pool.rs• L605-687 martin-core/…/render_pool.rs• L690-765
Ambient resource cache
All renderers use a shared MapLibre runtime. Its ambient cache stores fetched tiles, glyphs, and sprites for both pools up to the configured byte limit. A fresh cached resource avoids a network fetch, but normal HTTP freshness rules still apply.
This cache is different from the renderer cache. The renderer cache holds ready map state on a worker. The ambient cache holds shared external resources that many renderers can fetch. Setting its limit to zero can be useful when styles point to resources served locally by Martin.
References: martin-core/…/renderer.rs• L28-90 martin-core/…/render_pool.rs• L142-175 martin/…/styles.rs• L70-74
Render-pool scheduling
The render pool uses one bounded queue per target and assigns queued work to free workers. Its delay scheduling model favors a worker that already holds the needed renderer, but it gives older requests a firm path to service.
Request selection
When a worker becomes free, the scheduler follows this order:
- Take the oldest request if it has waited at least one second.
- Otherwise, search the first 64 queued requests for one whose renderer is already held by the worker.
- Otherwise, take the oldest request when its wait reaches the measured cost of changing renderers, or when the worker holds no renderer.
- If none applies, wait for the delay to pass or for queue activity, then evaluate the queue again.
The switch cost is learned from successful cache hits and misses. The scheduler limits the delay to a short range, so it can favor reuse without allowing a different style or pixel ratio to starve.
This diagram shows how a request moves from a bounded queue to a worker while balancing renderer reuse and request age.
flowchart TD
A[Render request] --> B[Bounded pool queue]
B --> C{Oldest request waited 1 second?}
C -- Yes --> D[Give oldest request to free worker]
C -- No --> E{Worker holds a matching renderer in lookahead?}
E -- Yes --> F[Give matching request to worker]
E -- No --> G{Wait reached switch cost or worker is empty?}
G -- Yes --> D
G -- No --> H[Wait for queue activity or switch delay]
H --> C
D --> I[Render one image]
F --> I
I --> J[Record hit or miss time]
J --> K[Return image]
References: martin-core/…/render_pool.rs• L224-229 martin-core/…/render_pool.rs• L273-339 martin-core/…/render_pool.rs• L418-498
Queue capacity and worker recovery
The queue has a fixed amount of room per worker. This bounds work already accepted by the pool and limits the latency caused by a stalled renderer. Requests that cannot enter a stopped pool fail instead of remaining in an unbounded queue.
If a render worker panics, the pool reports a rendering failure and builds a replacement for the next request. If the replacement cannot start, later requests receive the start-up error until a replacement can be built.
References: martin-core/…/render_pool.rs• L251-263 martin-core/…/render_pool.rs• L295-333 martin-core/…/render_pool.rs• L450-498
Image sizing and encoding
Rendering produces an in-memory RGBA image first. The HTTP layer then encodes that image in the requested format under a separate limit for CPU-bound encoding work.
Dimension and pixel-ratio rules
Tile dimensions come from the configured tile size and the request ratio. Static images accept a width and height from 1 through 2048 pixels before scaling. Both endpoints reject ratios that are not in the configured set before they reach the renderer.
The renderer receives logical static dimensions rather than dimensions already multiplied by the ratio. It applies the ratio once when it creates the output, which keeps 800x600@2x at 1600 by 1200 output pixels rather than scaling it twice.
References: martin/…/styles_static.rs• L149-189 martin/…/styles_static.rs• L477-493 martin-core/…/renderer.rs• L228-250
Output formats
The HTTP layer supports PNG, JPEG, and WebP. PNG can use a bounded color palette or keep full RGBA. WebP can be lossy or lossless, while JPEG is always lossy and drops alpha because the format has no alpha channel.
Encoding runs outside the request worker and is limited by process-wide permits. This keeps image conversion from blocking the server's async request workers when several renders finish at once.
References: martin/…/styles_rendering.rs• L31-69 martin/…/styles_rendering.rs• L88-180 martin/…/styles.rs• L81-127
Configuration boundaries
The style configuration decides which files become named styles and whether server-side rendering is available. It also sets the worker, cache, size, ratio, and encoding limits that shape the render service.
Rendering is available only in Linux builds with the rendering feature. When it is disabled, the style JSON resource can still be served, but image endpoints return a forbidden response rather than attempting to render.
The main settings have these system effects:
| Setting | System effect |
|---|---|
workers | Number of workers in each render pool |
renderers_per_worker | Tile renderer reuse and per-worker memory |
static_renderers_per_worker | Static renderer reuse and per-worker memory |
tile_size | Logical XYZ tile dimensions |
pixel_ratios | Image scales that the HTTP API accepts |
ambient_cache_size_mb | Shared MapLibre resource cache limit |
png_palette and quality | Image encoding size and fidelity |
See configuration and reload for the wider configuration lifecycle. See HTTP APIs and map resources for the public resource and image contracts. See source catalog and lifecycle for how configured sources enter the server.
References: martin/…/styles.rs• L24-91 martin/…/styles.rs• L216-317 martin-core/…/mod.rs• L266-335
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the accepted
pixel_ratioscontract aligned with both tile URL parsing and static size parsing. - Keep tile renderer keys aligned with every geometry input that changes output dimensions.
- Keep static renderer reuse aligned with style identity and pixel ratio so a renderer is never used with the wrong scale.
- Preserve the distinction between the renderer cache and the shared ambient resource cache when changing reload or cache settings.
- Keep image encoder settings aligned with the content type and the alpha behavior of each output format.
- When changing style path resolution, check both style JSON URL expansion and server-side file loading.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Rendering is enabled in a build without the Linux rendering support | Image requests return a forbidden response | Build and deploy the rendering-enabled Linux variant |
| A requested ratio is not configured | The request is rejected before rendering | Configure only the ratios that clients use |
| A style JSON file is missing or malformed | Style delivery or rendering fails; rendering returns an error response | Keep configured style paths valid and validate style documents |
| A worker cannot create a graphics device | Rendering pools fail to start or a replacement request fails | Provide a usable graphics or software rendering environment |
| A renderer times out or fails during a render | The renderer is discarded and the request fails | Let the next request rebuild the renderer and inspect MapLibre errors |
| A static image exceeds 2048 pixels in width or height | The API returns 400 Bad Request | Keep logical image dimensions within the endpoint limit |
| A JPEG request expects transparency | Alpha is lost in the encoded image | Use PNG or WebP when alpha is required |
Key files
| File | Why to read it |
|---|---|
| martin-core/…/mod.rs | Defines style identity, catalog entries, render settings, and the public style source manager |
| martin-core/…/render_pool.rs | Shows renderer reuse, tile and static pools, sizing keys, overlays, and scheduling |
| martin-core/…/renderer.rs | Defines the shared MapLibre runtime and the lifecycle of one renderer |
| martin/…/styles.rs | Shows how style JSON is resolved, validated, and returned to clients |
| martin/…/styles_rendering.rs | Shows rendered tile requests, output formats, and encoding limits |
| martin/…/styles_static.rs | Shows static camera, size, pixel-ratio, and overlay request flow |
| martin/…/styles.rs | Shows how configuration becomes style sources and render-pool settings |