menios icon indicating copy to clipboard operation
menios copied to clipboard

Port Pixman pixel manipulation library to meniOS

Open pbalduino opened this issue 3 months ago • 0 comments

Summary

Port the Pixman pixel manipulation library to meniOS. Pixman is a required dependency for Cairo, providing low-level pixel operations, compositing, and SIMD-optimized rasterization.

Background

Pixman is a low-level software library for pixel manipulation, providing:

  • Pixel compositing and blending
  • Image transformations
  • Rasterization of geometric shapes
  • SIMD optimizations (SSE2, SSSE3, AVX2)
  • Used by Cairo, X.org, and many other graphics projects

Version: Pixman 0.42.2 (latest stable) Language: C (~50,000 lines) License: MIT License Dependencies: None (standalone)

Why Pixman?

Pixman is required by Cairo. Cairo uses Pixman for all its low-level pixel operations and compositing. Without Pixman, Cairo cannot function.

Pixman provides:

  • Fast software rasterization
  • Porter-Duff compositing operators
  • Bilinear/bicubic filtering
  • SIMD acceleration for x86-64
  • Solid fills, gradients, patterns

Goals

Primary

  • Port Pixman to compile on meniOS
  • Enable core pixel operations and compositing
  • Support image surfaces (ARGB32, RGB24, A8)
  • Enable SIMD optimizations for x86-64 (SSE2, AVX2)

Secondary

  • Performance optimization
  • Memory usage optimization
  • Integration testing with Cairo

Implementation Plan

Phase 1: Build System Integration (3-5 days)

Goal: Get Pixman to compile

Tasks:

  • [ ] Download Pixman 0.42.2 source
  • [ ] Place in `vendor/pixman-0.42.2/`
  • [ ] Create Makefile for meniOS build
  • [ ] Configure build options
  • [ ] Resolve compilation errors

Build Configuration: ```makefile

Makefile for Pixman

PIXMAN_SRC := vendor/pixman-0.42.2 PIXMAN_CFLAGS := -DPIXMAN_NO_TLS \ -DUSE_SSE2 \ -DUSE_SSSE3 \ -msse2 -mssse3 \ -I$(PIXMAN_SRC)/pixman

PIXMAN_SRCS := $(wildcard $(PIXMAN_SRC)/pixman/*.c) PIXMAN_OBJS := $(PIXMAN_SRCS:.c=.o)

libpixman-1.a: $(PIXMAN_OBJS) $(AR) rcs $@ $^ ```

Configuration Options:

  • Enable SSE2/SSSE3/AVX2 for x86-64
  • Disable thread-local storage (use `PIXMAN_NO_TLS`)
  • Disable GCC vector extensions if needed
  • No external dependencies

Phase 2: Core Functionality (1 week)

Goal: Get basic pixel operations working

Tasks:

  • [ ] Test image creation and destruction
  • [ ] Test solid fills
  • [ ] Test basic compositing (SRC, OVER operators)
  • [ ] Verify pixel format conversions
  • [ ] Test image copying

Test Code: ```c #include <pixman.h> #include <stdio.h> #include <assert.h>

void test_pixman_basic() { // Create a 100x100 ARGB32 image pixman_image_t *image = pixman_image_create_bits( PIXMAN_a8r8g8b8, 100, 100, NULL, 100 * 4);

assert(image != NULL);

// Fill with solid blue
pixman_color_t blue = { 0, 0, 0xFFFF, 0xFFFF };
pixman_image_t *solid = pixman_image_create_solid_fill(&blue);

pixman_image_composite32(
    PIXMAN_OP_SRC,
    solid,           // src
    NULL,            // mask
    image,           // dest
    0, 0,            // src x, y
    0, 0,            // mask x, y
    0, 0,            // dest x, y
    100, 100         // width, height
);

// Verify pixel
uint32_t *pixels = pixman_image_get_data(image);
assert(pixels[0] == 0xFF0000FF);  // Blue in ARGB

printf("Pixman basic test passed!\\n");

pixman_image_unref(solid);
pixman_image_unref(image);

}

int main() { test_pixman_basic(); return 0; } ```


