SkRasterPipeline in SkArenaAlloc
[platform/upstream/libSkiaSharp.git] / src / core / SkRasterPipeline.cpp
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"
9
10 SkRasterPipeline::SkRasterPipeline(SkArenaAlloc* alloc) : fAlloc(alloc) {
11     this->reset();
12 }
13 void SkRasterPipeline::reset() {
14     fStages      = nullptr;
15     fSlotsNeeded = 1;  // We always need one extra slot for just_return().
16 }
17
18 void SkRasterPipeline::append(StockStage stage, void* ctx) {
19     SkASSERT(stage != from_srgb);
20     this->unchecked_append(stage, ctx);
21 }
22 void SkRasterPipeline::unchecked_append(StockStage stage, void* ctx) {
23     fStages = fAlloc->make<StageList>( StageList{fStages, stage, ctx} );
24     fSlotsNeeded += ctx ? 2 : 1;
25 }
26
27 void SkRasterPipeline::extend(const SkRasterPipeline& src) {
28     this->extend(src.fStages);
29 }
30 void SkRasterPipeline::extend(const StageList* stages) {
31     if (!stages) {
32         return;
33     }
34     this->extend(stages->prev);
35     this->unchecked_append(stages->stage, stages->ctx);
36 }
37
38 void SkRasterPipeline::dump() const {
39     SkDebugf("SkRasterPipeline, (in reverse)\n");
40     for (auto st = fStages; st; st = st->prev) {
41         const char* name = "";
42         switch (st->stage) {
43         #define M(x) case x: name = #x; break;
44             SK_RASTER_PIPELINE_STAGES(M)
45         #undef M
46         }
47         SkDebugf("\t%s\n", name);
48     }
49     SkDebugf("\n");
50 }
51
52 // It's pretty easy to start with sound premultiplied linear floats, pack those
53 // to sRGB encoded bytes, then read them back to linear floats and find them not
54 // quite premultiplied, with a color channel just a smidge greater than the alpha
55 // channel.  This can happen basically any time we have different transfer
56 // functions for alpha and colors... sRGB being the only one we draw into.
57
58 // This is an annoying problem with no known good solution.  So apply the clamp hammer.
59
60 void SkRasterPipeline::append_from_srgb(SkAlphaType at) {
61     this->unchecked_append(from_srgb, nullptr);
62     if (at == kPremul_SkAlphaType) {
63         this->append(SkRasterPipeline::clamp_a);
64     }
65 }