#pragma once #include "image_renderer.h" #include "include/internal/cef_types_wrappers.h" #include #include #include #include // Sixel graphics backend backed by libsixel, the library behind img2sixel. // Used when peak runs in a terminal that speaks the sixel protocol (e.g. via // SSH or an xterm-compatible emulator) and /dev/fb0 is not available. This is // the same rendering engine the original brow6el fork used: the terminal-sized // BGRA8888 page frame is color-quantized (256 colors, high-quality palette // construction + automatic diffusion dithering) and streamed to STDOUT as a // single "\x1bPq ... \x1b\\" DCS sequence per image. // // Two paths: // - monolithic (default): the whole frame is re-encoded on every paint. // - tiled: the frame is split into full-height column strips; each strip is // compared against the last emitted frame and only strips whose pixels // actually changed (plus strips touched by CEF dirty rects) are re-encoded. // Because every strip spans the full height, sixel's 6-row band packing // can never put a partially filled band on screen. class SixelRenderer : public ImageRenderer { public: // width/height are the terminal geometry in pixels (querySixelGeometry, // termInfo.width/height); cell_width/cell_height are kept for interface // parity with FbRenderer. SixelRenderer(int width, int height, int cell_width, int cell_height); ~SixelRenderer() override; void render(const void *buffer, int width, int height, bool hasAlpha, const std::vector &dirtyRects = {}) override; void clear() override; void setTiledRenderingEnabled(bool enabled) override { tiled_rendering_enabled_ = enabled; // Prime the frame cache with one full render when toggling on so the // pixel comparison has a valid reference. force_full_render_ = true; } void forceFullRender() override { force_full_render_ = true; } void resetFrameCache() override { last_frame_.clear(); } static std::mutex &getTerminalMutex(); private: // libsixel writer callback: streams encoded chunks to stdout. static int sixelWriteCallback(char *data, int size, void *priv); // Quantize + encode one contiguous BGRA8888 image and push it to the // output object (emits the full sixel DCS envelope). void encodeImage(const unsigned char *pixels, int w, int h); // True when the given x/y/w/h region of `frame` differs from the cached // last-emitted frame. bool isTileDirty(const unsigned char *frame, int x0, int y0, int w, int h); // Encode one full-height strip [x0, x1) and record it in the frame cache. void renderTile(const unsigned char *frame, int x0, int x1); // Encode the whole frame at the top of the terminal. void renderMonolithic(const unsigned char *frame); void *output_; // sixel_output_t* // Terminal-sized copy of the last frame that was actually emitted; used as // the change-detection reference in tiled mode. std::vector last_frame_; int width_; int height_; int cell_width_; int cell_height_; int tile_w_; bool tiled_rendering_enabled_ = false; bool force_full_render_ = false; };