Phase 3: SIMD Optimizations (1 week)

Goal: Enable and verify SIMD acceleration

SIMD Levels for x86-64:

  1. SSE2 - Baseline (always available on x86-64)
  2. SSSE3 - Supplemental SSE3
  3. AVX2 - Advanced Vector Extensions 2

Tasks:

  • [ ] Enable SSE2 optimizations (baseline)
  • [ ] Enable SSSE3 optimizations
  • [ ] Enable AVX2 optimizations (if CPU supports)
  • [ ] Runtime CPU detection
  • [ ] Benchmark performance vs scalar code

SIMD Configuration: ```c // Pixman automatically detects CPU features at runtime // We just need to compile with the right flags

// Makefile additions: PIXMAN_SSE2_SRCS := $(wildcard $(PIXMAN_SRC)/pixman/pixman-sse2.c) PIXMAN_SSSE3_SRCS := $(wildcard $(PIXMAN_SRC)/pixman/pixman-ssse3.c) PIXMAN_AVX2_SRCS := $(wildcard $(PIXMAN_SRC)/pixman/pixman-avx2.c)

$(PIXMAN_SSE2_SRCS:.c=.o): CFLAGS += -msse2 $(PIXMAN_SSSE3_SRCS:.c=.o): CFLAGS += -mssse3 $(PIXMAN_AVX2_SRCS:.c=.o): CFLAGS += -mavx2 ```

Performance Test: ```c #include <pixman.h> #include <time.h>

void benchmark_composite() { pixman_image_t *src = pixman_image_create_bits( PIXMAN_a8r8g8b8, 1024, 768, NULL, 1024 * 4); pixman_image_t *dst = pixman_image_create_bits( PIXMAN_a8r8g8b8, 1024, 768, NULL, 1024 * 4);

clock_t start = clock();

for (int i = 0; i < 100; i++) {
    pixman_image_composite32(
        PIXMAN_OP_OVER,
        src, NULL, dst,
        0, 0, 0, 0, 0, 0,
        1024, 768
    );
}

clock_t end = clock();
double elapsed = (double)(end - start) / CLOCKS_PER_SEC;

printf("100 composites of 1024x768: %.2f sec (%.2f fps)\\n",
       elapsed, 100.0 / elapsed);

pixman_image_unref(src);
pixman_image_unref(dst);

} ```

Expected performance (with SIMD):

  • ~60 fps for 1024x768 OVER compositing
  • 2-4x faster than scalar code

Phase 4: Advanced Features (1 week)

Goal: Support gradients, transformations, filtering

Tasks:

  • [ ] Test gradient fills (linear, radial)
  • [ ] Test image transformations (rotate, scale)
  • [ ] Test bilinear/bicubic filtering
  • [ ] Test non-opaque alpha compositing
  • [ ] Test all Porter-Duff operators

Gradient Test: ```c void test_linear_gradient() { pixman_gradient_stop_t stops[2] = { { 0x00000, { 0xFFFF, 0, 0, 0xFFFF } }, // Red { 0x10000, { 0, 0, 0xFFFF, 0xFFFF } } // Blue };

pixman_point_fixed_t p1 = { 0, 0 };
pixman_point_fixed_t p2 = { pixman_int_to_fixed(200), 0 };

pixman_image_t *gradient = pixman_image_create_linear_gradient(
    &p1, &p2, stops, 2);

pixman_image_t *dest = pixman_image_create_bits(
    PIXMAN_a8r8g8b8, 200, 100, NULL, 200 * 4);

pixman_image_composite32(
    PIXMAN_OP_SRC,
    gradient, NULL, dest,
    0, 0, 0, 0, 0, 0,
    200, 100
);

// Gradient should go from red to blue

pixman_image_unref(gradient);
pixman_image_unref(dest);

} ```

