Search by

A draw list store in C: paths and paints built in its own memory, recorded pictures, a tree of groups, and which rects of a tree changed since it was last compared; and a muxer, converting pixels from one format to another in memory at an address

Package info

github.com/php-io-extensions/drawlist

Language:C

Type:php-ext

Ext name:ext-drawlist

pkg:composer/php-io-extensions/drawlist

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

0.11.x-dev 2026-12-03 14:46 UTC

This package is auto-updated.

Last update: 2026-12-03 14:46:31 UTC


README

ext-drawlist

Paths, recorded pictures, a tree of groups, and what changed in it, kept in C, and pixels muxed between formats, for PHP 8.4+

CI Packagist PHP License Platform OKF PIE

A program that keeps a scene and paints it again and again has to hold what it drew, and work out which part of the surface differs from last time. In PHP that is an array for every segment, an array for every call, and a walk over the whole tree for every comparison.

ext-drawlist keeps all of it in C. A path is built straight into the store's memory. Paints, stroke styles and matrices are its own small objects. A picture is the calls recorded into it. A group holds pictures and groups under a matrix, a clip and an opacity, and a comparison answers the rects of a tree that differ since it last looked. It draws nothing: a picture plays its calls back onto your object, with the objects it keeps. It has no dependency.

Its second job is muxing: converting pixels from the format they were drawn in to the format a display takes, from 32-bit color down to 1-bit pages or a palette of inks. It works on memory at an address, a rect at a time, in integer arithmetic written down step by step, and pixels never cross into PHP as a string or an array.

Basic usage

$path = (new DrawPath())->moveTo(0, 0)->lineTo(10, 0)->lineTo(10, 10)->close();
$red = new DrawSolidPaint(1.0, 0.0, 0.0, 1.0);

$recording = new DrawRecording();
$recording->save();
$recording->transform(new DrawMatrix(1, 0, 0, 1, 20, 10));
$recording->fill($path, $red, DRAW_FILL_NON_ZERO);
$recording->restore();
$picture = new DrawPicture($recording);

$picture->box();             // [20.0, 10.0, 30.0, 20.0]: x0, y0, x1, y1
$picture->replay($painter);  // $painter->save(), ->transform($matrix), ->fill($path, $red, 0), ->restore()

$root = new DrawGroup();
$root->attach($picture);
$comparison = new DrawComparison($root);
$comparison->differences();  // [[20.0, 10.0, 10.0, 10.0]]: x, y, width, height
$comparison->differences();  // []

Muxing

$from = new MuxFormat(MUX_LAYOUT_RGBA8888, channelOrder: MUX_CHANNELS_BGRA, alpha: MUX_ALPHA_PREMULTIPLIED);
$to = new MuxFormat(MUX_LAYOUT_RGB565);
$frame = new MuxImage($to, 320, 480);

// $address and $stride: 320 by 480 premultiplied BGRA pixels, wherever they were drawn
mux_rects($from, $address, $stride, $to, $frame->address(), $frame->stride(), 320, 480, [[10, 20, 30, 40]], MUX_DITHER_ORDERED);
// [[10, 20, 30, 40]]: the rects written

mux_pixel($to, $frame->address(), $frame->stride(), 320, 480, 10, 20);  // 0xRRGGBBAA

A rect grows to what the target can address. Under 1-bit pages it takes whole pages:

$pages = new MuxFormat(MUX_LAYOUT_MONO_PAGES, MUX_BIT_ORDER_LSB);
mux_rects($from, $address, $stride, $pages, $oled->address(), $oled->stride(), 128, 64, [[3, 9, 2, 2]], MUX_DITHER_NONE);
// [[3, 8, 2, 8]]

An ink panel's palette is a list of [0xRRGGBB, inverted, code]. Each color goes to its nearest ink in Oklab:

$inks = new MuxFormat(MUX_LAYOUT_INDEX2, palette: [[0x000000, false, 0], [0xFFFFFF, false, 1], [0xFFFF00, false, 2], [0xFF0000, false, 3]]);

Examples

A group's matrix moves everything under it. The comparison reports where it was and where it is:

$root->setMatrix(new DrawMatrix(1, 0, 0, 1, 0, 50));
$comparison->differences();  // [[20.0, 10.0, 10.0, 10.0], [20.0, 60.0, 10.0, 10.0]]

The revision of a group rises when something under it really changes:

$before = $root->revision();
$root->setOpacity(1.0);              // the opacity it already has
$root->revision() === $before;       // true

A recorded call keeps its path as it was then. A segment added later does not reach it:

$recording = new DrawRecording();
$recording->fill($path, $red, DRAW_FILL_NON_ZERO);
$path->lineTo(500, 500);
(new DrawPicture($recording))->box();  // [0.0, 0.0, 10.0, 10.0]

