blob: f5fad0086ff0248eaf5a84dfa9dc97c64543bb60 [file] [log] [blame]
mtklein281b33f2016-07-12 15:01:26 -07001/*
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
Mike Klein0a76b412017-05-22 12:01:59 -040011#include "SkArenaAlloc.h"
Mike Kleind37d5d92016-12-14 13:38:24 +000012#include "SkImageInfo.h"
mtklein281b33f2016-07-12 15:01:26 -070013#include "SkNx.h"
Mike Klein16776df2017-08-03 10:22:42 -040014#include "SkPM4f.h"
mtklein281b33f2016-07-12 15:01:26 -070015#include "SkTArray.h"
16#include "SkTypes.h"
Mike Klein0a76b412017-05-22 12:01:59 -040017#include <functional>
Mike Kleincc631732016-12-06 09:17:55 -050018#include <vector>
mtklein281b33f2016-07-12 15:01:26 -070019
Mike Klein3b92b692017-07-18 11:30:25 -040020struct SkJumper_Engine;
Mike Kleinb24704d2017-05-24 07:53:00 -040021
mtklein281b33f2016-07-12 15:01:26 -070022/**
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 *
Mike Kleinb8d88d72017-06-29 12:21:22 -040031 * 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.
mtklein281b33f2016-07-12 15:01:26 -070035 *
Mike Kleinb8d88d72017-06-29 12:21:22 -040036 * If you'd like to see how this works internally, you want to start digging around src/jumper.
mtklein281b33f2016-07-12 15:01:26 -070037 */
38
Mike Klein1f49f262016-10-31 19:49:27 -040039#define SK_RASTER_PIPELINE_STAGES(M) \
Mike Klein7fee90c2017-04-07 16:55:09 -040040 M(callback) \
Mike Klein9f2b3d12017-06-27 17:26:50 -040041 M(move_src_dst) M(move_dst_src) \
Mike Reed279091e2017-06-27 16:58:00 -040042 M(clamp_0) M(clamp_1) M(clamp_a) M(clamp_a_dst) \
Mike Reed883c9bc2017-07-19 10:57:53 -040043 M(unpremul) M(premul) M(premul_dst) \
Mike Klein50d0d052017-08-08 11:29:01 -040044 M(set_rgb) M(swap_rb) M(invert) \
Mike Reed279091e2017-06-27 16:58:00 -040045 M(from_srgb) M(from_srgb_dst) M(to_srgb) \
Mike Reedc91e3872017-07-05 14:12:37 -040046 M(black_color) M(white_color) M(uniform_color) \
47 M(seed_shader) M(dither) \
Mike Klein111f8a92017-06-27 15:56:32 -040048 M(load_a8) M(load_a8_dst) M(store_a8) M(gather_a8) \
49 M(load_g8) M(load_g8_dst) M(gather_g8) \
50 M(load_565) M(load_565_dst) M(store_565) M(gather_565) \
51 M(load_4444) M(load_4444_dst) M(store_4444) M(gather_4444) \
52 M(load_f16) M(load_f16_dst) M(store_f16) M(gather_f16) \
Mike Reed279091e2017-06-27 16:58:00 -040053 M(load_f32) M(load_f32_dst) M(store_f32) \
Mike Klein111f8a92017-06-27 15:56:32 -040054 M(load_8888) M(load_8888_dst) M(store_8888) M(gather_8888) \
55 M(load_bgra) M(load_bgra_dst) M(store_bgra) M(gather_bgra) \
Matt Sarett1da27ef2017-01-19 17:14:07 -050056 M(load_u16_be) M(load_rgb_u16_be) M(store_u16_be) \
Matt Sarett5bee0b62017-01-19 12:04:32 -050057 M(load_tables_u16_be) M(load_tables_rgb_u16_be) \
Mike Reed9959f722017-05-15 09:34:22 -040058 M(load_tables) M(load_rgba) M(store_rgba) \
Mike Kleinfb126fa2017-08-24 13:06:23 -040059 M(scale_u8) M(scale_565) M(scale_1_float) \
60 M( lerp_u8) M( lerp_565) M( lerp_1_float) \
Mike Klein1f49f262016-10-31 19:49:27 -040061 M(dstatop) M(dstin) M(dstout) M(dstover) \
62 M(srcatop) M(srcin) M(srcout) M(srcover) \
63 M(clear) M(modulate) M(multiply) M(plus_) M(screen) M(xor_) \
64 M(colorburn) M(colordodge) M(darken) M(difference) \
65 M(exclusion) M(hardlight) M(lighten) M(overlay) M(softlight) \
Mike Kleinbb338332017-05-04 12:42:52 -040066 M(hue) M(saturation) M(color) M(luminosity) \
Mike Klein50626262017-05-25 13:06:57 -040067 M(srcover_rgba_8888) \
Mike Klein06a65e22016-11-17 12:39:09 -050068 M(luminance_to_alpha) \
Mike Reed7aad8cc2017-07-05 12:33:06 -040069 M(matrix_translate) M(matrix_scale_translate) \
Mike Reed02640952017-05-19 15:32:13 -040070 M(matrix_2x3) M(matrix_3x4) M(matrix_4x5) M(matrix_4x3) \
Mike Kleinc01e7df2016-11-17 16:27:10 -050071 M(matrix_perspective) \
Mike Kleincfcf6242016-11-16 09:01:30 -050072 M(parametric_r) M(parametric_g) M(parametric_b) \
Mike Kleina07e4302017-08-09 13:51:35 -040073 M(parametric_a) M(gamma) \
raftias54761282016-12-01 13:44:07 -050074 M(table_r) M(table_g) M(table_b) M(table_a) \
Mike Kleinc17dc242017-04-20 16:21:57 -040075 M(lab_to_xyz) \
Mike Kleinf3b4e162017-09-22 15:32:59 -040076 M(mirror_x) M(repeat_x) \
77 M(mirror_y) M(repeat_y) \
Mike Klein9f85d682017-05-23 07:52:01 -040078 M(clamp_x_1) M(mirror_x_1) M(repeat_x_1) \
Mike Kleinb0b17d12016-12-09 16:25:44 -050079 M(bilinear_nx) M(bilinear_px) M(bilinear_ny) M(bilinear_py) \
80 M(bicubic_n3x) M(bicubic_n1x) M(bicubic_p1x) M(bicubic_p3x) \
81 M(bicubic_n3y) M(bicubic_n1y) M(bicubic_p1y) M(bicubic_p3y) \
Florin Malitac86e4702017-01-20 08:41:34 -050082 M(save_xy) M(accumulate) \
Herb Derby4de13042017-05-15 10:49:39 -040083 M(evenly_spaced_gradient) \
Mike Reed65331592017-05-24 16:45:34 -040084 M(gauss_a_to_rgba) M(gradient) \
Mike Klein5c7960b2017-05-11 10:59:22 -040085 M(evenly_spaced_2_stop_gradient) \
86 M(xy_to_unit_angle) \
Herb Derby090fbf82017-05-08 15:10:36 -040087 M(xy_to_radius) \
Florin Malita9026fe12017-06-29 11:03:45 -040088 M(xy_to_2pt_conical_quadratic_min) \
89 M(xy_to_2pt_conical_quadratic_max) \
90 M(xy_to_2pt_conical_linear) \
91 M(mask_2pt_conical_degenerates) M(apply_vector_mask) \
Matt Sarette522f4c2017-02-22 13:02:31 -050092 M(byte_tables) M(byte_tables_rgb) \
Mike Kleinc2f876b2017-08-09 18:23:25 -040093 M(rgb_to_hsl) M(hsl_to_rgb) \
94 M(clut_3D) M(clut_4D)
Mike Kleinaebfb452016-10-25 10:27:33 -040095
mtklein281b33f2016-07-12 15:01:26 -070096class SkRasterPipeline {
97public:
Mike Kleinb24704d2017-05-24 07:53:00 -040098 explicit SkRasterPipeline(SkArenaAlloc*);
99
100 SkRasterPipeline(const SkRasterPipeline&) = delete;
101 SkRasterPipeline(SkRasterPipeline&&) = default;
102
103 SkRasterPipeline& operator=(const SkRasterPipeline&) = delete;
104 SkRasterPipeline& operator=(SkRasterPipeline&&) = default;
105
106 void reset();
mtklein281b33f2016-07-12 15:01:26 -0700107
Mike Kleinfa9f2412016-09-29 13:40:01 -0400108 enum StockStage {
Mike Kleinaebfb452016-10-25 10:27:33 -0400109 #define M(stage) stage,
110 SK_RASTER_PIPELINE_STAGES(M)
111 #undef M
Mike Kleinfa9f2412016-09-29 13:40:01 -0400112 };
Mike Klein26bea5d2016-10-05 10:36:38 -0400113 void append(StockStage, void* = nullptr);
114 void append(StockStage stage, const void* ctx) { this->append(stage, const_cast<void*>(ctx)); }
Mike Kleinfa9f2412016-09-29 13:40:01 -0400115
mtklein9a5c47f2016-07-22 11:05:04 -0700116 // Append all stages to this pipeline.
117 void extend(const SkRasterPipeline&);
118
Mike Klein3b92b692017-07-18 11:30:25 -0400119 // Runs the pipeline in 2d from (x,y) inclusive to (x+w,y+h) exclusive.
Mike Klein45c16fa2017-07-18 18:15:13 -0400120 void run(size_t x, size_t y, size_t w, size_t h) const;
Mike Klein3b92b692017-07-18 11:30:25 -0400121
Mike Klein0a76b412017-05-22 12:01:59 -0400122 // Allocates a thunk which amortizes run() setup cost in alloc.
Mike Klein45c16fa2017-07-18 18:15:13 -0400123 std::function<void(size_t, size_t, size_t, size_t)> compile() const;
Mike Klein0a76b412017-05-22 12:01:59 -0400124
Mike Klein3928c6b2016-11-15 16:18:38 -0500125 void dump() const;
126
Mike Kleind37d5d92016-12-14 13:38:24 +0000127 // Conversion from sRGB can be subtly tricky when premultiplication is involved.
128 // Use these helpers to keep things sane.
Mike Klein8c8cb5b2017-01-06 10:21:56 -0500129 void append_from_srgb(SkAlphaType);
Mike Reed279091e2017-06-27 16:58:00 -0400130 void append_from_srgb_dst(SkAlphaType);
Mike Kleind37d5d92016-12-14 13:38:24 +0000131
Mike Klein073073e2017-08-03 09:42:53 -0400132 // Appends a stage for the specified matrix.
133 // Tries to optimize the stage by analyzing the type of matrix.
Mike Reed6b59bf42017-07-03 21:26:44 -0400134 void append_matrix(SkArenaAlloc*, const SkMatrix&);
135
Mike Klein073073e2017-08-03 09:42:53 -0400136 // Appends a stage for a constant uniform color.
137 // Tries to optimize the stage based on the color.
Mike Klein16776df2017-08-03 10:22:42 -0400138 void append_constant_color(SkArenaAlloc*, const float rgba[4]);
139
140 void append_constant_color(SkArenaAlloc* alloc, const SkPM4f& color) {
141 this->append_constant_color(alloc, color.fVec);
142 }
143 void append_constant_color(SkArenaAlloc* alloc, const SkColor4f& color) {
144 this->append_constant_color(alloc, color.vec());
145 }
Mike Reedc91e3872017-07-05 14:12:37 -0400146
Mike Kleinb24704d2017-05-24 07:53:00 -0400147 bool empty() const { return fStages == nullptr; }
Mike Klein1859f692017-05-22 08:28:45 -0400148
Mike Klein159db0a2017-07-25 12:02:13 -0400149 // Used to track if we're handling values outside [0.0f, 1.0f],
150 // and to clamp back to [0.0f, 1.0f] if so.
151 void set_clamped(bool clamped) { fClamped = clamped; }
152 void clamp_if_unclamped(SkAlphaType);
153
mtklein281b33f2016-07-12 15:01:26 -0700154private:
Mike Kleinb24704d2017-05-24 07:53:00 -0400155 struct StageList {
156 StageList* prev;
157 StockStage stage;
158 void* ctx;
159 };
160
Mike Klein3b92b692017-07-18 11:30:25 -0400161 const SkJumper_Engine& build_pipeline(void**) const;
Mike Kleinb24704d2017-05-24 07:53:00 -0400162 void unchecked_append(StockStage, void*);
Mike Kleinb24704d2017-05-24 07:53:00 -0400163
164 SkArenaAlloc* fAlloc;
165 StageList* fStages;
Mike Klein9fff1112017-05-24 11:37:52 -0400166 int fNumStages;
Mike Kleinb24704d2017-05-24 07:53:00 -0400167 int fSlotsNeeded;
Mike Klein159db0a2017-07-25 12:02:13 -0400168 bool fClamped;
mtklein281b33f2016-07-12 15:01:26 -0700169};
170
Mike Kleinb24704d2017-05-24 07:53:00 -0400171template <size_t bytes>
172class SkRasterPipeline_ : public SkRasterPipeline {
173public:
174 SkRasterPipeline_()
Florin Malita14a64302017-05-24 14:53:44 -0400175 : SkRasterPipeline(&fBuiltinAlloc) {}
Mike Kleinb24704d2017-05-24 07:53:00 -0400176
177private:
Florin Malita14a64302017-05-24 14:53:44 -0400178 SkSTArenaAlloc<bytes> fBuiltinAlloc;
Mike Kleinb24704d2017-05-24 07:53:00 -0400179};
180
181
mtklein281b33f2016-07-12 15:01:26 -0700182#endif//SkRasterPipeline_DEFINED