Debian Codegraph @

src/core/SkDraw_atlas.cpp

raw
1 /*
2 * Copyright 2019 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/core/SkAlphaType.h"
9 #include "include/core/SkColor.h"
10 #include "include/core/SkMaskFilter.h"
11 #include "include/core/SkMatrix.h"
12 #include "include/core/SkPaint.h"
13 #include "include/core/SkPath.h"
14 #include "include/core/SkPixmap.h"
15 #include "include/core/SkPoint.h"
16 #include "include/core/SkRect.h"
17 #include "include/core/SkRefCnt.h"
18 #include "include/core/SkScalar.h"
19 #include "include/core/SkShader.h"
20 #include "include/core/SkSpan.h"
21 #include "include/core/SkSurfaceProps.h"
22 #include "include/private/base/SkAssert.h"
23 #include "src/base/SkArenaAlloc.h"
24 #include "src/core/SkBlendModePriv.h"
25 #include "src/core/SkBlenderBase.h"
26 #include "src/core/SkColorSpacePriv.h"
27 #include "src/core/SkColorSpaceXformSteps.h"
28 #include "src/core/SkCoreBlitters.h"
29 #include "src/core/SkDraw.h"
30 #include "src/core/SkEffectPriv.h"
31 #include "src/core/SkPathRawShapes.h"
32 #include "src/core/SkRasterClip.h"
33 #include "src/core/SkRasterPipeline.h"
34 #include "src/core/SkRasterPipelineOpContexts.h"
35 #include "src/core/SkRasterPipelineOpList.h"
36 #include "src/core/SkScan.h"
37 #include "src/core/SkSurfacePriv.h"
38 #include "src/shaders/SkShaderBase.h"
39 #include "src/shaders/SkTransformShader.h"
40
41 #include <cstddef>
42 #include <cstdint>
43 #include <optional>
44
45 class SkBlender;
46 class SkBlitter;
47 enum class SkBlendMode;
48 struct SkRSXform;
49
50 static void fill_rect(const SkMatrix& ctm, const SkRasterClip& rc,
51 const SkRect& r, SkBlitter* blitter) {
52 if (ctm.rectStaysRect()) {
53 SkScan::FillRect(ctm.mapRect(r), rc, blitter);
54 } else {
55 SkPathRawShapes::Rect raw(r);
56 ctm.mapPoints(raw.fStorage);
57 if (auto bounds = SkRect::Bounds(raw.fStorage)) {
58 raw.fBounds = *bounds;
59 SkScan::FillPath(raw, rc, blitter);
60 }
61 }
62 }
63
64 static void load_color(SkRasterPipelineContexts::UniformColorCtx* ctx, const float rgba[]) {
65 // only need one of these. can I query the pipeline to know if its lowp or highp?
66 ctx->rgba[0] = SkScalarRoundToInt(rgba[0]*255); ctx->r = rgba[0];
67 ctx->rgba[1] = SkScalarRoundToInt(rgba[1]*255); ctx->g = rgba[1];
68 ctx->rgba[2] = SkScalarRoundToInt(rgba[2]*255); ctx->b = rgba[2];
69 ctx->rgba[3] = SkScalarRoundToInt(rgba[3]*255); ctx->a = rgba[3];
70 }
71
72 namespace skcpu {
73 void Draw::drawAtlas(SkSpan<const SkRSXform> xform,
74 SkSpan<const SkRect> textures,
75 SkSpan<const SkColor> colors,
76 sk_sp<SkBlender> blender,
77 const SkPaint& paint) {
78 SkASSERT(xform.size() == textures.size());
79 SkASSERT(colors.empty() || (xform.size() == colors.size()));
80
81 sk_sp<SkShader> atlasShader = paint.refShader();
82 if (!atlasShader || xform.empty()) {
83 return;
84 }
85
86 SkSTArenaAlloc<256> alloc;
87
88 SkPaint p(paint);
89 p.setAntiAlias(false); // we never respect this for drawAtlas(or drawVertices)
90 p.setStyle(SkPaint::kFill_Style);
91 p.setShader(nullptr);
92 p.setMaskFilter(nullptr);
93
94 // The RSXForms can't contain perspective - only the CTM can.
95 const bool perspective = fCTM->hasPerspective();
96
97 auto transformShader = alloc.make<SkTransformShader>(*as_SB(atlasShader), perspective);
98
99 SkRasterPipeline pipeline(&alloc);
100 SkSurfaceProps props = SkSurfacePropsCopyOrDefault(fProps);
101 SkStageRec rec = {&pipeline,
102 &alloc,
103 fDst.colorType(),
104 fDst.colorSpace(),
105 p.getColor4f(),
106 props,
107 SkRect::MakeEmpty()};
108 // We pass an identity matrix here rather than the CTM. The CTM gets folded into the
109 // per-triangle matrix.
110 if (!as_SB(transformShader)->appendRootStages(rec, SkMatrix::I())) {
111 return;
112 }
113
114 SkRasterPipelineContexts::UniformColorCtx* uniformCtx = nullptr;
115 SkColorSpaceXformSteps steps(sk_srgb_singleton(), kUnpremul_SkAlphaType,
116 rec.fDstCS, kUnpremul_SkAlphaType);
117 if (!colors.empty()) {
118 // we will late-bind the values in ctx, once for each color in the loop
119 uniformCtx = alloc.make<SkRasterPipelineContexts::UniformColorCtx>();
120 rec.fPipeline->append(SkRasterPipelineOp::uniform_color_dst, uniformCtx);
121 std::optional<SkBlendMode> bm = as_BB(blender)->asBlendMode();
122 if (!bm.has_value()) {
123 return;
124 }
125 SkBlendMode_AppendStages(*bm, rec.fPipeline);
126 }
127
128 bool isOpaque = colors.empty() && transformShader->isOpaque();
129 if (p.getAlphaf() != 1) {
130 rec.fPipeline->append(SkRasterPipelineOp::scale_1_float, alloc.make<float>(p.getAlphaf()));
131 isOpaque = false;
132 }
133
134 auto blitter = SkCreateRasterPipelineBlitter(fDst, p, pipeline, isOpaque, &alloc,
135 fRC->clipShader());
136 if (!blitter) {
137 return;
138 }
139
140 for (size_t i = 0; i < xform.size(); ++i) {
141 if (!colors.empty()) {
142 SkColor4f c4 = SkColor4f::FromColor(colors[i]);
143 steps.apply(c4.vec());
144 load_color(uniformCtx, c4.premul().vec());
145 }
146
147 SkMatrix mx;
148 mx.setRSXform(xform[i]);
149 mx.preTranslate(-textures[i].fLeft, -textures[i].fTop);
150 mx.postConcat(*fCTM);
151 if (auto inv = mx.invert()) {
152 if (transformShader->update(*inv)) {
153 fill_rect(mx, *fRC, textures[i], blitter);
154 }
155 } else {
156 return; // non-invertible
157 }
158 }
159 }
160 } // namespace skcpu