A replay hands a fill rule on as the value you give it for that rule, and keeps neither:

$picture->replay($painter, 'winding', 'alternate');  // fill() gets 'winding' or 'alternate' as its third argument

The operand classes are yours to extend, and a replay hands your own objects back. The copy of a path that a recording or a clip keeps is of the path's own class, with the values its declared properties had:

final class NamedPaint extends DrawSolidPaint
{
    public string $name = 'ink';
}

Requirements

  • PHP 8.4 or later, NTS or ZTS
  • Linux or macOS
  • A C compiler and the PHP development headers. No system library.

Installation

With PHP PIE (recommended)

pie install php-io-extensions/drawlist

Manually (install script)

./install-macos.sh            # Homebrew php@8.4 and php@8.4-zts
./install-debian-trixie.sh    # Debian trixie, Raspberry Pi OS

Manually (phpize)

phpize
./configure --enable-drawlist
make
sudo make install

Then add extension=drawlist to your php.ini.

Verify

php -r 'var_dump(extension_loaded("drawlist"));'

API

The full reference, with the layout of a path in memory and the comparison step by step, is in the knowledge bundle.

Paths and operands

Class What it is
DrawPath moveTo(), lineTo(), quadTo(), cubicTo(), close() build it; ops(), bounds() read it; rect() gives its rectangle when it is one closed axis-aligned rectangle; kindsAddress(), kindCount(), numbersAddress(), numberCount() give its memory; recorded() says whether it is a recorded copy. A new path is empty
DrawMatrix(a, b, c, d, e, f) toArray()
DrawStrokeStyle(width, cap, join, miterLimit, dashes, dashOffset, reach) width(), capCode(), joinCode(), miterLimit(), dashes(), dashOffset(), reach()
DrawSolidPaint(red, green, blue, alpha) components()
DrawGradientPaint(kind, geometry, stops, spread) kind(), geometry(), stops(), spreadCode()

Caps, joins, spreads, gradient kinds and fill rules are the DRAW_* constants. These five classes can be extended.

DrawRecording and DrawPicture

Method What it does
new DrawRecording() An empty recording
fill(path, paint, rule), stroke(path, paint, style) Record a fill or a stroke
save(), restore() Record them
transform(matrix), clip(path) Record a matrix or a clip
new DrawPicture(recording) The picture; the recording is spent
replay(reader, nonZero, evenOdd) Call reader once for each recorded call, with the operands the picture keeps
box() [x0, y0, x1, y1] of what it draws, or null
holder() The group that holds it, or null

DrawGroup and DrawComparison

Method What it does
attach(child), detach(child) Add a picture or a group last in paint order; take one out
children(), holder() The children; the group that holds this one
revision() Rises when this group, or anything under it, changes
setMatrix(matrix), matrix() The matrix of everything under it, or none
setClipPath(path), clipPath() The clip of everything under it, or none
setOpacity(opacity), opacity() How opaque everything under it is
new DrawComparison(root), differences() The rects of the tree that differ since this comparison's last call

MuxFormat, MuxImage and the mux calls

Call What it does
new MuxFormat(layout, bitOrder, byteOrder, channelOrder, scan, alpha, palette) How pixels are stored, from the MUX_* constants; a palette of at most 16 inks
new MuxImage(format, width, height) Zeroed memory for an image, freed with it; address(), stride(), size()
mux_rects(from, fromAddress, fromStride, to, toAddress, toStride, width, height, rects, dither) Convert the pixels inside each rect; answers the rects written
mux_pixel(format, address, stride, width, height, x, y) One pixel as 0xRRGGBBAA, straight
mux_fill(format, address, stride, width, height, rect, rgba) One color into a rect

Eleven layouts: 1-bit rows and pages, 2-, 4- and 8-bit index, 12, 16, 18, 24 and 32-bit color, and planes. MUX_DITHER_NONE takes each pixel to its nearest level or ink, MUX_DITHER_ORDERED dithers by a fixed blue-noise tile, so a rect gives the same bytes alone as in a whole frame, and MUX_DITHER_DIFFUSED is Floyd-Steinberg over the whole frame. Two formats of the same bytes are copied rect by rect under every dither, and no rects do no work.

What is refused

A segment before any moveTo(), a number that is not finite or is outside what its argument takes, a gradient that is not laid out as one, a picture from a recording whose save() and restore() do not pair, and a child that is already held or would sit under itself throw DrawListException, an InvalidArgumentException. So do a format part a layout does not read, a palette a format cannot keep, an address of 0, a stride under a row, and a rect outside its image. Its code says why, $what names the argument and $value is the number, or an entry's place in its list.

A call the store cannot act on (a method on a spent recording, a segment added to a recorded path, replay() onto an object without a method for a call) is an Error.

Testing

composer install
vendor/bin/pest

License

MIT. See LICENSE.