php-io-extensions / lvgl
1:1 PHP bindings of LVGL 9.6.0, an embedded graphics library (displays with caller-owned buffers, flush callbacks and partial refresh, the software renderer's draw calls, vector graphics through its internal ThorVG, draw buffers, colors and the canvas widget), compiled into the extension
Package info
github.com/php-io-extensions/lvgl
Language:C
Type:php-ext
Ext name:ext-lvgl
pkg:composer/php-io-extensions/lvgl
Requires
- php: >=8.4
Requires (Dev)
- pestphp/pest: ^4.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-12-02 16:52:06 UTC
README
ext-lvgl
LVGL bound 1:1 into PHP 8.4+, at 9.6.0, compiled in
A small display wired to a board has no compositor and no window. It has a block of memory in its own pixel layout, 16-bit color or one bit a pixel, and a bus that sends a rectangle of it at a time. Redrawing the whole panel for every change is slow over SPI and wears an e-paper screen; drawing somewhere else and converting the result costs a copy and memory a microcontroller-sized budget does not have.
LVGL is built for that. It draws straight into the memory you hand a display, in that display's color format, keeps track of which areas changed, and calls you once for each changed area with its rectangle and the address of its pixels, so you send only those. It runs on millions of devices, is written in C, and documents every call.
The only problem? PHP can't reach it. This extension binds LVGL 9.6.0 one to one under the C names, and compiles LVGL, with its internal ThorVG for vector graphics, into the module: displays over memory you own, the PARTIAL, DIRECT and FULL render modes, the flush callback, invalidation and its events, the software renderer's draw calls (rectangles, fills, borders, shadows, lines, arcs, triangles, images, masks, gradients), vector paths, draw buffers, colors, areas, matrices and the canvas widget. Structs are typed classes, memory is addresses, and LVGL's callbacks call PHP callables. No defaults, no composites, no opinions: what LVGL documents is what you call.
Basic usage
Most ways you will interface with ext-lvgl are in two parts -
Give a display the panel's memory and a flush callable. In DIRECT mode LVGL renders into that memory itself and calls the flush callable once for each area it changed, with the address of its pixels:
lv_init(); lv_tick_set_cb(fn (): int => (int) (hrtime(true) / 1_000_000)); $memory = lv_draw_buf_create(240, 240, LV_COLOR_FORMAT_RGB565, 0); // or the panel's own memory $display = lv_display_create(240, 240); lv_display_set_color_format($display, LV_COLOR_FORMAT_RGB565); lv_display_set_buffers($display, lv_draw_buf_goto_xy($memory, 0, 0), 0, 240 * 240 * 2, LV_DISPLAY_RENDER_MODE_DIRECT); lv_display_set_flush_cb($display, function (lv_display_t $display, lv_area_t $area, int $px): void { printf("send %d,%d..%d,%d\n", $area->x1, $area->y1, $area->x2, $area->y2); // send the rectangle over the bus lv_display_flush_ready($display); });
Draw when LVGL asks, and say what changed. The screen's LV_EVENT_DRAW_MAIN hands a layer to draw on; lv_obj_invalidate_area() marks an area stale, and the next refresh renders and flushes that area alone:
$box = new lv_area_t(); lv_area_set($box, 20, 20, 99, 59); lv_obj_add_event_cb(lv_screen_active(), function (lv_event_t $e) use ($box): void { $dsc = new lv_draw_rect_dsc_t(); lv_draw_rect_dsc_init($dsc); $dsc->bg_color = lv_color_hex(0x2196F3); $dsc->radius = 8; lv_draw_rect(lv_event_get_layer($e), $dsc, $box); }, LV_EVENT_DRAW_MAIN, null); lv_refr_now($display); // the whole panel, once lv_area_set($box, 140, 20, 219, 59); lv_obj_invalidate_area(lv_screen_active(), $box); lv_refr_now($display); // only the area that changed lv_refr_now($display); // nothing changed: no flush
$ php panel.php send 0,0..239,239 send 140,20..219,59
To draw into memory with no display refresh at all, put a canvas over a draw buffer, draw on its layer, and finish it: lv_canvas_init_layer($canvas, $layer), any lv_draw_* call, lv_canvas_finish_layer($canvas, $layer).
Requirements
| OS | Linux or macOS. composer.json lists those two families. |
| PHP | 8.4 or newer, NTS or ZTS. |
| Extensions | None. |
| Libraries | None at run time. LVGL 9.6.0 and its internal ThorVG are vendored under lib/lvgl and compiled into the module, configured by src/lvgl-config/lv_conf.h. |
To build it you need a C and C++ compiler and the development files of the PHP you intend to load it into:
# Debian, Ubuntu, Raspberry Pi OS sudo apt install build-essential php8.4-dev # macOS: Xcode Command Line Tools, and Homebrew's PHP ships phpize and php-config xcode-select --install brew install php@8.4
Installation
With PHP PIE (recommended)
PIE is the PHP Foundation's installer for extensions. It downloads the tagged source from Packagist, builds it against the PHP on your PATH, installs the module and enables it. sudo where your PHP's extension directory is root-owned, as on Debian; not on a Homebrew Mac:
pie install php-io-extensions/lvgl
Manually (install script)
The repository ships two installers, one per platform. Each one builds for each PHP it targets, installs lvgl.so into that PHP's extension_dir, writes 30-lvgl.ini into its conf.d scan directory, and checks the module loads. The macOS one builds in a temporary copy of the checkout and ad-hoc signs the module so the system loads it on first use; the Debian one builds in the checkout, writes the ini into the CLI, FPM and Apache conf.d of that PHP, and cleans the build products out afterwards.
git clone https://github.com/php-io-extensions/lvgl.git cd lvgl ./install-debian-trixie.sh # Debian Trixie, Ubuntu 24.04+, Raspberry Pi OS: the php on PATH ./install-macos.sh # Homebrew php@8.4 and php@8.4-zts ./install-macos.sh /path/to/bin/php # the macOS script takes specific PHP binaries
Manually (phpize)
git clone https://github.com/php-io-extensions/lvgl.git cd lvgl phpize ./configure --enable-lvgl make -j"$(nproc 2>/dev/null || sysctl -n hw.ncpu)" sudo make install
Then enable it: add extension=lvgl.so to your php.ini, or drop that line into a file in the scan directory that php --ini reports.
Verify
php --ri lvgl also names the LVGL version and commit it was built from:
$ php --ri lvgl lvgl lvgl support => enabled Version => 0.11.0 LVGL => 9.6.0 LVGL commit => 80ca777e37a2b176770726a02e07a6fb79ef0b39
API
Function, handle, struct, field and constant names are the C names, so LVGL's documentation is the reference: 288 functions, one of them a binding addition, one stub per group in stubs/, each declared with its PHP types. SUPPORT.md lists every function of the LVGL headers this extension binds from, bound or not; the API badge is computed from it.
| Stub | Functions | What it covers |
|---|---|---|
lv_core |
40 | lv_init(), lv_deinit(), the tick and its callable, the timer handler, lv_refr_now(); objects as far as screens and events need them (create, delete, clean, position, size, background color, invalidate, the screen and layer getters); event callables, codes, targets, layers, user data and invalidated areas; lv_obj_t, lv_event_t, lv_event_dsc_t. |
lv_display |
54 | Create, delete, resolution, rotation, DPI, buffers by address and draw buffers, render mode, color format, the flush callable and lv_display_flush_ready(), antialiasing, screens and layers, display events, invalidation, buffer sizes; lv_display_t. |
lv_area |
20 | Areas and points: set, copy, measure, move, grow, align, transform, swap; lv_area_t, lv_point_t, lv_point_precise_t. |
lv_color |
41 | Colors and formats, mixing, HSV, premultiplying, the palette, gradient descriptors; lv_color_t, lv_color32_t, lv_color16_t, lv_color_hsv_t, lv_grad_dsc_t. |
lv_draw |
31 | Layers, the rectangle, fill, border, box shadow, line, arc, triangle, image and mask descriptors and their draw calls, the 1-bit and RGB565 conversion helpers, rotation; lv_layer_t and the descriptor classes. |
lv_draw_buf |
19 | Draw buffers: create, init over an address, duplicate, reshape, destroy, clear, copy, pixel addresses, stride, flags, palette; lv_draw_buf_t. |
lv_canvas |
13 | The canvas widget over a buffer or a draw buffer, pixels, palette, lv_canvas_init_layer() and lv_canvas_finish_layer(). |
lv_vector |
67 | Vector paths, lv_vector_path_append_segments() (a binding addition), the vector draw descriptor (fill, stroke, gradients, dashes, transforms) and lv_draw_vector(), drawn by LVGL's ThorVG; lv_matrix_*; lv_fpoint_t, lv_grad_stop_t, lv_matrix_t, lv_vector_path_t, lv_draw_vector_dsc_t. |
lvgl |
— | The C enum constants the bound functions take and answer: LV_COLOR_FORMAT_*, LV_DISPLAY_RENDER_MODE_*, LV_EVENT_*, LV_OPA_*, LV_VECTOR_* and the rest, and LV_GRADIENT_MAX_STOPS, the stops a gradient holds as the module is built (16), and LV_INV_BUF_SIZE, the invalid areas a display holds before one more makes it refresh the whole screen (32, lv_refr.c 339-341). |
Translations
Every C parameter is its own PHP parameter, in C order. The only differences from C:
- A struct is a
finalclass with one typed public property per field. A struct the call writes through (lv_area_set(),lv_point_transform(),lv_color_premultiply()) is read and written back; one LVGL returns is a new object. - A draw descriptor's
lv_draw_*_dsc_init()fills every property. The draw call starts its C struct from the same init call and copies the properties over it, so fields PHP does not see keep LVGL's values. An image source is anlv_draw_buf_tornull; a triangle'spis a list of threelv_point_precise_t. lv_grad_dsc_tandlv_matrix_thold the C struct inside the object, zeroed bynew; thelv_grad_*andlv_matrix_*calls set them.lv_matrix_t::$mreads and writes the struct's field,m[3][3], as nine floats in its own order; assign the whole list.- A
void *buffer is anintaddress, trusted. A pointer LVGL answers (lv_draw_buf_goto_xy(),lv_canvas_get_buf(), the flush callable's$px_map) is anintaddress. - A C array with a count is a list with the count its own parameter; the C copy holds as many entries as LVGL reads, zero past the list.
lv_layer_init(),lv_canvas_init_layer()andlv_draw_buf_init()initialize a struct their C caller declares: the parameter is by-reference and receives a new handle that owns it.- A callback is a PHP callable: the flush callable, the tick callable, and event callables with any PHP value as user data, answered by
lv_event_get_user_data(). LVGL calls them on your thread, inside the LVGL call that triggered them. lv_vector_path_append_segments()is the one binding addition. It takes a path's segments from memory at two addresses, one kind byte a segment (M,L,Q,C,Z) and the doubles they take, checks them all, then appends them with LVGL's own move, line, quad, cubic and close calls, so a path reaches LVGL in one PHP call.- Integer types and enums are
int;lv_value_precise_tandfloatarefloat. The handles arefinal, with a private constructor,pointer(): intandstatic fromPointer(int $pointer): static; the same native pointer and class in one thread is the same PHP object.
Lifetime
Dropping the last PHP reference does not free the native object. LVGL frees it, and the extension clears the handle when it does: lv_display_delete(), lv_obj_delete() and an object's deletion by its parent or display, lv_draw_buf_destroy() of a buffer LVGL allocated, the vector and layer delete calls, and lv_deinit(), which also runs at the end of each request. An lv_event_t, and a layer an event answers, hold NULL once the callback returns. When a flush callable throws, the extension marks the flush ready so LVGL returns and the exception reaches you.
Refusals
LVGL checks NULL arguments itself, and reads a NULL display as the default display, so a handle LVGL freed is passed to it as NULL. The extension refuses these calls, each of which crashes the process otherwise (tests/Probes shows each crash on a probe build; tools/sweep.php calls every function with NULL handles to find them):
- A parent layer holding NULL in
lv_draw_layer_create(), and an event holding NULL inlv_event_get_user_data():ValueError. - A negative
$countinlv_point_array_transform(), which LVGL reads as a size:ValueError. - In
lv_vector_path_append_segments(): an unknown kind, or kinds that take more numbers than$count. Either would read past what the caller described:ValueError, and the path is unchanged.
Vector drawing outside ARGB8888
A fault of LVGL 9.6.0, bound as it is. lv_draw_vector() is correct only on an LV_COLOR_FORMAT_ARGB8888 or XRGB8888 layer, and what ThorVG leaves there is premultiplied. On any other layer, ThorVG draws into a temporary ARGB8888 buffer, premultiplied (lib/lvgl/src/draw/sw/lv_draw_sw_vector.c 500-509), and _blend_draw_buf() blends it back as straight ARGB8888, for RGB565, RGB565A8 and RGB888 only (396-427). Translucent pixels in RGB565 are multiplied by their alpha twice: half-opaque red over black comes out 0x3800 (red 7 of 31). On RGB565_SWAPPED, L8, I1 and ARGB8888_PREMULTIPLIED layers, nothing is drawn.
LVGL's image blend has neither fault. Draw the vectors into an ARGB8888 draw buffer, give the same memory a second header in LV_COLOR_FORMAT_ARGB8888_PREMULTIPLIED with lv_draw_buf_init() (the software image blend reads the header's color format, lv_draw_sw_img.c 226; it does not read LV_IMAGE_FLAGS_PREMULTIPLIED), and draw that buffer with lv_draw_image() on the layer in its own format.
Extensions you might use with this one
- ext-pixman: composites and converts pixel memory between formats, by address.
- ext-thorvg: ThorVG's full C API, if you want the vector engine itself; this module's copy stays private to it.
AI-assisted development
This codebase was built with LLM assistance under human direction. Prompts, reviews, and architectural decisions were owned by a professional software engineer — the model accelerates drafting and exploration; it does not replace judgment on APIs, dependency direction, or what ships.
Agentic Documentation
This repository carries an Open Knowledge Format bundle at .okf/: Markdown with YAML frontmatter, readable by you and loadable by a coding agent. It records what the README doesn't: every function, class and constant by stub, how handles follow LVGL's own frees, how draw descriptors and lists are copied, how the flush, tick and event callbacks reach PHP, where NULL is refused and why, how to build release and probe builds and run the sweep, how to add a binding, and a log of every change that altered behavior.
Start at .okf/index.md. AGENTS.md is the short rule sheet an agent reads before touching the C, the stubs, or packaging; each rule points into the bundle for the reason behind it.
Point your agent at the repo and it has the same context the maintainer has. Point yourself at it when the README's answer is "it does X" and you want "because".
Resources
- LVGL documentation: the manual for every call this extension binds; this README only adds what PHP changes.
- LVGL 9.6.0: the upstream tag vendored under
lib/lvgl, which the stubs and SUPPORT.md are checked against. - PIE: the PHP Foundation's extension installer.
gen_stub.php: the php-src stub format.stubs/holds the API declarations; each*_arginfo.his generated from its stub.- Open Knowledge Format: the spec the
.okf/bundle follows.
Running the test suite
The suite is Pest v4: displays in DIRECT and PARTIAL modes, the flush area and address, a refresh that flushes only the invalidated area and one that flushes nothing, RGB565 output, the invalidation event, a throwing flush, events, deletion, draw calls and vectors on a canvas read back pixel by pixel, draw buffers, areas, colors, and the coverage script. Build the module, then enable it through an ini scan directory rather than -d, so the child processes the probe tests start load it too:
git clone https://github.com/php-io-extensions/lvgl.git cd lvgl phpize && ./configure --enable-lvgl && make -j"$(nproc 2>/dev/null || sysctl -n hw.ncpu)" composer install mkdir -p /tmp/lvgl-ini echo "extension=$PWD/modules/lvgl.so" > /tmp/lvgl-ini/30-lvgl.ini PHP_INI_SCAN_DIR=":/tmp/lvgl-ini" php -d memory_limit=128M vendor/bin/pest
Tests: 3 skipped, 46 passed
The three skipped tests are in tests/Probes: they run only against a probe build (./configure --enable-lvgl --enable-lvgl-probes, or LVGL_CONFIGURE=--enable-lvgl-probes ./install-macos.sh), which can turn each refusal off to show the crash it prevents. On macOS the probes run their child processes under libgmalloc; the point-count probe needs its guard pages and skips on Linux. Never install a probe build for use.
The leading : keeps your PHP's own scan directory and adds the second one. If that PHP already loads an installed lvgl.so, the suite warns Module "lvgl" is already loaded and tests the installed one instead. A module only loads in the PHP whose phpize built it: for Homebrew's ZTS build, run phpize and pass --with-php-config from /opt/homebrew/opt/php@8.4-zts/bin. Set XDEBUG_MODE=off for any timing you intend to read.
Contributing
Open an issue first for anything beyond a typo. Pull requests change a stub in stubs/ and the C under src/ together, regenerate the *_arginfo.h files with gen_stub, regenerate SUPPORT.md and the API badge with php scripts/support.php, and add a Pest test for the behavior. A function that takes a handle is swept for NULL with tools/sweep.php on a probe build, and a new refusal needs a probe test that shows the crash it prevents. CI must be green on every cell of the matrix, NTS and ZTS alike. Read AGENTS.md for the rules the code follows.
Security
ext-lvgl writes through the memory addresses it is given. See SECURITY.md for what it guards against and how to report a vulnerability.
License
MIT. See LICENSE. LVGL is MIT, and its internal ThorVG is MIT; both are compiled into the module, with their licenses under lib/lvgl.