blob: acbabcbadc0d7b17af5ab616e59591c62d2a61df [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
11#include "SkNx.h"
12#include "SkTArray.h"
13#include "SkTypes.h"
14
15/**
16 * SkRasterPipeline provides a cheap way to chain together a pixel processing pipeline.
17 *
18 * It's particularly designed for situations where the potential pipeline is extremely
19 * combinatoric: {N dst formats} x {M source formats} x {K mask formats} x {C transfer modes} ...
20 * No one wants to write specialized routines for all those combinations, and if we did, we'd
21 * end up bloating our code size dramatically. SkRasterPipeline stages can be chained together
22 * at runtime, so we can scale this problem linearly rather than combinatorically.
23 *
24 * Each stage is represented by a function conforming to a common interface, SkRasterPipeline::Fn,
25 * and by an arbitrary context pointer. Fn's arguments, and sometimes custom calling convention,
26 * are designed to maximize the amount of data we can pass along the pipeline cheaply.
27 * On many machines all arguments stay in registers the entire time.
28 *
29 * The meaning of the arguments to Fn are sometimes fixed...
30 * - The Stage* always represents the current stage, mainly providing access to ctx().
31 * - The size_t is always the destination x coordinate. If you need y, put it in your context.
32 * - By the time the shader's done, the first four vectors should hold source red,
33 * green, blue, and alpha, up to 4 pixels' worth each.
34 *
35 * ...and sometimes flexible:
36 * - In the shader, the first four vectors can be used for anything, e.g. sample coordinates.
37 * - The last four vectors are scratch registers that can be used to communicate between
38 * stages; transfer modes use these to hold the original destination pixel components.
39 *
40 * On some platforms the last four vectors are slower to work with than the other arguments.
41 *
42 * When done mutating its arguments and/or context, a stage can either:
43 * 1) call st->next() with its mutated arguments, chaining to the next stage of the pipeline; or
44 * 2) return, indicating the pipeline is complete for these pixels.
45 *
46 * Some obvious stages that typically return are those that write a color to a destination pointer,
47 * but any stage can short-circuit the rest of the pipeline by returning instead of calling next().
48 */
49
50class SkRasterPipeline {
51public:
52 struct Stage;
53 using Fn = void(SK_VECTORCALL *)(Stage*, size_t, Sk4f,Sk4f,Sk4f,Sk4f,
54 Sk4f,Sk4f,Sk4f,Sk4f);
55 struct Stage {
56 template <typename T>
57 T ctx() { return static_cast<T>(fCtx); }
58
59 void SK_VECTORCALL next(size_t x, Sk4f v0, Sk4f v1, Sk4f v2, Sk4f v3,
60 Sk4f v4, Sk4f v5, Sk4f v6, Sk4f v7) {
61 // Stages are logically a pipeline, and physically are contiguous in an array.
62 // To get to the next stage, we just increment our pointer to the next array element.
63 fNext(this+1, x, v0,v1,v2,v3, v4,v5,v6,v7);
64 }
65
66 // It makes next() a good bit cheaper if we hold the next function to call here,
67 // rather than logically simpler choice of the function implementing this stage.
68 Fn fNext;
69 void* fCtx;
70 };
71
72
73 SkRasterPipeline();
74
mtklein9a5c47f2016-07-22 11:05:04 -070075 // Run the pipeline constructed with append(), walking x through [x,x+n),
mtklein281b33f2016-07-12 15:01:26 -070076 // generally in 4 pixel steps, but sometimes 1 pixel at a time.
mtklein9a5c47f2016-07-22 11:05:04 -070077 void run(size_t x, size_t n);
78 void run(size_t n) { this->run(0, n); }
mtklein281b33f2016-07-12 15:01:26 -070079
80 // Use this append() if your stage is sensitive to the number of pixels you're working with:
81 // - body will always be called for a full 4 pixels
82 // - tail will always be called for a single pixel
83 // Typically this is only an essential distintion for stages that read or write memory.
84 void append(Fn body, const void* body_ctx,
85 Fn tail, const void* tail_ctx);
86
87 // Most stages don't actually care if they're working on 4 or 1 pixel.
88 void append(Fn fn, const void* ctx = nullptr) {
89 this->append(fn, ctx, fn, ctx);
90 }
91
92 // Most 4 pixel or 1 pixel variants share the same context pointer.
93 void append(Fn body, Fn tail, const void* ctx = nullptr) {
94 this->append(body, ctx, tail, ctx);
95 }
96
mtklein9a5c47f2016-07-22 11:05:04 -070097 // Append all stages to this pipeline.
98 void extend(const SkRasterPipeline&);
99
mtklein281b33f2016-07-12 15:01:26 -0700100private:
101 using Stages = SkSTArray<10, Stage, /*MEM_COPY=*/true>;
102
mtklein0abddf72016-07-13 08:22:20 -0700103 // This no-op default makes fBodyStart and fTailStart unconditionally safe to call,
104 // and is always the last stage's fNext as a sort of safety net to make sure even a
105 // buggy pipeline can't walk off its own end.
106 static void SK_VECTORCALL JustReturn(Stage*, size_t, Sk4f,Sk4f,Sk4f,Sk4f,
107 Sk4f,Sk4f,Sk4f,Sk4f);
108
mtklein281b33f2016-07-12 15:01:26 -0700109 Stages fBody,
110 fTail;
mtklein0abddf72016-07-13 08:22:20 -0700111 Fn fBodyStart = &JustReturn,
112 fTailStart = &JustReturn;
mtklein281b33f2016-07-12 15:01:26 -0700113};
114
115#endif//SkRasterPipeline_DEFINED