odin-blend2d

Odin bindings to Blend2D
Log | Files | Refs | README | LICENSE

bl_bench_backend.h (6373B)


      1 // This file is part of Blend2D project <https://blend2d.com>
      2 //
      3 // See LICENSE.md for license and copyright information
      4 // SPDX-License-Identifier: Zlib
      5 
      6 #ifndef BL_BENCH_BACKEND_H
      7 #define BL_BENCH_BACKEND_H
      8 
      9 #include "bl_bench_backend.h"
     10 #include "jsonbuilder.h"
     11 #include "shape_data.h"
     12 
     13 #include <blend2d.h>
     14 
     15 namespace blbench {
     16 
     17 struct BenchApp;
     18 
     19 // blbench - Constants
     20 // ===================
     21 
     22 enum class BackendKind : uint32_t {
     23   kBlend2D,
     24   kAGG,
     25   kCairo,
     26   kQt,
     27   kSkia,
     28   kJUCE,
     29   kCoreGraphics,
     30 
     31   kMaxValue = kCoreGraphics
     32 };
     33 
     34 enum class TestKind : uint32_t {
     35   kFillAlignedRect,
     36   kFillSmoothRect,
     37   kFillRotatedRect,
     38   kFillSmoothRound,
     39   kFillRotatedRound,
     40   kFillTriangle,
     41   kFillPolygon10NZ,
     42   kFillPolygon10EO,
     43   kFillPolygon20NZ,
     44   kFillPolygon20EO,
     45   kFillPolygon40NZ,
     46   kFillPolygon40EO,
     47   kFillButterfly,
     48   kFillFish,
     49   kFillDragon,
     50   kFillWorld,
     51 
     52   kStrokeAlignedRect,
     53   kStrokeSmoothRect,
     54   kStrokeRotatedRect,
     55   kStrokeSmoothRound,
     56   kStrokeRotatedRound,
     57   kStrokeTriangle,
     58   kStrokePolygon10,
     59   kStrokePolygon20,
     60   kStrokePolygon40,
     61   kStrokeButterfly,
     62   kStrokeFish,
     63   kStrokeDragon,
     64   kStrokeWorld,
     65 
     66   kMaxValue = kStrokeWorld
     67 };
     68 
     69 enum class StyleKind : uint32_t {
     70   kSolid,
     71   kLinearPad,
     72   kLinearRepeat,
     73   kLinearReflect,
     74   kRadialPad,
     75   kRadialRepeat,
     76   kRadialReflect,
     77   kConic,
     78   kPatternNN,
     79   kPatternBI,
     80 
     81   kMaxValue = kPatternBI
     82 };
     83 
     84 enum class RenderOp : uint32_t {
     85   kFillNonZero,
     86   kFillEvenOdd,
     87   kStroke
     88 };
     89 
     90 static constexpr uint32_t kBackendKindCount = uint32_t(BackendKind::kMaxValue) + 1;
     91 static constexpr uint32_t kTestKindCount = uint32_t(TestKind::kMaxValue) + 1;
     92 static constexpr uint32_t kStyleKindCount = uint32_t(StyleKind::kMaxValue) + 1;
     93 static constexpr uint32_t kBenchNumSprites = 4;
     94 static constexpr uint32_t kBenchShapeSizeCount = 6;
     95 
     96 // blbench::BenchParams
     97 // ====================
     98 
     99 struct BenchParams {
    100   uint32_t screen_w;
    101   uint32_t screen_h;
    102 
    103   BLFormat format;
    104   uint32_t quantity;
    105 
    106   TestKind testKind;
    107   StyleKind style;
    108   BLCompOp comp_op;
    109   uint32_t shape_size;
    110 
    111   double stroke_width;
    112 };
    113 
    114 // blbench::BenchRandom
    115 // ====================
    116 
    117 struct BenchRandom {
    118   inline BenchRandom(uint64_t seed)
    119     : _prng(seed),
    120       _initial(seed) {}
    121 
    122   BLRandom _prng;
    123   BLRandom _initial;
    124 
    125   inline void rewind() { _prng = _initial; }
    126 
    127   inline int next_int() {
    128     return int(_prng.next_uint32() & 0x7FFFFFFFu);
    129   }
    130 
    131   inline int next_int(int a, int b) {
    132     return int(next_double(double(a), double(b)));
    133   }
    134 
    135   inline double next_double() {
    136     return _prng.next_double();
    137   }
    138 
    139   inline double next_double(double a, double b) {
    140     return a + _prng.next_double() * (b - a);
    141   }
    142 
    143   inline BLPoint nextPoint(const BLSizeI& bounds) {
    144     double x = next_double(0.0, double(bounds.w));
    145     double y = next_double(0.0, double(bounds.h));
    146     return BLPoint(x, y);
    147   }
    148 
    149   inline BLPointI nextIntPoint(const BLSizeI& bounds) {
    150     int x = next_int(0, bounds.w);
    151     int y = next_int(0, bounds.h);
    152     return BLPointI(x, y);
    153   }
    154 
    155   inline void next_rect_t(BLRect& out, const BLSize& bounds, double w, double h) {
    156     double x = next_double(0.0, bounds.w - w);
    157     double y = next_double(0.0, bounds.h - h);
    158     out.reset(x, y, w, h);
    159   }
    160 
    161   inline void next_rect_t(BLRectI& out, const BLSizeI& bounds, int w, int h) {
    162     int x = next_int(0, bounds.w - w);
    163     int y = next_int(0, bounds.h - h);
    164     out.reset(x, y, w, h);
    165   }
    166 
    167   inline BLRect next_rect(const BLSize& bounds, double w, double h) {
    168     double x = next_double(0.0, bounds.w - w);
    169     double y = next_double(0.0, bounds.h - h);
    170     return BLRect(x, y, w, h);
    171   }
    172 
    173   inline BLRectI next_rect_i(const BLSizeI& bounds, int w, int h) {
    174     int x = next_int(0, bounds.w - w);
    175     int y = next_int(0, bounds.h - h);
    176     return BLRectI(x, y, w, h);
    177   }
    178 
    179   inline BLRgba32 next_rgb32() {
    180     return BLRgba32(_prng.next_uint32() | 0xFF000000u);
    181   }
    182 
    183   inline BLRgba32 next_rgba32() {
    184     return BLRgba32(_prng.next_uint32());
    185   }
    186 
    187   inline BLRgba32 next_rgba32(uint32_t mask) {
    188     return BLRgba32(_prng.next_uint32() | mask);
    189   }
    190 };
    191 
    192 // blbench::Backend
    193 // ====================
    194 
    195 struct Backend {
    196   //! Module name.
    197   char _name[64] {};
    198   //! Current parameters.
    199   BenchParams _params {};
    200   //! Current duration.
    201   uint64_t _duration {};
    202 
    203   //! Random number generator for coordinates (points or rectangles).
    204   BenchRandom _rnd_coord;
    205   //! Random number generator for colors.
    206   BenchRandom _rnd_color;
    207   //! Random number generator for extras (radius).
    208   BenchRandom _rnd_extra;
    209   //! Random number generator for sprites.
    210   uint32_t _rnd_sprite_id {};
    211 
    212   //! Blend surface (used by all modules).
    213   BLImage _surface;
    214   //! Sprites.
    215   BLImage _sprites[kBenchNumSprites];
    216 
    217   Backend();
    218   virtual ~Backend();
    219 
    220   void run(const BenchApp& app, const BenchParams& params);
    221 
    222   inline const char* name() const { return _name; }
    223 
    224   inline uint32_t nextSpriteId() {
    225     uint32_t i = _rnd_sprite_id;
    226     if (++_rnd_sprite_id >= kBenchNumSprites)
    227       _rnd_sprite_id = 0;
    228     return i;
    229   };
    230 
    231   virtual void serialize_info(JSONBuilder& json) const;
    232 
    233   virtual bool supports_comp_op(BLCompOp comp_op) const = 0;
    234   virtual bool supports_style(StyleKind style) const = 0;
    235 
    236   virtual void before_run() = 0;
    237   virtual void flush() = 0;
    238   virtual void after_run() = 0;
    239 
    240   virtual void render_rect_a(RenderOp op) = 0;
    241   virtual void render_rect_f(RenderOp op) = 0;
    242   virtual void render_rect_rotated(RenderOp op) = 0;
    243   virtual void render_round_f(RenderOp op) = 0;
    244   virtual void render_round_rotated(RenderOp op) = 0;
    245   virtual void render_polygon(RenderOp op, uint32_t complexity) = 0;
    246   virtual void render_shape(RenderOp op, ShapeData shape) = 0;
    247 };
    248 
    249 Backend* create_blend2d_backend(uint32_t thread_count = 0, uint32_t cpu_features = 0);
    250 
    251 #if defined(BL_BENCH_ENABLE_AGG)
    252 Backend* create_agg_backend();
    253 #endif // BL_BENCH_ENABLE_AGG
    254 
    255 #if defined(BL_BENCH_ENABLE_CAIRO)
    256 Backend* create_cairo_backend();
    257 #endif // BL_BENCH_ENABLE_CAIRO
    258 
    259 #if defined(BL_BENCH_ENABLE_QT)
    260 Backend* create_qt_backend();
    261 #endif // BL_BENCH_ENABLE_QT
    262 
    263 #if defined(BL_BENCH_ENABLE_SKIA)
    264 Backend* create_skia_backend();
    265 #endif // BL_BENCH_ENABLE_SKIA
    266 
    267 #if defined(BL_BENCH_ENABLE_COREGRAPHICS)
    268 Backend* create_cg_backend();
    269 #endif // BL_BENCH_ENABLE_COREGRAPHICS
    270 
    271 #if defined(BL_BENCH_ENABLE_JUCE)
    272 Backend* create_juce_backend();
    273 #endif // BL_BENCH_ENABLE_JUCE
    274 
    275 
    276 } // {blbench}
    277 
    278 #endif // BL_BENCH_BACKEND_H