KryonOSDocs
Graphics & UI

Display & 2D Drawing Pipeline

Hardware-accelerated RGB565 primitives, text rendering, and double-buffered sprites.

3. Display Drawing Pipeline

KryonOS uses a direct-to-glass rendering pipeline without double-buffering. Calling shape-drawing functions directly overwrites pixels on the TFT screen.

Color Engine

The ESP32 TFT uses the high-performance 16-bit RGB565 color format. You can define colors directly via Hex (e.g. 0xF800 for Red), or use the color conversion API.

System.color(r, g, b)

  • Parameters: r, g, b (Integers 0-255)
  • Returns: Integer (16-bit packed color)
  • Description: Packs 24-bit 8/8/8 RGB color values into the 16-bit 5/6/5 RGB format expected by the hardware.

Graphics APIs

System.fillScreen(color)

  • Parameters: color (16-bit Integer)
  • Description: Floods the entire screen with a single color. Extremely fast as it bypasses the pixel loop and uses hardware SPI DMA directly.

System.drawPixel(x, y, color)

  • Parameters: x (Int), y (Int), color (16-bit Int)
  • Description: Renders a single pixel.

System.drawLine(x1, y1, x2, y2, color)

  • Parameters: x1, y1, x2, y2 (Ints), color (16-bit Int)
  • Description: Uses Bresenham's line algorithm to render a straight line between two points.

System.drawRect(x, y, w, h, color)

System.fillRect(x, y, w, h, color)

  • Parameters: x, y (Top-Left coords), w (Width), h (Height), color (16-bit Int)
  • Description: Draws hollow or filled rectangles.

System.drawRoundRect(x, y, w, h, radius, color)

System.fillRoundRect(x, y, w, h, radius, color)

  • Parameters: x (Int), y (Int), w (Int), h (Int), radius (Int), color (Int)
  • Description: Fills a rectangle with rounded corners using the specified color.

Display.drawImage(path, x, y, [options]) / System.drawImage(...)

  • Parameters:
    • path (String): Path to the image file on /sd/ or /local/ (e.g. "/sd/photos/nature.png" or "/local/icons/app.jpg").
    • x (Int), y (Int): Screen coordinates where the top-left of the image will be drawn.
    • options (Object | Number, optional):
      • When passed as a Number: Downscaling factor (e.g. 1, 2, 4, 8).
      • When passed as an Object: { scale: 1|2|4|8, autoFit: true|false, maxWidth: Int, maxHeight: Int }.
  • Returns: Boolean (true if successfully decoded and rendered, false otherwise).
  • Description: High-level unified image renderer with intelligent format auto-detection (PNG, JPEG, BMP). Automatically sniffs magic bytes or file extension and routes to hardware-accelerated decoders (PNGdec, TJpg_Decoder, BMP stream engine). Seamlessly renders to active double-buffer sprites or physical TFT glass.

Display.drawPNG(path, x, y, [enableAlpha]) / System.drawPNG(...)

  • Parameters:
    • path (String): Path to .png file.
    • x (Int), y (Int): Coordinates.
    • enableAlpha (Boolean, optional, default: true): When true, enables full 8-bit alpha channel transparency and 1-bit palette transparency masking with background pixel blending (blendRGB565). When false, treats image as fully opaque for maximum rendering speed.
  • Returns: Boolean (true if successful).
  • Description: Native hardware-accelerated PNG decoder utilizing ESP32-S3 SIMD instructions. Reads scanlines and blends translucent pixels directly with the existing framebuffer.

Display.drawJPG(path, x, y, [scale]) / Display.drawJPEG(...)

  • Parameters:
    • path (String): Path to .jpg or .jpeg file.
    • x (Int), y (Int): Coordinates.
    • scale (Int, optional, default: 1): Downscale ratio (1 = 1:1 original, 2 = 1/2, 4 = 1/4, 8 = 1/8). Enables instantaneous rendering of multi-megapixel camera photos directly onto the 320x240 screen without running out of RAM.
  • Returns: Boolean (true if successful).
  • Description: Fast MCU block-based streaming JPEG decoder. Bypasses JavaScript heap entirely, streaming 16x16 pixel blocks directly over SPI/DMA.

Display.drawBMP(path, x, y) / System.drawBMP(...)

  • Parameters: path (String), x (Int), y (Int)
  • Returns: Boolean (true if successful, false if unsupported or file missing)
  • Description: Reads a 16-bit, 24-bit, or 32-bit .bmp image from the FileSystem (/sd/ or /local/) and streams the pixel data directly to the TFT display or active sprite at coordinates x, y.

