Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright 2016 Google Inc.
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 #ifndef SkRasterPipeline_DEFINED
      9 #define SkRasterPipeline_DEFINED
     10 
     11 #include "SkArenaAlloc.h"
     12 #include "SkImageInfo.h"
     13 #include "SkNx.h"
     14 #include "SkPM4f.h"
     15 #include "SkTArray.h"
     16 #include "SkTypes.h"
     17 #include <functional>
     18 #include <vector>
     19 
     20 struct SkJumper_Engine;
     21 
     22 /**
     23  * SkRasterPipeline provides a cheap way to chain together a pixel processing pipeline.
     24  *
     25  * It's particularly designed for situations where the potential pipeline is extremely
     26  * combinatoric: {N dst formats} x {M source formats} x {K mask formats} x {C transfer modes} ...
     27  * No one wants to write specialized routines for all those combinations, and if we did, we'd
     28  * end up bloating our code size dramatically.  SkRasterPipeline stages can be chained together
     29  * at runtime, so we can scale this problem linearly rather than combinatorically.
     30  *
     31  * Each stage is represented by a function conforming to a common interface and by an
     32  * arbitrary context pointer.  The stage funciton arguments and calling convention are
     33  * designed to maximize the amount of data we can pass along the pipeline cheaply, and
     34  * vary depending on CPU feature detection.
     35  *
     36  * If you'd like to see how this works internally, you want to start digging around src/jumper.
     37  */
     38 
     39 #define SK_RASTER_PIPELINE_STAGES(M)                               \
     40     M(callback)                                                    \
     41     M(move_src_dst) M(move_dst_src)                                \
     42     M(clamp_0) M(clamp_1) M(clamp_a) M(clamp_a_dst)                \
     43     M(unpremul) M(premul) M(premul_dst)                            \
     44     M(force_opaque) M(force_opaque_dst)                            \
     45     M(set_rgb) M(swap_rb) M(invert)                                \
     46     M(from_srgb) M(from_srgb_dst) M(to_srgb)                       \
     47     M(black_color) M(white_color) M(uniform_color)                 \
     48     M(seed_shader) M(dither)                                       \
     49     M(load_a8)   M(load_a8_dst)   M(store_a8)   M(gather_a8)       \
     50     M(load_g8)   M(load_g8_dst)                 M(gather_g8)       \
     51     M(load_565)  M(load_565_dst)  M(store_565)  M(gather_565)      \
     52     M(load_4444) M(load_4444_dst) M(store_4444) M(gather_4444)     \
     53     M(load_f16)  M(load_f16_dst)  M(store_f16)  M(gather_f16)      \
     54     M(load_f32)  M(load_f32_dst)  M(store_f32)                     \
     55     M(load_8888) M(load_8888_dst) M(store_8888) M(gather_8888)     \
     56     M(load_bgra) M(load_bgra_dst) M(store_bgra) M(gather_bgra)     \
     57     M(load_1010102) M(load_1010102_dst) M(store_1010102) M(gather_1010102) \
     58     M(bilerp_clamp_8888)                                           \
     59     M(load_u16_be) M(load_rgb_u16_be) M(store_u16_be)              \
     60     M(load_tables_u16_be) M(load_tables_rgb_u16_be) M(load_tables) \
     61     M(load_rgba) M(store_rgba)                                     \
     62     M(scale_u8) M(scale_565) M(scale_1_float)                      \
     63     M( lerp_u8) M( lerp_565) M( lerp_1_float)                      \
     64     M(dstatop) M(dstin) M(dstout) M(dstover)                       \
     65     M(srcatop) M(srcin) M(srcout) M(srcover)                       \
     66     M(clear) M(modulate) M(multiply) M(plus_) M(screen) M(xor_)    \
     67     M(colorburn) M(colordodge) M(darken) M(difference)             \
     68     M(exclusion) M(hardlight) M(lighten) M(overlay) M(softlight)   \
     69     M(hue) M(saturation) M(color) M(luminosity)                    \
     70     M(srcover_rgba_8888) M(srcover_bgra_8888)                      \
     71     M(luminance_to_alpha)                                          \
     72     M(matrix_translate) M(matrix_scale_translate)                  \
     73     M(matrix_2x3) M(matrix_3x4) M(matrix_4x5) M(matrix_4x3)        \
     74     M(matrix_perspective)                                          \
     75     M(parametric_r) M(parametric_g) M(parametric_b)                \
     76     M(parametric_a) M(gamma) M(gamma_dst)                          \
     77     M(table_r) M(table_g) M(table_b) M(table_a)                    \
     78     M(lab_to_xyz)                                                  \
     79                  M(mirror_x)   M(repeat_x)                         \
     80                  M(mirror_y)   M(repeat_y)                         \
     81                  M(decal_x)    M(decal_y)   M(decal_x_and_y)       \
     82     M(check_decal_mask)                                            \
     83     M(negate_x)                                                    \
     84     M(bilinear_nx) M(bilinear_px) M(bilinear_ny) M(bilinear_py)    \
     85     M(bicubic_n3x) M(bicubic_n1x) M(bicubic_p1x) M(bicubic_p3x)    \
     86     M(bicubic_n3y) M(bicubic_n1y) M(bicubic_p1y) M(bicubic_p3y)    \
     87     M(save_xy) M(accumulate)                                       \
     88     M(clamp_x_1) M(mirror_x_1) M(repeat_x_1)                       \
     89     M(evenly_spaced_gradient)                                      \
     90     M(gradient)                                                    \
     91     M(evenly_spaced_2_stop_gradient)                               \
     92     M(xy_to_unit_angle)                                            \
     93     M(xy_to_radius)                                                \
     94     M(xy_to_2pt_conical_strip)                                     \
     95     M(xy_to_2pt_conical_focal_on_circle)                           \
     96     M(xy_to_2pt_conical_well_behaved)                              \
     97     M(xy_to_2pt_conical_smaller)                                   \
     98     M(xy_to_2pt_conical_greater)                                   \
     99     M(alter_2pt_conical_compensate_focal)                          \
    100     M(alter_2pt_conical_unswap)                                    \
    101     M(mask_2pt_conical_nan)                                        \
    102     M(mask_2pt_conical_degenerates) M(apply_vector_mask)           \
    103     M(byte_tables) M(byte_tables_rgb)                              \
    104     M(rgb_to_hsl) M(hsl_to_rgb)                                    \
    105     M(clut_3D) M(clut_4D)                                          \
    106     M(gauss_a_to_rgba)
    107 
    108 class SkRasterPipeline {
    109 public:
    110     explicit SkRasterPipeline(SkArenaAlloc*);
    111 
    112     SkRasterPipeline(const SkRasterPipeline&) = delete;
    113     SkRasterPipeline(SkRasterPipeline&&)      = default;
    114 
    115     SkRasterPipeline& operator=(const SkRasterPipeline&) = delete;
    116     SkRasterPipeline& operator=(SkRasterPipeline&&)      = default;
    117 
    118     void reset();
    119 
    120     enum StockStage {
    121     #define M(stage) stage,
    122         SK_RASTER_PIPELINE_STAGES(M)
    123     #undef M
    124     };
    125     void append(StockStage, void* = nullptr);
    126     void append(StockStage stage, const void* ctx) { this->append(stage, const_cast<void*>(ctx)); }
    127 
    128     // Append all stages to this pipeline.
    129     void extend(const SkRasterPipeline&);
    130 
    131     // Runs the pipeline in 2d from (x,y) inclusive to (x+w,y+h) exclusive.
    132     void run(size_t x, size_t y, size_t w, size_t h) const;
    133 
    134     // Allocates a thunk which amortizes run() setup cost in alloc.
    135     std::function<void(size_t, size_t, size_t, size_t)> compile() const;
    136 
    137     void dump() const;
    138 
    139     // Appends a stage for the specified matrix.
    140     // Tries to optimize the stage by analyzing the type of matrix.
    141     void append_matrix(SkArenaAlloc*, const SkMatrix&);
    142 
    143     // Appends a stage for a constant uniform color.
    144     // Tries to optimize the stage based on the color.
    145     void append_constant_color(SkArenaAlloc*, const float rgba[4]);
    146 
    147     void append_constant_color(SkArenaAlloc* alloc, const SkPM4f& color) {
    148         this->append_constant_color(alloc, color.fVec);
    149     }
    150     void append_constant_color(SkArenaAlloc* alloc, const SkColor4f& color) {
    151         this->append_constant_color(alloc, color.vec());
    152     }
    153 
    154     // Helper to append(seed_shader) with the normal {+0.5,+1.5,+2.5,...} argument it expects.
    155     void append_seed_shader();
    156 
    157     bool empty() const { return fStages == nullptr; }
    158 
    159 private:
    160     struct StageList {
    161         StageList* prev;
    162         StockStage stage;
    163         void*      ctx;
    164     };
    165 
    166     const SkJumper_Engine& build_pipeline(void**) const;
    167     void unchecked_append(StockStage, void*);
    168 
    169     SkArenaAlloc* fAlloc;
    170     StageList*    fStages;
    171     int           fNumStages;
    172     int           fSlotsNeeded;
    173 };
    174 
    175 template <size_t bytes>
    176 class SkRasterPipeline_ : public SkRasterPipeline {
    177 public:
    178     SkRasterPipeline_()
    179         : SkRasterPipeline(&fBuiltinAlloc) {}
    180 
    181 private:
    182     SkSTArenaAlloc<bytes> fBuiltinAlloc;
    183 };
    184 
    185 
    186 #endif//SkRasterPipeline_DEFINED
    187