Coverage Report

Created: 2021-08-22 09:07

/src/skia/src/core/SkRasterPipeline.cpp
Line
Count
Source (jump to first uncovered line)
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 "include/private/SkImageInfoPriv.h"
9
#include "include/private/SkNx.h"
10
#include "include/private/SkTemplates.h"
11
#include "src/core/SkColorSpacePriv.h"
12
#include "src/core/SkOpts.h"
13
#include "src/core/SkRasterPipeline.h"
14
#include <algorithm>
15
16
bool gForceHighPrecisionRasterPipeline;
17
18
1.97M
SkRasterPipeline::SkRasterPipeline(SkArenaAlloc* alloc) : fAlloc(alloc) {
19
1.97M
    this->reset();
20
1.97M
}
21
2.09M
void SkRasterPipeline::reset() {
22
2.09M
    fStages      = nullptr;
23
2.09M
    fNumStages   = 0;
24
2.09M
    fSlotsNeeded = 1;  // We always need one extra slot for just_return().
25
2.09M
}
26
27
7.23M
void SkRasterPipeline::append(StockStage stage, void* ctx) {
28
7.23M
    SkASSERT(stage !=           uniform_color);  // Please use append_constant_color().
29
7.23M
    SkASSERT(stage != unbounded_uniform_color);  // Please use append_constant_color().
30
7.23M
    SkASSERT(stage !=                 set_rgb);  // Please use append_set_rgb().
31
7.23M
    SkASSERT(stage !=       unbounded_set_rgb);  // Please use append_set_rgb().
32
7.23M
    SkASSERT(stage !=             clamp_gamut);  // Please use append_gamut_clamp_if_normalized().
33
7.23M
    SkASSERT(stage !=              parametric);  // Please use append_transfer_function().
34
7.23M
    SkASSERT(stage !=                  gamma_);  // Please use append_transfer_function().
35
7.23M
    SkASSERT(stage !=                   PQish);  // Please use append_transfer_function().
36
7.23M
    SkASSERT(stage !=                  HLGish);  // Please use append_transfer_function().
37
7.23M
    SkASSERT(stage !=               HLGinvish);  // Please use append_transfer_function().
38
7.23M
    this->unchecked_append(stage, ctx);
39
7.23M
}
40
7.83M
void SkRasterPipeline::unchecked_append(StockStage stage, void* ctx) {
41
7.83M
    fStages = fAlloc->make<StageList>( StageList{fStages, stage, ctx} );
42
7.83M
    fNumStages   += 1;
43
4.31M
    fSlotsNeeded += ctx ? 2 : 1;
44
7.83M
}
45
0
void SkRasterPipeline::append(StockStage stage, uintptr_t ctx) {
46
0
    void* ptrCtx;
47
0
    memcpy(&ptrCtx, &ctx, sizeof(ctx));
48
0
    this->append(stage, ptrCtx);
49
0
}
50
51
725k
void SkRasterPipeline::extend(const SkRasterPipeline& src) {
52
725k
    if (src.empty()) {
53
22.9k
        return;
54
22.9k
    }
55
702k
    auto stages = fAlloc->makeArrayDefault<StageList>(src.fNumStages);
56
57
702k
    int n = src.fNumStages;
58
702k
    const StageList* st = src.fStages;
59
3.53M
    while (n --> 1) {
60
2.83M
        stages[n]      = *st;
61
2.83M
        stages[n].prev = &stages[n-1];
62
2.83M
        st = st->prev;
63
2.83M
    }
64
702k
    stages[0]      = *st;
65
702k
    stages[0].prev = fStages;
66
67
702k
    fStages = &stages[src.fNumStages - 1];
68
702k
    fNumStages   += src.fNumStages;
69
702k
    fSlotsNeeded += src.fSlotsNeeded - 1;  // Don't double count just_returns().
70
702k
}
71
72
0
void SkRasterPipeline::dump() const {
73
0
    SkDebugf("SkRasterPipeline, %d stages\n", fNumStages);
74
0
    std::vector<const char*> stages;
75
0
    for (auto st = fStages; st; st = st->prev) {
76
0
        const char* name = "";
77
0
        switch (st->stage) {
78
0
        #define M(x) case x: name = #x; break;
79
0
            SK_RASTER_PIPELINE_STAGES(M)
80
0
        #undef M
81
0
        }
82
0
        stages.push_back(name);
83
0
    }
84
0
    std::reverse(stages.begin(), stages.end());
85
0
    for (const char* name : stages) {
86
0
        SkDebugf("\t%s\n", name);
87
0
    }
88
0
    SkDebugf("\n");
89
0
}
90
91
83
void SkRasterPipeline::append_set_rgb(SkArenaAlloc* alloc, const float rgb[3]) {
92
83
    auto arg = alloc->makeArrayDefault<float>(3);
93
83
    arg[0] = rgb[0];
94
83
    arg[1] = rgb[1];
95
83
    arg[2] = rgb[2];
96
97
83
    auto stage = unbounded_set_rgb;
98
83
    if (0 <= rgb[0] && rgb[0] <= 1 &&
99
83
        0 <= rgb[1] && rgb[1] <= 1 &&
100
83
        0 <= rgb[2] && rgb[2] <= 1)
101
83
    {
102
83
        stage = set_rgb;
103
83
    }
104
105
83
    this->unchecked_append(stage, arg);
106
83
}
107
108
329k
void SkRasterPipeline::append_constant_color(SkArenaAlloc* alloc, const float rgba[4]) {
109
    // r,g,b might be outside [0,1], but alpha should probably always be in [0,1].
110
329k
    SkASSERT(0 <= rgba[3] && rgba[3] <= 1);
111
112
329k
    if (rgba[0] == 0 && rgba[1] == 0 && rgba[2] == 0 && rgba[3] == 1) {
113
62.8k
        this->append(black_color);
114
266k
    } else if (rgba[0] == 1 && rgba[1] == 1 && rgba[2] == 1 && rgba[3] == 1) {
115
7.67k
        this->append(white_color);
116
259k
    } else {
117
259k
        auto ctx = alloc->make<SkRasterPipeline_UniformColorCtx>();
118
259k
        Sk4f color = Sk4f::Load(rgba);
119
259k
        color.store(&ctx->r);
120
121
        // uniform_color requires colors in range and can go lowp,
122
        // while unbounded_uniform_color supports out-of-range colors too but not lowp.
123
259k
        if (0 <= rgba[0] && rgba[0] <= rgba[3] &&
124
259k
            0 <= rgba[1] && rgba[1] <= rgba[3] &&
125
259k
            0 <= rgba[2] && rgba[2] <= rgba[3]) {
126
            // To make loads more direct, we store 8-bit values in 16-bit slots.
127
259k
            color = color * 255.0f + 0.5f;
128
259k
            ctx->rgba[0] = (uint16_t)color[0];
129
259k
            ctx->rgba[1] = (uint16_t)color[1];
130
259k
            ctx->rgba[2] = (uint16_t)color[2];
131
259k
            ctx->rgba[3] = (uint16_t)color[3];
132
259k
            this->unchecked_append(uniform_color, ctx);
133
5
        } else {
134
5
            this->unchecked_append(unbounded_uniform_color, ctx);
135
5
        }
136
259k
    }
137
329k
}
138
139
49.5k
void SkRasterPipeline::append_matrix(SkArenaAlloc* alloc, const SkMatrix& matrix) {
140
49.5k
    SkMatrix::TypeMask mt = matrix.getType();
141
142
49.5k
    if (mt == SkMatrix::kIdentity_Mask) {
143
21.9k
        return;
144
21.9k
    }
145
27.6k
    if (mt == SkMatrix::kTranslate_Mask) {
146
22.1k
        float* trans = alloc->makeArrayDefault<float>(2);
147
22.1k
        trans[0] = matrix.getTranslateX();
148
22.1k
        trans[1] = matrix.getTranslateY();
149
22.1k
        this->append(SkRasterPipeline::matrix_translate, trans);
150
5.52k
    } else if ((mt | (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask)) ==
151
3.03k
                     (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask)) {
152
3.03k
        float* scaleTrans = alloc->makeArrayDefault<float>(4);
153
3.03k
        scaleTrans[0] = matrix.getScaleX();
154
3.03k
        scaleTrans[1] = matrix.getScaleY();
155
3.03k
        scaleTrans[2] = matrix.getTranslateX();
156
3.03k
        scaleTrans[3] = matrix.getTranslateY();
157
3.03k
        this->append(SkRasterPipeline::matrix_scale_translate, scaleTrans);
158
2.48k
    } else {
159
2.48k
        float* storage = alloc->makeArrayDefault<float>(9);
160
2.48k
        if (matrix.asAffine(storage)) {
161
            // note: asAffine and the 2x3 stage really only need 6 entries
162
1.86k
            this->append(SkRasterPipeline::matrix_2x3, storage);
163
624
        } else {
164
624
            matrix.get9(storage);
165
624
            this->append(SkRasterPipeline::matrix_perspective, storage);
166
624
        }
167
2.48k
    }
168
27.6k
}
169
170
496k
void SkRasterPipeline::append_load(SkColorType ct, const SkRasterPipeline_MemoryCtx* ctx) {
171
496k
    switch (ct) {
172
0
        case kUnknown_SkColorType: SkASSERT(false); break;
173
174
0
        case kAlpha_8_SkColorType:           this->append(load_a8,      ctx); break;
175
24
        case kA16_unorm_SkColorType:         this->append(load_a16,     ctx); break;
176
29
        case kA16_float_SkColorType:         this->append(load_af16,    ctx); break;
177
68
        case kRGB_565_SkColorType:           this->append(load_565,     ctx); break;
178
75
        case kARGB_4444_SkColorType:         this->append(load_4444,    ctx); break;
179
63
        case kR8G8_unorm_SkColorType:        this->append(load_rg88,    ctx); break;
180
27
        case kR16G16_unorm_SkColorType:      this->append(load_rg1616,  ctx); break;
181
24
        case kR16G16_float_SkColorType:      this->append(load_rgf16,   ctx); break;
182
0
        case kRGBA_8888_SkColorType:         this->append(load_8888,    ctx); break;
183
10
        case kRGBA_1010102_SkColorType:      this->append(load_1010102, ctx); break;
184
57
        case kR16G16B16A16_unorm_SkColorType:this->append(load_16161616,ctx); break;
185
9
        case kRGBA_F16Norm_SkColorType:
186
22
        case kRGBA_F16_SkColorType:          this->append(load_f16,     ctx); break;
187
133k
        case kRGBA_F32_SkColorType:          this->append(load_f32,     ctx); break;
188
189
592
        case kGray_8_SkColorType:            this->append(load_a8, ctx);
190
592
                                             this->append(alpha_to_gray);
191
592
                                             break;
192
193
12
        case kRGB_888x_SkColorType:          this->append(load_8888, ctx);
194
12
                                             this->append(force_opaque);
195
12
                                             break;
196
197
6
        case kBGRA_1010102_SkColorType:      this->append(load_1010102, ctx);
198
6
                                             this->append(swap_rb);
199
6
                                             break;
200
201
8
        case kRGB_101010x_SkColorType:       this->append(load_1010102, ctx);
202
8
                                             this->append(force_opaque);
203
8
                                             break;
204
205
10
        case kBGR_101010x_SkColorType:       this->append(load_1010102, ctx);
206
10
                                             this->append(force_opaque);
207
10
                                             this->append(swap_rb);
208
10
                                             break;
209
210
362k
        case kBGRA_8888_SkColorType:         this->append(load_8888, ctx);
211
362k
                                             this->append(swap_rb);
212
362k
                                             break;
213
496k
    }
214
496k
}
215
216
279k
void SkRasterPipeline::append_load_dst(SkColorType ct, const SkRasterPipeline_MemoryCtx* ctx) {
217
279k
    switch (ct) {
218
0
        case kUnknown_SkColorType: SkASSERT(false); break;
219
220
32.0k
        case kAlpha_8_SkColorType:            this->append(load_a8_dst,      ctx); break;
221
0
        case kA16_unorm_SkColorType:          this->append(load_a16_dst,     ctx); break;
222
0
        case kA16_float_SkColorType:          this->append(load_af16_dst,    ctx); break;
223
0
        case kRGB_565_SkColorType:            this->append(load_565_dst,     ctx); break;
224
0
        case kARGB_4444_SkColorType:          this->append(load_4444_dst,    ctx); break;
225
0
        case kR8G8_unorm_SkColorType:         this->append(load_rg88_dst,    ctx); break;
226
0
        case kR16G16_unorm_SkColorType:       this->append(load_rg1616_dst,  ctx); break;
227
0
        case kR16G16_float_SkColorType:       this->append(load_rgf16_dst,   ctx); break;
228
261
        case kRGBA_8888_SkColorType:          this->append(load_8888_dst,    ctx); break;
229
0
        case kRGBA_1010102_SkColorType:       this->append(load_1010102_dst, ctx); break;
230
0
        case kR16G16B16A16_unorm_SkColorType: this->append(load_16161616_dst,ctx); break;
231
0
        case kRGBA_F16Norm_SkColorType:
232
0
        case kRGBA_F16_SkColorType:           this->append(load_f16_dst,     ctx); break;
233
0
        case kRGBA_F32_SkColorType:           this->append(load_f32_dst,     ctx); break;
234
235
0
        case kGray_8_SkColorType:             this->append(load_a8_dst, ctx);
236
0
                                              this->append(alpha_to_gray_dst);
237
0
                                              break;
238
239
0
        case kRGB_888x_SkColorType:           this->append(load_8888_dst, ctx);
240
0
                                              this->append(force_opaque_dst);
241
0
                                              break;
242
243
0
        case kBGRA_1010102_SkColorType:       this->append(load_1010102_dst, ctx);
244
0
                                              this->append(swap_rb_dst);
245
0
                                              break;
246
247
0
        case kRGB_101010x_SkColorType:        this->append(load_1010102_dst, ctx);
248
0
                                              this->append(force_opaque_dst);
249
0
                                              break;
250
251
0
        case kBGR_101010x_SkColorType:        this->append(load_1010102_dst, ctx);
252
0
                                              this->append(force_opaque_dst);
253
0
                                              this->append(swap_rb_dst);
254
0
                                              break;
255
256
246k
        case kBGRA_8888_SkColorType:          this->append(load_8888_dst, ctx);
257
246k
                                              this->append(swap_rb_dst);
258
246k
                                              break;
259
279k
    }
260
279k
}
261
262
662k
void SkRasterPipeline::append_store(SkColorType ct, const SkRasterPipeline_MemoryCtx* ctx) {
263
662k
    switch (ct) {
264
0
        case kUnknown_SkColorType: SkASSERT(false); break;
265
266
79.2k
        case kAlpha_8_SkColorType:            this->append(store_a8,      ctx); break;
267
38
        case kA16_unorm_SkColorType:          this->append(store_a16,     ctx); break;
268
44
        case kA16_float_SkColorType:          this->append(store_af16,    ctx); break;
269
96
        case kRGB_565_SkColorType:            this->append(store_565,     ctx); break;
270
104
        case kARGB_4444_SkColorType:          this->append(store_4444,    ctx); break;
271
85
        case kR8G8_unorm_SkColorType:         this->append(store_rg88,    ctx); break;
272
41
        case kR16G16_unorm_SkColorType:       this->append(store_rg1616,  ctx); break;
273
35
        case kR16G16_float_SkColorType:       this->append(store_rgf16,   ctx); break;
274
4.68k
        case kRGBA_8888_SkColorType:          this->append(store_8888,    ctx); break;
275
21
        case kRGBA_1010102_SkColorType:       this->append(store_1010102, ctx); break;
276
141
        case kR16G16B16A16_unorm_SkColorType: this->append(store_16161616,ctx); break;
277
18
        case kRGBA_F16Norm_SkColorType:
278
38
        case kRGBA_F16_SkColorType:           this->append(store_f16,     ctx); break;
279
135k
        case kRGBA_F32_SkColorType:           this->append(store_f32,     ctx); break;
280
281
25
        case kRGB_888x_SkColorType:           this->append(force_opaque);
282
25
                                              this->append(store_8888, ctx);
283
25
                                              break;
284
285
14
        case kBGRA_1010102_SkColorType:       this->append(swap_rb);
286
14
                                              this->append(store_1010102, ctx);
287
14
                                              break;
288
289
12
        case kRGB_101010x_SkColorType:        this->append(force_opaque);
290
12
                                              this->append(store_1010102, ctx);
291
12
                                              break;
292
293
16
        case kBGR_101010x_SkColorType:        this->append(force_opaque);
294
16
                                              this->append(swap_rb);
295
16
                                              this->append(store_1010102, ctx);
296
16
                                              break;
297
298
126
        case kGray_8_SkColorType:             this->append(bt709_luminance_or_luma_to_alpha);
299
126
                                              this->append(store_a8, ctx);
300
126
                                              break;
301
302
442k
        case kBGRA_8888_SkColorType:          this->append(swap_rb);
303
442k
                                              this->append(store_8888, ctx);
304
442k
                                              break;
305
662k
    }
306
662k
}
307
308
23.4k
void SkRasterPipeline::append_transfer_function(const skcms_TransferFunction& tf) {
309
23.4k
    void* ctx = const_cast<void*>(static_cast<const void*>(&tf));
310
23.4k
    switch (classify_transfer_fn(tf)) {
311
0
        case Bad_TF: SkASSERT(false); break;
312
313
23.4k
        case TFKind::sRGBish_TF:
314
23.4k
            if (tf.a == 1 && tf.b == 0 && tf.c == 0 && tf.d == 0 && tf.e == 0 && tf.f == 0) {
315
0
                this->unchecked_append(gamma_, ctx);
316
23.4k
            } else {
317
23.4k
                this->unchecked_append(parametric, ctx);
318
23.4k
            }
319
23.4k
            break;
320
0
        case PQish_TF:     this->unchecked_append(PQish,     ctx); break;
321
0
        case HLGish_TF:    this->unchecked_append(HLGish,    ctx); break;
322
0
        case HLGinvish_TF: this->unchecked_append(HLGinvish, ctx); break;
323
23.4k
    }
324
23.4k
}
325
326
// Clamp premul values to [0,alpha] (logical [0,1]) to avoid the confusing
327
// scenario of being able to store a logical color channel > 1.0 when alpha < 1.0.
328
// Most software that works with normalized premul values expect r,g,b channels all <= a.
329
//
330
// In addition, GL clamps all its color channels to limits of the format just
331
// before the blend step (~here).  To match that auto-clamp, we clamp alpha to
332
// [0,1] too, just in case someone gave us a crazy alpha.
333
589k
void SkRasterPipeline::append_gamut_clamp_if_normalized(const SkImageInfo& info) {
334
589k
    if (info.alphaType() == kPremul_SkAlphaType && SkColorTypeIsNormalized(info.colorType())) {
335
313k
        this->unchecked_append(SkRasterPipeline::clamp_gamut, nullptr);
336
313k
    }
337
589k
}
338
339
842k
SkRasterPipeline::StartPipelineFn SkRasterPipeline::build_pipeline(void** ip) const {
340
842k
    if (!gForceHighPrecisionRasterPipeline) {
341
        // We'll try to build a lowp pipeline, but if that fails fallback to a highp float pipeline.
342
842k
        void** reset_point = ip;
343
344
        // Stages are stored backwards in fStages, so we reverse here, back to front.
345
842k
        *--ip = (void*)SkOpts::just_return_lowp;
346
2.50M
        for (const StageList* st = fStages; st; st = st->prev) {
347
2.32M
            if (auto fn = SkOpts::stages_lowp[st->stage]) {
348
1.65M
                if (st->ctx) {
349
797k
                    *--ip = st->ctx;
350
797k
                }
351
1.65M
                *--ip = (void*)fn;
352
668k
            } else {
353
668k
                ip = reset_point;
354
668k
                break;
355
668k
            }
356
2.32M
        }
357
842k
        if (ip != reset_point) {
358
174k
            return SkOpts::start_pipeline_lowp;
359
174k
        }
360
668k
    }
361
362
668k
    *--ip = (void*)SkOpts::just_return_highp;
363
4.22M
    for (const StageList* st = fStages; st; st = st->prev) {
364
3.55M
        if (st->ctx) {
365
1.70M
            *--ip = st->ctx;
366
1.70M
        }
367
3.55M
        *--ip = (void*)SkOpts::stages_highp[st->stage];
368
3.55M
    }
369
668k
    return SkOpts::start_pipeline_highp;
370
668k
}
371
372
747k
void SkRasterPipeline::run(size_t x, size_t y, size_t w, size_t h) const {
373
747k
    if (this->empty()) {
374
0
        return;
375
0
    }
376
377
    // Best to not use fAlloc here... we can't bound how often run() will be called.
378
747k
    SkAutoSTMalloc<64, void*> program(fSlotsNeeded);
379
380
747k
    auto start_pipeline = this->build_pipeline(program.get() + fSlotsNeeded);
381
747k
    start_pipeline(x,y,x+w,y+h, program.get());
382
747k
}
383
384
94.9k
std::function<void(size_t, size_t, size_t, size_t)> SkRasterPipeline::compile() const {
385
94.9k
    if (this->empty()) {
386
0
        return [](size_t, size_t, size_t, size_t) {};
Unexecuted instantiation: SkRasterPipeline.cpp:SkRasterPipeline::compile() const::$_0::operator()(unsigned long, unsigned long, unsigned long, unsigned long) const
Unexecuted instantiation: SkRasterPipeline.cpp:SkRasterPipeline::compile() const::$_15::operator()(unsigned long, unsigned long, unsigned long, unsigned long) const
387
0
    }
388
389
94.9k
    void** program = fAlloc->makeArray<void*>(fSlotsNeeded);
390
391
94.9k
    auto start_pipeline = this->build_pipeline(program + fSlotsNeeded);
392
11.0M
    return [=](size_t x, size_t y, size_t w, size_t h) {
393
11.0M
        start_pipeline(x,y,x+w,y+h, program);
394
11.0M
    };
SkRasterPipeline.cpp:SkRasterPipeline::compile() const::$_1::operator()(unsigned long, unsigned long, unsigned long, unsigned long) const
Line
Count
Source
392
11.0M
    return [=](size_t x, size_t y, size_t w, size_t h) {
393
11.0M
        start_pipeline(x,y,x+w,y+h, program);
394
11.0M
    };
Unexecuted instantiation: SkRasterPipeline.cpp:SkRasterPipeline::compile() const::$_16::operator()(unsigned long, unsigned long, unsigned long, unsigned long) const
395
94.9k
}