Display.getImageInfo(path) / System.getImageInfo(...)

  • Parameters: path (String)
  • Returns: Object:
    • valid (Boolean): Whether the image file header is valid and readable.
    • width (Int): Image pixel width.
    • height (Int): Image pixel height.
    • type (String): "png", "jpeg", "bmp", or "unknown".
    • bpp (Int): Bits per pixel (e.g. 24, 32, 16, 8).
    • hasAlpha (Boolean): true if file includes an alpha transparency channel.
  • Description: Ultra-fast pre-flight metadata parser. Inspects header chunks without decompressing the full image payload into RAM.

Unified Display and Screen Global Objects For maximum convenience, all drawing methods (fillScreen, fillRect, drawRect, drawLine, drawPixel, drawCircle, fillCircle, drawTriangle, fillTriangle, drawRoundRect, fillRoundRect, drawString, setTextColor, setTextSize, color, width, height, createSprite, pushSprite, drawImage, drawPNG, drawJPG, drawBMP, getImageInfo) are accessible globally and on both Display and Screen objects:

// All of the following are valid and equivalent:
Display.drawImage("/sd/wallpaper.png", 0, 0);
Screen.drawPNG("/local/icon.png", 10, 10);
drawImage("/sd/photo.jpg", 0, 0, 2);
System.drawImage("/sd/photo.jpg", 0, 0);

System.drawCircle(x, y, radius, color)

System.fillCircle(x, y, radius, color)

  • Parameters: x, y (Center coords), radius (Int), color (16-bit Int)
  • Description: Renders perfect hollow or filled circles.

System.drawTriangle(x1, y1, x2, y2, x3, y3, color)

System.fillTriangle(x1, y1, x2, y2, x3, y3, color)

  • Parameters: x1, y1, x2, y2, x3, y3 (Vertex coords), color (16-bit Int)
  • Description: Renders hollow or filled triangles. Useful for 3D projections or UI indicators.

System.drawFastVLine(x, y, h, color)

  • Parameters: x, y (Start coords), h (Height), color (16-bit Int)
  • Description: Hardware-accelerated vertical line drawing. Substantially faster than System.fillRect() for rendering raycaster slices.

System.drawFastHLine(x, y, w, color)

  • Parameters: x, y (Start coords), w (Width), color (16-bit Int)
  • Description: Hardware-accelerated horizontal line drawing.

Hardware Double Buffering (Mini-Sprites)

Double Buffering allows you to draw shapes invisibly to an off-screen RAM buffer (a Sprite) and then "push" the completed frame to the physical screen in a single instant hardware DMA transfer. This completely eliminates 3D screen flickering.

Severe RAM Limitations & Heap Fragmentation The ESP32-WROOM has very limited contiguous RAM (~320KB total, but due to fragmentation from WiFi/WebManager, the max allocatable block is often under ~30KB). Allocating a massive buffer (e.g. 240x320 at 16-bit color takes 153.6 KB) will cause the engine to instantly return false from System.createSprite. Always keep your buffers as small as possible. The recommended architecture is Sliced Rendering: divide the screen into small horizontal slices (e.g. 10 slices of 32px height) or vertical columns. System.createSprite() now automatically triggers aggressive Garbage Collection to defragment memory before allocation, and will automatically fall back to lower-quality 8-bit color to prevent crashes if RAM is too fragmented for 16-bit color.

System.createSprite(width, height)

  • Parameters: width, height (Integer)
  • Returns: Boolean (true if successfully allocated a 16-bit or 8-bit color buffer in RAM, false if RAM exhausted)
  • Description: Allocates a persistent off-screen Sprite buffer in RAM. Automatically forces GC and falls back to 8-bit color to secure contiguous memory.

System.bindSprite(enabled)

  • Parameters: enabled (Boolean)
  • Description: If true, ALL subsequent System.draw... and System.fill... API calls are automatically intercepted and drawn invisibly to the persistent Sprite instead of the screen. If false, resumes drawing directly to the TFT.

System.pushSprite(x, y)

  • Parameters: x, y (Integer - Top-left coordinates to paste the buffer on the physical screen)
  • Description: Pushes the entire hidden buffer onto the physical screen instantly via DMA. The buffer remains in RAM and can be modified and pushed again.

System.deleteSprite()

  • Description: Instantly destroys the Sprite and frees the RAM. You must call this when you are done to prevent severe memory leaks!

Text APIs

System.setTextColor(fg_color, bg_color)

  • Parameters: fg_color (Foreground), bg_color (Background)
  • Description: Sets the active text rendering colors. Providing a bg_color enables hardware-level text overwriting, wiping the previous pixels completely without needing to draw a rectangle manually.

System.setTextSize(size)

  • Parameters: size (Integer 1-5)
  • Description: Multiplies the default pixel-font scaling.

System.drawString(text, x, y, font)

  • Parameters:
    • text (String) - Text to render.
    • x, y (Ints) - Top-Left coordinate to begin rendering.
    • font (Integer 1, 2, or 4) - Hardware font selection. 2 is standard, 4 is bold/large.
  • Description: Renders high-speed string buffers to the display.