Transformation Test: ```c void test_rotation() { pixman_image_t *src = pixman_image_create_bits( PIXMAN_a8r8g8b8, 100, 100, NULL, 100 * 4); pixman_image_t *dst = pixman_image_create_bits( PIXMAN_a8r8g8b8, 200, 200, NULL, 200 * 4);

// Rotate 45 degrees
pixman_transform_t transform;
pixman_transform_init_identity(&transform);
pixman_transform_rotate(&transform, NULL,
    pixman_double_to_fixed(cos(M_PI/4)),
    pixman_double_to_fixed(sin(M_PI/4)));

pixman_image_set_transform(src, &transform);
pixman_image_set_filter(src, PIXMAN_FILTER_BILINEAR, NULL, 0);

pixman_image_composite32(
    PIXMAN_OP_SRC,
    src, NULL, dst,
    0, 0, 0, 0, 50, 50,
    100, 100
);

pixman_image_unref(src);
pixman_image_unref(dst);

} ```


Phase 5: Cairo Integration Testing (3-5 days)

Goal: Verify Pixman works correctly with Cairo

Tasks:

  • [ ] Build Cairo with Pixman
  • [ ] Run Cairo test suite
  • [ ] Verify Cairo can create surfaces
  • [ ] Verify Cairo rendering uses Pixman
  • [ ] Performance testing

Integration Test: ```c // Test that Cairo uses Pixman correctly #include <cairo/cairo.h>

int main() { cairo_surface_t *surface = cairo_image_surface_create( CAIRO_FORMAT_ARGB32, 320, 240);

cairo_t *cr = cairo_create(surface);

// This should use Pixman underneath
cairo_set_source_rgb(cr, 0.2, 0.3, 0.8);
cairo_rectangle(cr, 50, 50, 100, 100);
cairo_fill(cr);

cairo_destroy(cr);
cairo_surface_destroy(surface);

printf("Cairo+Pixman integration successful!\\n");
return 0;

} ```


Timeline

Phase Task Duration
1 Build system integration 3-5 days
2 Core functionality 1 week
3 SIMD optimizations 1 week
4 Advanced features 1 week
5 Cairo integration testing 3-5 days

Total: 3-4 weeks


Definition of Done

Core Requirements

  • [ ] Pixman library compiles on meniOS
  • [ ] Can create and destroy images
  • [ ] Basic compositing works (SRC, OVER operators)
  • [ ] Solid fills work
  • [ ] Pixel format conversions work
  • [ ] Test suite passes

SIMD Requirements

  • [ ] SSE2 optimizations enabled
  • [ ] SSSE3 optimizations enabled (if available)
  • [ ] AVX2 optimizations enabled (if available)
  • [ ] Runtime CPU detection works
  • [ ] Performance meets expectations

Advanced Requirements

  • [ ] Gradients work (linear and radial)
  • [ ] Image transformations work
  • [ ] Bilinear filtering works
  • [ ] All Porter-Duff operators work

Integration Requirements

  • [ ] Cairo compiles and links with Pixman
  • [ ] Cairo test suite passes
  • [ ] No memory leaks detected
  • [ ] Performance acceptable

Dependencies

Required

  • None (Pixman is standalone)

Optional

  • #339: Thread-safe libc - For multi-threaded usage (can use PIXMAN_NO_TLS)

Enables

  • #396: Port Cairo (CRITICAL BLOCKER) - Cairo requires Pixman
  • All GUI stack components depend indirectly on Pixman

Related Issues

See docs/road/road_to_gui.md for the complete GUI roadmap.


Resources

  • Pixman Website: https://www.pixman.org/
  • Source Code: https://gitlab.freedesktop.org/pixman/pixman
  • API Documentation: https://www.pixman.org/pixman/
  • freedesktop.org: https://www.freedesktop.org/wiki/Software/pixman/

Priority

CRITICAL - Pixman is a hard dependency for Cairo. The entire GUI stack is blocked on this.

Timeline: Should be completed FIRST, before Cairo

pbalduino avatar Oct 30 '25 22:10 pbalduino