blob: ac1e3b926a7062729753120e43ee21cb73bbd846 [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 Kleind37d5d92016-12-14 13:38:24 +000011#include "SkImageInfo.h"
mtklein281b33f2016-07-12 15:01:26 -070012#include "SkNx.h"
13#include "SkTArray.h"
14#include "SkTypes.h"
Mike Kleine9f74b82016-10-25 13:31:21 -040015#include <functional>
Mike Kleincc631732016-12-06 09:17:55 -050016#include <vector>
mtklein281b33f2016-07-12 15:01:26 -070017
18/**
19 * SkRasterPipeline provides a cheap way to chain together a pixel processing pipeline.
20 *
21 * It's particularly designed for situations where the potential pipeline is extremely
22 * combinatoric: {N dst formats} x {M source formats} x {K mask formats} x {C transfer modes} ...
23 * No one wants to write specialized routines for all those combinations, and if we did, we'd
24 * end up bloating our code size dramatically. SkRasterPipeline stages can be chained together
25 * at runtime, so we can scale this problem linearly rather than combinatorically.
26 *
27 * Each stage is represented by a function conforming to a common interface, SkRasterPipeline::Fn,
28 * and by an arbitrary context pointer. Fn's arguments, and sometimes custom calling convention,
29 * are designed to maximize the amount of data we can pass along the pipeline cheaply.
30 * On many machines all arguments stay in registers the entire time.
31 *
Mike Kleinc8dd6bc2016-09-28 10:43:53 -040032 * The meaning of the arguments to Fn are sometimes fixed:
mtklein281b33f2016-07-12 15:01:26 -070033 * - The Stage* always represents the current stage, mainly providing access to ctx().
Mike Kleinc8dd6bc2016-09-28 10:43:53 -040034 * - The first size_t is always the destination x coordinate.
35 * (If you need y, put it in your context.)
36 * - The second size_t is always tail: 0 when working on a full 4-pixel slab,
37 * or 1..3 when using only the bottom 1..3 lanes of each register.
mtklein281b33f2016-07-12 15:01:26 -070038 * - By the time the shader's done, the first four vectors should hold source red,
39 * green, blue, and alpha, up to 4 pixels' worth each.
40 *
Mike Kleinc8dd6bc2016-09-28 10:43:53 -040041 * Sometimes arguments are flexible:
mtklein281b33f2016-07-12 15:01:26 -070042 * - In the shader, the first four vectors can be used for anything, e.g. sample coordinates.
43 * - The last four vectors are scratch registers that can be used to communicate between
44 * stages; transfer modes use these to hold the original destination pixel components.
45 *
46 * On some platforms the last four vectors are slower to work with than the other arguments.
47 *
48 * When done mutating its arguments and/or context, a stage can either:
49 * 1) call st->next() with its mutated arguments, chaining to the next stage of the pipeline; or
50 * 2) return, indicating the pipeline is complete for these pixels.
51 *
Mike Kleinc8dd6bc2016-09-28 10:43:53 -040052 * Some stages that typically return are those that write a color to a destination pointer,
mtklein281b33f2016-07-12 15:01:26 -070053 * but any stage can short-circuit the rest of the pipeline by returning instead of calling next().
54 */
55
Mike Kleinc8dd6bc2016-09-28 10:43:53 -040056// TODO: There may be a better place to stuff tail, e.g. in the bottom alignment bits of
57// the Stage*. This mostly matters on 64-bit Windows where every register is precious.
58
Mike Klein1f49f262016-10-31 19:49:27 -040059#define SK_RASTER_PIPELINE_STAGES(M) \
Mike Kleina9312fd2016-11-16 13:38:15 -050060 M(trace) M(registers) \
Mike Klein8c8cb5b2017-01-06 10:21:56 -050061 M(move_src_dst) M(move_dst_src) M(swap) \
62 M(clamp_0) M(clamp_1) M(clamp_a) \
Mike Kleind5de0132016-11-28 09:33:02 -050063 M(unpremul) M(premul) \
Mike Klein8c8cb5b2017-01-06 10:21:56 -050064 M(set_rgb) M(swap_rb) \
65 M(from_srgb) M(to_srgb) \
raftias97524542016-12-14 13:15:05 -050066 M(from_2dot2) M(to_2dot2) \
mtkleina4a44882016-11-04 13:20:07 -070067 M(constant_color) M(store_f32) \
Mike Kleine71b1672017-01-13 07:59:23 -050068 M(load_a8) M(store_a8) \
Mike Klein8c8cb5b2017-01-06 10:21:56 -050069 M(load_565) M(store_565) \
70 M(load_f16) M(store_f16) \
71 M(load_8888) M(store_8888) \
Matt Sarett1da27ef2017-01-19 17:14:07 -050072 M(load_u16_be) M(load_rgb_u16_be) M(store_u16_be) \
Matt Sarett5bee0b62017-01-19 12:04:32 -050073 M(load_tables_u16_be) M(load_tables_rgb_u16_be) \
74 M(load_tables) M(store_tables) \
Mike Kleinbabd93e2016-11-30 16:05:10 -050075 M(scale_u8) M(scale_1_float) \
76 M(lerp_u8) M(lerp_565) M(lerp_1_float) \
Mike Klein1f49f262016-10-31 19:49:27 -040077 M(dstatop) M(dstin) M(dstout) M(dstover) \
78 M(srcatop) M(srcin) M(srcout) M(srcover) \
79 M(clear) M(modulate) M(multiply) M(plus_) M(screen) M(xor_) \
80 M(colorburn) M(colordodge) M(darken) M(difference) \
81 M(exclusion) M(hardlight) M(lighten) M(overlay) M(softlight) \
Mike Klein06a65e22016-11-17 12:39:09 -050082 M(luminance_to_alpha) \
83 M(matrix_2x3) M(matrix_3x4) M(matrix_4x5) \
Mike Kleinc01e7df2016-11-17 16:27:10 -050084 M(matrix_perspective) \
Mike Kleincfcf6242016-11-16 09:01:30 -050085 M(parametric_r) M(parametric_g) M(parametric_b) \
raftias54761282016-12-01 13:44:07 -050086 M(parametric_a) \
87 M(table_r) M(table_g) M(table_b) M(table_a) \
Mike Kleind5de0132016-11-28 09:33:02 -050088 M(color_lookup_table) M(lab_to_xyz) \
Mike Klein06a65e22016-11-17 12:39:09 -050089 M(clamp_x) M(mirror_x) M(repeat_x) \
90 M(clamp_y) M(mirror_y) M(repeat_y) \
Mike Kleinb04c3522016-11-28 11:55:58 -050091 M(gather_a8) M(gather_g8) M(gather_i8) \
92 M(gather_565) M(gather_4444) M(gather_8888) M(gather_f16) \
Mike Kleinb0b17d12016-12-09 16:25:44 -050093 M(bilinear_nx) M(bilinear_px) M(bilinear_ny) M(bilinear_py) \
94 M(bicubic_n3x) M(bicubic_n1x) M(bicubic_p1x) M(bicubic_p3x) \
95 M(bicubic_n3y) M(bicubic_n1y) M(bicubic_p1y) M(bicubic_p3y) \
Mike Klein886cf532016-12-06 11:31:25 -050096 M(save_xy) M(accumulate)
Mike Kleinaebfb452016-10-25 10:27:33 -040097
mtklein281b33f2016-07-12 15:01:26 -070098class SkRasterPipeline {
99public:
mtklein281b33f2016-07-12 15:01:26 -0700100 SkRasterPipeline();
101
Mike Kleinfa9f2412016-09-29 13:40:01 -0400102 enum StockStage {
Mike Kleinaebfb452016-10-25 10:27:33 -0400103 #define M(stage) stage,
104 SK_RASTER_PIPELINE_STAGES(M)
105 #undef M
Mike Kleinfa9f2412016-09-29 13:40:01 -0400106 };
Mike Klein26bea5d2016-10-05 10:36:38 -0400107 void append(StockStage, void* = nullptr);
108 void append(StockStage stage, const void* ctx) { this->append(stage, const_cast<void*>(ctx)); }
Mike Kleinfa9f2412016-09-29 13:40:01 -0400109
mtklein9a5c47f2016-07-22 11:05:04 -0700110 // Append all stages to this pipeline.
111 void extend(const SkRasterPipeline&);
112
Mike Kleinaf49b192016-11-15 08:52:04 -0500113 // Runs the pipeline walking x through [x,x+n), holding y constant.
Mike Kleinc789b612016-11-30 13:45:06 -0500114 void run(size_t x, size_t y, size_t n) const;
115
116 // If you're going to run() the pipeline more than once, it's best to compile it.
Mike Kleinaf49b192016-11-15 08:52:04 -0500117 std::function<void(size_t x, size_t y, size_t n)> compile() const;
Mike Kleinaebfb452016-10-25 10:27:33 -0400118
Mike Klein3928c6b2016-11-15 16:18:38 -0500119 void dump() const;
120
Mike Kleinaebfb452016-10-25 10:27:33 -0400121 struct Stage {
122 StockStage stage;
123 void* ctx;
124 };
125
Mike Kleind37d5d92016-12-14 13:38:24 +0000126 // Conversion from sRGB can be subtly tricky when premultiplication is involved.
127 // Use these helpers to keep things sane.
Mike Klein8c8cb5b2017-01-06 10:21:56 -0500128 void append_from_srgb(SkAlphaType);
Mike Kleind37d5d92016-12-14 13:38:24 +0000129
mtklein281b33f2016-07-12 15:01:26 -0700130private:
Mike Klein7f71d882017-01-06 12:00:31 -0500131 std::function<void(size_t, size_t, size_t)> jit() const;
132
Mike Kleincc631732016-12-06 09:17:55 -0500133 std::vector<Stage> fStages;
mtklein281b33f2016-07-12 15:01:26 -0700134};
135
mtklein281b33f2016-07-12 15:01:26 -0700136#endif//SkRasterPipeline_DEFINED