RMGui 0.15.0
C11 retained-mode UI library: layout kernel, style schema, widget painters, fonts, software framebuffer
Loading...
Searching...
No Matches
layer.h File Reference

layer.h — paint an element through a visual transform: offset, scale, rotation about a pivot, opacity, and a soft shadow or glow cast by the element's rounded rect. More...

#include <stdint.h>
#include "draw.h"
#include "fb.h"
#include "geom.h"
#include "motion.h"
#include "rounded.h"

Data Structures

struct  rmgui_layer_transform_t
 The visual transform of an element arranged at rect r. More...
struct  rmgui_layer_shadow_t
 A shadow (OVER) or glow (ADD) cast by the rounded rect of the element, radii device px, the SAME value passed to the face painter: shadow.radii = radii; rmgui_draw_fill_rounded_corners(fb, clip, rect, radii, color, mode); Uniform shadows retain the legacy binary raster; asymmetric shadows use the corner fill's exact antialiased raster. More...
struct  rmgui_layer_t
 Caller-owned layer. More...
struct  rmgui_layer_target_t
 Where rmgui_layer_begin wants the element painted. More...

Macros

#define RMGUI_LAYER_IDENTITY   {0, 0, RMGUI_MOTION_ONE, RMGUI_MOTION_ONE, 0, 0, 0, 255, RMGUI_LAYER_FILTER_BILINEAR}
 The identity transform: no offset, scale 1, no rotation, opaque, bilinear.

Enumerations

enum  rmgui_layer_filter_t { RMGUI_LAYER_FILTER_NEAREST , RMGUI_LAYER_FILTER_BILINEAR }
 Surface sampling for scale and rotation. More...

Functions

int rmgui_layer_alloc (rmgui_layer_t *layer, int max_w, int max_h, int max_blur_px)
 Allocates the surface (max_w x max_h) and a shadow mask with 6 * max_blur_px added to each axis of an all-zero layer.
void rmgui_layer_free (rmgui_layer_t *layer)
 Releases both surfaces and zero-fills layer.
int rmgui_layer_begin (rmgui_layer_t *layer, rmgui_fb_t *dst, rmgui_rect_t rect, rmgui_rect_t clip, const rmgui_layer_transform_t *transform, const rmgui_layer_shadow_t *shadow, rmgui_layer_target_t *out)
 Starts painting the element arranged at rect into dst through transform (and shadow, NULL for none); the result is kept inside clip (destination space).
void rmgui_layer_end (rmgui_layer_t *layer)
 Finishes the open begin: on the surface path, composites the shadow and then the surface into the begin's dst inside its clip (see rmgui_layer_transform_t).
int rmgui_layer_composite (rmgui_layer_t *layer, rmgui_fb_t *dst, const rmgui_fb_t *src, rmgui_rect_t rect, rmgui_rect_t clip, const rmgui_layer_transform_t *transform, const rmgui_layer_shadow_t *shadow)
 Composites the element arranged at rect, already painted into the region {0, 0, rect.w, rect.h} of the caller-owned src in the surface's premultiplied form (layer.h's precondition), into dst through transform and shadow (NULL for none), inside clip.
rmgui_rect_t rmgui_layer_bounds (rmgui_rect_t rect, const rmgui_layer_transform_t *transform, const rmgui_layer_shadow_t *shadow, int max_blur_px)
 The destination rect holding every pixel begin/end or rmgui_layer_composite may write for these arguments (unclipped): the transformed rect grown by one source pixel, joined with the shadow's rect grown by 3 * blur_px, transformed and moved by its offset.

Detailed Description

layer.h — paint an element through a visual transform: offset, scale, rotation about a pivot, opacity, and a soft shadow or glow cast by the element's rounded rect.

The transform is paint-only: hit testing and layout keep the arranged rect.

Protocol: rmgui_layer_begin names where to paint the element (a target fb, rect and clip); the host calls the element's ordinary painter with them; rmgui_layer_end puts the result into the destination. A transform that is a whole-pixel translation at scale 1, rotation 0 and opacity 255 paints directly into the destination at the offset rect, so it is byte-identical to plain painting and needs no allocation. Any other transform paints into the layer's surface and is composited at end. An element the caller-selected surface cannot hold is painted by the host into an fb of its own and put through rmgui_layer_composite, which takes only the shadow's mask from the layer.

Surface contents are premultiplied RGBA: begin clears the target rect to transparent black, and every rmgui draw, paint and element painter accumulates premultiplied colour there. Precondition: every colour channel of every surface pixel stays at most its alpha, so no pixel with alpha < 255 is written verbatim into a surface unless it is already premultiplied. The verbatim writers are rmgui_fb_fill_rect and rmgui_fb_clear (the colour given) and rmgui_fb_blit, rmgui_fb_blit_scaled and rmgui_fb_blit_stretch (every source pixel): a translucent image is blitted into a surface only in premultiplied form.

Macro Definition Documentation

◆ RMGUI_LAYER_IDENTITY

#define RMGUI_LAYER_IDENTITY   {0, 0, RMGUI_MOTION_ONE, RMGUI_MOTION_ONE, 0, 0, 0, 255, RMGUI_LAYER_FILTER_BILINEAR}

The identity transform: no offset, scale 1, no rotation, opaque, bilinear.

Enumeration Type Documentation

