mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 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 | #include "SkRasterPipeline.h" |
Mike Reed | c91e387 | 2017-07-05 14:12:37 -0400 | [diff] [blame] | 9 | #include "SkPM4f.h" |
Mike Klein | 1a2e3e1 | 2017-08-03 11:24:13 -0400 | [diff] [blame] | 10 | #include "SkPM4fPriv.h" |
| 11 | #include "../jumper/SkJumper.h" |
mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 12 | |
Mike Klein | b24704d | 2017-05-24 07:53:00 -0400 | [diff] [blame] | 13 | SkRasterPipeline::SkRasterPipeline(SkArenaAlloc* alloc) : fAlloc(alloc) { |
| 14 | this->reset(); |
Mike Klein | 1859f69 | 2017-05-22 08:28:45 -0400 | [diff] [blame] | 15 | } |
Mike Klein | b24704d | 2017-05-24 07:53:00 -0400 | [diff] [blame] | 16 | void SkRasterPipeline::reset() { |
| 17 | fStages = nullptr; |
Mike Klein | 9fff111 | 2017-05-24 11:37:52 -0400 | [diff] [blame] | 18 | fNumStages = 0; |
Mike Klein | b24704d | 2017-05-24 07:53:00 -0400 | [diff] [blame] | 19 | fSlotsNeeded = 1; // We always need one extra slot for just_return(). |
Mike Klein | 159db0a | 2017-07-25 12:02:13 -0400 | [diff] [blame] | 20 | fClamped = true; |
Mike Klein | 1859f69 | 2017-05-22 08:28:45 -0400 | [diff] [blame] | 21 | } |
mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 22 | |
Mike Klein | 26bea5d | 2016-10-05 10:36:38 -0400 | [diff] [blame] | 23 | void SkRasterPipeline::append(StockStage stage, void* ctx) { |
Mike Klein | 16776df | 2017-08-03 10:22:42 -0400 | [diff] [blame] | 24 | SkASSERT(stage != from_srgb); // Please use append_from_srgb(). |
| 25 | SkASSERT(stage != uniform_color); // Please use append_constant_color(). |
Mike Klein | b24704d | 2017-05-24 07:53:00 -0400 | [diff] [blame] | 26 | this->unchecked_append(stage, ctx); |
| 27 | } |
| 28 | void SkRasterPipeline::unchecked_append(StockStage stage, void* ctx) { |
| 29 | fStages = fAlloc->make<StageList>( StageList{fStages, stage, ctx} ); |
Mike Klein | 9fff111 | 2017-05-24 11:37:52 -0400 | [diff] [blame] | 30 | fNumStages += 1; |
Mike Klein | b24704d | 2017-05-24 07:53:00 -0400 | [diff] [blame] | 31 | fSlotsNeeded += ctx ? 2 : 1; |
Mike Klein | fa9f241 | 2016-09-29 13:40:01 -0400 | [diff] [blame] | 32 | } |
| 33 | |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 34 | void SkRasterPipeline::extend(const SkRasterPipeline& src) { |
Mike Klein | 9fff111 | 2017-05-24 11:37:52 -0400 | [diff] [blame] | 35 | if (src.empty()) { |
Mike Klein | b24704d | 2017-05-24 07:53:00 -0400 | [diff] [blame] | 36 | return; |
| 37 | } |
Mike Klein | 9fff111 | 2017-05-24 11:37:52 -0400 | [diff] [blame] | 38 | auto stages = fAlloc->makeArrayDefault<StageList>(src.fNumStages); |
| 39 | |
| 40 | int n = src.fNumStages; |
| 41 | const StageList* st = src.fStages; |
| 42 | while (n --> 1) { |
| 43 | stages[n] = *st; |
| 44 | stages[n].prev = &stages[n-1]; |
| 45 | st = st->prev; |
| 46 | } |
| 47 | stages[0] = *st; |
| 48 | stages[0].prev = fStages; |
| 49 | |
| 50 | fStages = &stages[src.fNumStages - 1]; |
| 51 | fNumStages += src.fNumStages; |
| 52 | fSlotsNeeded += src.fSlotsNeeded - 1; // Don't double count just_returns(). |
Mike Klein | 159db0a | 2017-07-25 12:02:13 -0400 | [diff] [blame] | 53 | fClamped = fClamped && src.fClamped; |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Mike Klein | 3928c6b | 2016-11-15 16:18:38 -0500 | [diff] [blame] | 56 | void SkRasterPipeline::dump() const { |
Mike Klein | 9fff111 | 2017-05-24 11:37:52 -0400 | [diff] [blame] | 57 | SkDebugf("SkRasterPipeline, %d stages (in reverse)\n", fNumStages); |
Mike Klein | b24704d | 2017-05-24 07:53:00 -0400 | [diff] [blame] | 58 | for (auto st = fStages; st; st = st->prev) { |
Mike Klein | 3928c6b | 2016-11-15 16:18:38 -0500 | [diff] [blame] | 59 | const char* name = ""; |
Mike Klein | b24704d | 2017-05-24 07:53:00 -0400 | [diff] [blame] | 60 | switch (st->stage) { |
Mike Klein | 3928c6b | 2016-11-15 16:18:38 -0500 | [diff] [blame] | 61 | #define M(x) case x: name = #x; break; |
| 62 | SK_RASTER_PIPELINE_STAGES(M) |
| 63 | #undef M |
| 64 | } |
Mike Klein | bf49d61 | 2016-11-30 20:54:37 +0000 | [diff] [blame] | 65 | SkDebugf("\t%s\n", name); |
Mike Klein | 3928c6b | 2016-11-15 16:18:38 -0500 | [diff] [blame] | 66 | } |
| 67 | SkDebugf("\n"); |
| 68 | } |
Mike Klein | d37d5d9 | 2016-12-14 13:38:24 +0000 | [diff] [blame] | 69 | |
Mike Reed | c91e387 | 2017-07-05 14:12:37 -0400 | [diff] [blame] | 70 | //#define TRACK_COLOR_HISTOGRAM |
| 71 | #ifdef TRACK_COLOR_HISTOGRAM |
| 72 | static int gBlack; |
| 73 | static int gWhite; |
| 74 | static int gColor; |
| 75 | #define INC_BLACK gBlack++ |
| 76 | #define INC_WHITE gWhite++ |
| 77 | #define INC_COLOR gColor++ |
| 78 | #else |
| 79 | #define INC_BLACK |
| 80 | #define INC_WHITE |
| 81 | #define INC_COLOR |
| 82 | #endif |
| 83 | |
Mike Klein | 16776df | 2017-08-03 10:22:42 -0400 | [diff] [blame] | 84 | void SkRasterPipeline::append_constant_color(SkArenaAlloc* alloc, const float rgba[4]) { |
Mike Klein | f757ae6 | 2017-09-15 14:57:02 -0400 | [diff] [blame] | 85 | SkASSERT(0 <= rgba[0] && rgba[0] <= 1); |
| 86 | SkASSERT(0 <= rgba[1] && rgba[1] <= 1); |
| 87 | SkASSERT(0 <= rgba[2] && rgba[2] <= 1); |
| 88 | SkASSERT(0 <= rgba[3] && rgba[3] <= 1); |
| 89 | |
Mike Klein | 16776df | 2017-08-03 10:22:42 -0400 | [diff] [blame] | 90 | if (rgba[0] == 0 && rgba[1] == 0 && rgba[2] == 0 && rgba[3] == 1) { |
Mike Reed | c91e387 | 2017-07-05 14:12:37 -0400 | [diff] [blame] | 91 | this->append(black_color); |
| 92 | INC_BLACK; |
Mike Klein | 16776df | 2017-08-03 10:22:42 -0400 | [diff] [blame] | 93 | } else if (rgba[0] == 1 && rgba[1] == 1 && rgba[2] == 1 && rgba[3] == 1) { |
Mike Reed | c91e387 | 2017-07-05 14:12:37 -0400 | [diff] [blame] | 94 | this->append(white_color); |
| 95 | INC_WHITE; |
| 96 | } else { |
Mike Klein | 1a2e3e1 | 2017-08-03 11:24:13 -0400 | [diff] [blame] | 97 | auto ctx = alloc->make<SkJumper_UniformColorCtx>(); |
| 98 | Sk4f color = Sk4f::Load(rgba); |
| 99 | color.store(&ctx->r); |
Mike Klein | f757ae6 | 2017-09-15 14:57:02 -0400 | [diff] [blame] | 100 | |
| 101 | // To make loads more direct, we store 8-bit values in 16-bit slots. |
| 102 | color = color * 255.0f + 0.5f; |
| 103 | ctx->rgba[0] = (uint16_t)color[0]; |
| 104 | ctx->rgba[1] = (uint16_t)color[1]; |
| 105 | ctx->rgba[2] = (uint16_t)color[2]; |
| 106 | ctx->rgba[3] = (uint16_t)color[3]; |
Mike Klein | 1a2e3e1 | 2017-08-03 11:24:13 -0400 | [diff] [blame] | 107 | |
| 108 | this->unchecked_append(uniform_color, ctx); |
Mike Reed | c91e387 | 2017-07-05 14:12:37 -0400 | [diff] [blame] | 109 | INC_COLOR; |
| 110 | } |
| 111 | |
| 112 | #ifdef TRACK_COLOR_HISTOGRAM |
| 113 | SkDebugf("B=%d W=%d C=%d\n", gBlack, gWhite, gColor); |
| 114 | #endif |
| 115 | } |
| 116 | |
| 117 | #undef INC_BLACK |
| 118 | #undef INC_WHITE |
| 119 | #undef INC_COLOR |
| 120 | |
Mike Klein | d37d5d9 | 2016-12-14 13:38:24 +0000 | [diff] [blame] | 121 | // It's pretty easy to start with sound premultiplied linear floats, pack those |
| 122 | // to sRGB encoded bytes, then read them back to linear floats and find them not |
| 123 | // quite premultiplied, with a color channel just a smidge greater than the alpha |
| 124 | // channel. This can happen basically any time we have different transfer |
| 125 | // functions for alpha and colors... sRGB being the only one we draw into. |
| 126 | |
| 127 | // This is an annoying problem with no known good solution. So apply the clamp hammer. |
| 128 | |
| 129 | void SkRasterPipeline::append_from_srgb(SkAlphaType at) { |
Mike Klein | b24704d | 2017-05-24 07:53:00 -0400 | [diff] [blame] | 130 | this->unchecked_append(from_srgb, nullptr); |
Mike Klein | d37d5d9 | 2016-12-14 13:38:24 +0000 | [diff] [blame] | 131 | if (at == kPremul_SkAlphaType) { |
| 132 | this->append(SkRasterPipeline::clamp_a); |
| 133 | } |
| 134 | } |
Mike Reed | 279091e | 2017-06-27 16:58:00 -0400 | [diff] [blame] | 135 | |
| 136 | void SkRasterPipeline::append_from_srgb_dst(SkAlphaType at) { |
| 137 | this->unchecked_append(from_srgb_dst, nullptr); |
| 138 | if (at == kPremul_SkAlphaType) { |
| 139 | this->append(SkRasterPipeline::clamp_a_dst); |
| 140 | } |
| 141 | } |
Mike Reed | 6b59bf4 | 2017-07-03 21:26:44 -0400 | [diff] [blame] | 142 | |
Mike Reed | 7aad8cc | 2017-07-05 12:33:06 -0400 | [diff] [blame] | 143 | //static int gCounts[5] = { 0, 0, 0, 0, 0 }; |
| 144 | |
Mike Reed | 6b59bf4 | 2017-07-03 21:26:44 -0400 | [diff] [blame] | 145 | void SkRasterPipeline::append_matrix(SkArenaAlloc* alloc, const SkMatrix& matrix) { |
Mike Reed | 7aad8cc | 2017-07-05 12:33:06 -0400 | [diff] [blame] | 146 | SkMatrix::TypeMask mt = matrix.getType(); |
| 147 | #if 0 |
| 148 | if (mt > 4) mt = 4; |
| 149 | gCounts[mt] += 1; |
| 150 | SkDebugf("matrices: %d %d %d %d %d\n", |
| 151 | gCounts[0], gCounts[1], gCounts[2], gCounts[3], gCounts[4]); |
| 152 | #endif |
| 153 | |
| 154 | // Based on a histogram of skps, we determined the following special cases were common, more |
| 155 | // or fewer can be used if client behaviors change. |
| 156 | |
| 157 | if (mt == SkMatrix::kIdentity_Mask) { |
| 158 | return; |
| 159 | } |
| 160 | if (mt == SkMatrix::kTranslate_Mask) { |
| 161 | float* trans = alloc->makeArrayDefault<float>(2); |
| 162 | trans[0] = matrix.getTranslateX(); |
| 163 | trans[1] = matrix.getTranslateY(); |
| 164 | this->append(SkRasterPipeline::matrix_translate, trans); |
| 165 | } else if ((mt | (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask)) == |
| 166 | (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask)) { |
| 167 | float* scaleTrans = alloc->makeArrayDefault<float>(4); |
| 168 | scaleTrans[0] = matrix.getTranslateX(); |
| 169 | scaleTrans[1] = matrix.getTranslateY(); |
| 170 | scaleTrans[2] = matrix.getScaleX(); |
| 171 | scaleTrans[3] = matrix.getScaleY(); |
| 172 | this->append(SkRasterPipeline::matrix_scale_translate, scaleTrans); |
Mike Reed | 6b59bf4 | 2017-07-03 21:26:44 -0400 | [diff] [blame] | 173 | } else { |
Mike Reed | 7aad8cc | 2017-07-05 12:33:06 -0400 | [diff] [blame] | 174 | float* storage = alloc->makeArrayDefault<float>(9); |
| 175 | if (matrix.asAffine(storage)) { |
| 176 | // note: asAffine and the 2x3 stage really only need 6 entries |
| 177 | this->append(SkRasterPipeline::matrix_2x3, storage); |
| 178 | } else { |
| 179 | matrix.get9(storage); |
| 180 | this->append(SkRasterPipeline::matrix_perspective, storage); |
| 181 | } |
Mike Reed | 6b59bf4 | 2017-07-03 21:26:44 -0400 | [diff] [blame] | 182 | } |
| 183 | } |
Mike Klein | 159db0a | 2017-07-25 12:02:13 -0400 | [diff] [blame] | 184 | |
| 185 | void SkRasterPipeline::clamp_if_unclamped(SkAlphaType alphaType) { |
| 186 | if (!fClamped) { |
| 187 | this->append(SkRasterPipeline::clamp_0); |
| 188 | this->append(alphaType == kPremul_SkAlphaType ? SkRasterPipeline::clamp_a |
| 189 | : SkRasterPipeline::clamp_1); |
| 190 | fClamped = true; |
| 191 | } |
| 192 | } |