bl_qt_canvas.h (5899B)
1 #ifndef QT_BL_CANVAS_H_INCLUDED 2 #define QT_BL_CANVAS_H_INCLUDED 3 4 #include <blend2d.h> 5 #include <cmath> 6 #include <string.h> 7 #include <stddef.h> 8 #include <stdint.h> 9 10 #include "bl_qt_headers.h" 11 #include <functional> 12 13 class QBLCanvas : public QWidget { 14 Q_OBJECT 15 16 public: 17 QImage qt_image; 18 QImage qt_image_non_scaling; 19 BLImage bl_image; 20 21 enum RendererType : uint32_t { 22 RendererBlend2D = 0, 23 RendererBlend2D_1t = 1, 24 RendererBlend2D_2t = 2, 25 RendererBlend2D_4t = 4, 26 RendererBlend2D_8t = 8, 27 RendererBlend2D_12t = 12, 28 RendererBlend2D_16t = 16, 29 30 RendererQt = 0xFF 31 }; 32 33 uint32_t _renderer_type {}; 34 bool _dirty {}; 35 double _fps {}; 36 uint32_t _frame_count {}; 37 QElapsedTimer _elapsed_timer; 38 39 size_t _rendered_frames {}; 40 size_t _render_time_pos = 31; 41 double _render_time[32] {}; 42 43 std::function<void(BLContext& ctx)> on_render_blend2d; 44 std::function<void(QPainter& ctx)> on_render_qt; 45 std::function<void(QMouseEvent*)> on_mouse_event; 46 47 QBLCanvas(); 48 ~QBLCanvas(); 49 50 void resizeEvent(QResizeEvent* event) override; 51 void paintEvent(QPaintEvent *event) override; 52 53 void mousePressEvent(QMouseEvent* event) override; 54 void mouseReleaseEvent(QMouseEvent* event) override; 55 void mouseMoveEvent(QMouseEvent* event) override; 56 57 void set_renderer_type(uint32_t renderer_type); 58 void update_canvas(bool force = false); 59 void _resize_canvas(); 60 void _render_canvas(); 61 void _after_render(); 62 63 inline BLSizeI image_size() const { return bl_image.size(); } 64 inline int image_width() const { return bl_image.width(); } 65 inline int image_height() const { return bl_image.height(); } 66 67 inline uint32_t renderer_type() const { return _renderer_type; } 68 inline double fps() const { return _fps; } 69 70 double last_render_time() const; 71 double average_render_time() const; 72 73 static void init_renderer_select_box(QComboBox* dst, bool blend2d_only = false); 74 static QString renderer_type_to_string(uint32_t renderer_type); 75 }; 76 77 static BL_INLINE QColor bl_rgba_to_qcolor(const BLRgba32& rgba) noexcept { 78 return QColor(rgba.r(), rgba.g(), rgba.b(), rgba.a()); 79 } 80 81 static BL_INLINE QColor bl_rgba_to_qcolor(const BLRgba64& rgba) noexcept { 82 return QColor(QRgba64::fromRgba64(uint16_t(rgba.r()), uint16_t(rgba.g()), uint16_t(rgba.b()), uint16_t(rgba.a()))); 83 } 84 85 static BL_INLINE QPainter::CompositionMode bl_comp_op_to_qt_composition_mode(BLCompOp comp_op) { 86 switch (comp_op) { 87 default: 88 case BL_COMP_OP_SRC_OVER : return QPainter::CompositionMode_SourceOver; 89 case BL_COMP_OP_SRC_COPY : return QPainter::CompositionMode_Source; 90 case BL_COMP_OP_SRC_IN : return QPainter::CompositionMode_SourceIn; 91 case BL_COMP_OP_SRC_OUT : return QPainter::CompositionMode_SourceOut; 92 case BL_COMP_OP_SRC_ATOP : return QPainter::CompositionMode_SourceAtop; 93 case BL_COMP_OP_DST_OVER : return QPainter::CompositionMode_DestinationOver; 94 case BL_COMP_OP_DST_COPY : return QPainter::CompositionMode_Destination; 95 case BL_COMP_OP_DST_IN : return QPainter::CompositionMode_DestinationIn; 96 case BL_COMP_OP_DST_OUT : return QPainter::CompositionMode_DestinationOut; 97 case BL_COMP_OP_DST_ATOP : return QPainter::CompositionMode_DestinationAtop; 98 case BL_COMP_OP_XOR : return QPainter::CompositionMode_Xor; 99 case BL_COMP_OP_CLEAR : return QPainter::CompositionMode_Clear; 100 case BL_COMP_OP_PLUS : return QPainter::CompositionMode_Plus; 101 case BL_COMP_OP_MULTIPLY : return QPainter::CompositionMode_Multiply; 102 case BL_COMP_OP_SCREEN : return QPainter::CompositionMode_Screen; 103 case BL_COMP_OP_OVERLAY : return QPainter::CompositionMode_Overlay; 104 case BL_COMP_OP_DARKEN : return QPainter::CompositionMode_Darken; 105 case BL_COMP_OP_LIGHTEN : return QPainter::CompositionMode_Lighten; 106 case BL_COMP_OP_COLOR_DODGE: return QPainter::CompositionMode_ColorDodge; 107 case BL_COMP_OP_COLOR_BURN : return QPainter::CompositionMode_ColorBurn; 108 case BL_COMP_OP_HARD_LIGHT : return QPainter::CompositionMode_HardLight; 109 case BL_COMP_OP_SOFT_LIGHT : return QPainter::CompositionMode_SoftLight; 110 case BL_COMP_OP_DIFFERENCE : return QPainter::CompositionMode_Difference; 111 case BL_COMP_OP_EXCLUSION : return QPainter::CompositionMode_Exclusion; 112 } 113 } 114 115 static inline BLRgba32 bl_background_for_comp_op(BLCompOp comp_op) noexcept { 116 switch (comp_op) { 117 case BL_COMP_OP_SRC_OVER : return BLRgba32(0xFF000000); 118 case BL_COMP_OP_SRC_COPY : return BLRgba32(0xFF000000); 119 case BL_COMP_OP_SRC_IN : return BLRgba32(0xFFFFFFFF); 120 case BL_COMP_OP_SRC_OUT : return BLRgba32(0x00000000); 121 case BL_COMP_OP_SRC_ATOP : return BLRgba32(0xFFFFFFFF); 122 case BL_COMP_OP_DST_OVER : return BLRgba32(0xFFFFFFFF); 123 case BL_COMP_OP_DST_COPY : return BLRgba32(0xFF000000); 124 case BL_COMP_OP_DST_IN : return BLRgba32(0xFF000000); 125 case BL_COMP_OP_DST_OUT : return BLRgba32(0xFF000000); 126 case BL_COMP_OP_DST_ATOP : return BLRgba32(0xFF000000); 127 case BL_COMP_OP_XOR : return BLRgba32(0xFF000000); 128 case BL_COMP_OP_PLUS : return BLRgba32(0xFF000000); 129 case BL_COMP_OP_MULTIPLY : return BLRgba32(0xFFFFFFFF); 130 case BL_COMP_OP_SCREEN : return BLRgba32(0xFF000000); 131 case BL_COMP_OP_OVERLAY : return BLRgba32(0x00000000); 132 case BL_COMP_OP_DARKEN : return BLRgba32(0xFFFFFFFF); 133 case BL_COMP_OP_LIGHTEN : return BLRgba32(0xFF000000); 134 case BL_COMP_OP_COLOR_DODGE: return BLRgba32(0x00000000); 135 case BL_COMP_OP_COLOR_BURN : return BLRgba32(0x00000000); 136 case BL_COMP_OP_HARD_LIGHT : return BLRgba32(0xFF000000); 137 case BL_COMP_OP_SOFT_LIGHT : return BLRgba32(0x00000000); 138 case BL_COMP_OP_DIFFERENCE : return BLRgba32(0xFF000000); 139 case BL_COMP_OP_EXCLUSION : return BLRgba32(0xFFFFFFFF); 140 141 default: 142 return BLRgba32(0xFF000000); 143 } 144 } 145 146 #endif // QT_BL_CANVAS_H_INCLUDED