◆ rmgui_layer_filter_t

Surface sampling for scale and rotation.

Enumerator
RMGUI_LAYER_FILTER_NEAREST 

the source pixel containing the sample point

RMGUI_LAYER_FILTER_BILINEAR 

8-bit-weight two-stage lerp of the 4 nearest; outside the element is transparent

Function Documentation

◆ rmgui_layer_alloc()

int rmgui_layer_alloc ( rmgui_layer_t * layer,
int max_w,
int max_h,
int max_blur_px )

Allocates the surface (max_w x max_h) and a shadow mask with 6 * max_blur_px added to each axis of an all-zero layer.

Sizes must be positive, max_blur_px nonnegative, and each grown mask extent plus two must be <= UINT16_MAX (the Q16 sampling type ceiling). Returns 0; -1 leaving *layer unchanged on invalid arguments, an already allocated layer, or allocation failure. Memory is proportional to the requested sizes; callers choose dimensions and blur to bound resources.

◆ rmgui_layer_free()

void rmgui_layer_free ( rmgui_layer_t * layer)

Releases both surfaces and zero-fills layer.

NULL or all-zero: no-op.

◆ rmgui_layer_begin()

int rmgui_layer_begin ( rmgui_layer_t * layer,
rmgui_fb_t * dst,
rmgui_rect_t rect,
rmgui_rect_t clip,
const rmgui_layer_transform_t * transform,
const rmgui_layer_shadow_t * shadow,
rmgui_layer_target_t * out )

Starts painting the element arranged at rect into dst through transform (and shadow, NULL for none); the result is kept inside clip (destination space).

Fills *out with where to call the element's painter: direct (whole-pixel translation, scale 1, rotation 0, opacity 255): {dst, rect moved by the offset, clip}; a shadow is composited into dst now, below the element. A moved rect whose origin leaves the int range lies outside every fb and gets the empty clip {0, 0, 0, 0}; surface (anything else): {&layer->surface, {0, 0, rect.w, rect.h}, the same rect}, cleared to transparent black. The shadow mask is the rect grown by 3 * blur_px per side. When the layer's mask holds it, it is rebuilt only when rect.w, rect.h, radii, blur_px or the mode (OVER vs ADD) differ from the cached one. A larger mask is generated and composited in tiles of the layer's mask, byte-identical to compositing it whole, and leaves no mask cached. Returns 0. Returns -1, changing nothing, when an argument other than shadow is NULL, rect is empty, a scale is outside the transform range, filter, mode, a casting radius or blur_px is out of range for the caller's blur allowance (radii must be nonnegative), dst is the layer's own surface or mask, a begin is already open on layer, the surface path is needed and the layer is unallocated or smaller than rect, or a shadow is needed and its grown extent plus two exceeds UINT16_MAX, the layer is unallocated, or its mask must be tiled and is smaller than 2 x 2. An all-zero layer still supports direct painting without a shadow.

◆ rmgui_layer_end()

void rmgui_layer_end ( rmgui_layer_t * layer)

Finishes the open begin: on the surface path, composites the shadow and then the surface into the begin's dst inside its clip (see rmgui_layer_transform_t).

Closes the layer. No open begin, or NULL: no-op.

◆ rmgui_layer_composite()

int rmgui_layer_composite ( rmgui_layer_t * layer,
rmgui_fb_t * dst,
const rmgui_fb_t * src,
rmgui_rect_t rect,
rmgui_rect_t clip,
const rmgui_layer_transform_t * transform,
const rmgui_layer_shadow_t * shadow )

Composites the element arranged at rect, already painted into the region {0, 0, rect.w, rect.h} of the caller-owned src in the surface's premultiplied form (layer.h's precondition), into dst through transform and shadow (NULL for none), inside clip.

The result is byte-identical to rmgui_layer_begin's surface path and rmgui_layer_end with a surface holding the same region. The layer supplies only the shadow's mask, cached or tiled as rmgui_layer_begin describes; without a shadow an all-zero layer suffices. src is only read. Returns 0. Returns -1, changing nothing, when layer, dst, src or transform is NULL, rect is empty, exceeds src, or its sampled extent plus two exceeds UINT16_MAX (including six times blur for a shadow), the transform or shadow is invalid as for rmgui_layer_begin, dst shares its pixels with src or the layer's surface or mask, src shares its pixels with the layer's mask, a begin is open on layer, or a shadow is cast and the layer is unallocated or its mask must be tiled and is smaller than 2 x 2.

◆ rmgui_layer_bounds()

rmgui_rect_t rmgui_layer_bounds ( rmgui_rect_t rect,
const rmgui_layer_transform_t * transform,
const rmgui_layer_shadow_t * shadow,
int max_blur_px )

The destination rect holding every pixel begin/end or rmgui_layer_composite may write for these arguments (unclipped): the transformed rect grown by one source pixel, joined with the shadow's rect grown by 3 * blur_px, transformed and moved by its offset.

shadow may be NULL; max_blur_px is the nonnegative device-pixel blur allowance. Sampled extents plus two must fit UINT16_MAX, including the shadow margin. Edges beyond the int range saturate, and a span still wider than INT_MAX starts at 0, below which no fb pixel lies. An empty rect, a NULL transform, or invalid arguments give {rect.x, rect.y, 0, 0}. Direct painting without a shadow needs no allocation and permits larger rects.