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/SkColorFilter.h" |
9 | #include "include/core/SkRSXform.h" |
10 | #include "src/core/SkBlendModePriv.h" |
11 | #include "src/core/SkColorSpacePriv.h" |
12 | #include "src/core/SkColorSpaceXformSteps.h" |
13 | #include "src/core/SkCoreBlitters.h" |
14 | #include "src/core/SkDraw.h" |
15 | #include "src/core/SkMatrixProvider.h" |
16 | #include "src/core/SkRasterClip.h" |
17 | #include "src/core/SkRasterPipeline.h" |
18 | #include "src/core/SkScan.h" |
19 | #include "src/shaders/SkShaderBase.h" |
20 | |
21 | #include "include/core/SkMatrix.h" |
22 | #include "src/core/SkScan.h" |
23 | |
24 | static void fill_rect(const SkMatrix& ctm, const SkRasterClip& rc, |
25 | const SkRect& r, SkBlitter* blitter) { |
26 | if (ctm.rectStaysRect()) { |
27 | SkRect dr; |
28 | ctm.mapRect(&dr, r); |
29 | SkScan::FillRect(dr, rc, blitter); |
30 | } else { |
31 | SkPoint pts[4]; |
32 | r.toQuad(pts); |
33 | ctm.mapPoints(pts, pts, 4); |
34 | |
35 | SkRect bounds; |
36 | bounds.setBounds(pts, 4); |
37 | |
38 | SkScan::FillPath(SkPathView_quad(pts, bounds), rc, blitter); |
39 | } |
40 | } |
41 | |
42 | static void load_color(SkRasterPipeline_UniformColorCtx* ctx, const float rgba[]) { |
43 | // only need one of these. can I query the pipeline to know if its lowp or highp? |
44 | ctx->rgba[0] = SkScalarRoundToInt(rgba[0]*255); ctx->r = rgba[0]; |
45 | ctx->rgba[1] = SkScalarRoundToInt(rgba[1]*255); ctx->g = rgba[1]; |
46 | ctx->rgba[2] = SkScalarRoundToInt(rgba[2]*255); ctx->b = rgba[2]; |
47 | ctx->rgba[3] = SkScalarRoundToInt(rgba[3]*255); ctx->a = rgba[3]; |
48 | } |
49 | |
50 | void SkDraw::drawAtlas(const SkImage* atlas, const SkRSXform xform[], const SkRect textures[], |
51 | const SkColor colors[], int count, SkBlendMode bmode, const SkPaint& paint) { |
52 | sk_sp<SkShader> atlasShader = atlas->makeShader(); |
53 | if (!atlasShader) { |
54 | return; |
55 | } |
56 | |
57 | SkPaint p(paint); |
58 | p.setAntiAlias(false); // we never respect this for drawAtlas(or drawVertices) |
59 | p.setStyle(SkPaint::kFill_Style); |
60 | p.setShader(nullptr); |
61 | p.setMaskFilter(nullptr); |
62 | |
63 | SkSTArenaAlloc<256> alloc; |
64 | SkRasterPipeline pipeline(&alloc); |
65 | SkStageRec rec = { |
66 | &pipeline, &alloc, fDst.colorType(), fDst.colorSpace(), p, nullptr, *fMatrixProvider |
67 | }; |
68 | |
69 | SkStageUpdater* updator = as_SB(atlasShader.get())->appendUpdatableStages(rec); |
70 | if (!updator) { |
71 | SkDraw draw(*this); |
72 | |
73 | p.setShader(atlasShader); |
74 | for (int i = 0; i < count; ++i) { |
75 | if (colors) { |
76 | p.setShader(SkShaders::Blend(bmode, SkShaders::Color(colors[i]), atlasShader)); |
77 | } |
78 | SkMatrix mx; |
79 | mx.setRSXform(xform[i]); |
80 | mx.preTranslate(-textures[i].fLeft, -textures[i].fTop); |
81 | SkPreConcatMatrixProvider matrixProvider(*fMatrixProvider, mx); |
82 | draw.fMatrixProvider = &matrixProvider; |
83 | draw.drawRect(textures[i], p); |
84 | } |
85 | return; |
86 | } |
87 | |
88 | SkRasterPipeline_UniformColorCtx* uniformCtx = nullptr; |
89 | SkColorSpaceXformSteps steps(sk_srgb_singleton(), kUnpremul_SkAlphaType, |
90 | rec.fDstCS, kUnpremul_SkAlphaType); |
91 | |
92 | if (colors) { |
93 | // we will late-bind the values in ctx, once for each color in the loop |
94 | uniformCtx = alloc.make<SkRasterPipeline_UniformColorCtx>(); |
95 | rec.fPipeline->append(SkRasterPipeline::uniform_color_dst, uniformCtx); |
96 | SkBlendMode_AppendStages(bmode, rec.fPipeline); |
97 | } |
98 | |
99 | bool isOpaque = !colors && atlasShader->isOpaque(); |
100 | if (p.getAlphaf() != 1) { |
101 | rec.fPipeline->append(SkRasterPipeline::scale_1_float, alloc.make<float>(p.getAlphaf())); |
102 | isOpaque = false; |
103 | } |
104 | |
105 | if (auto blitter = SkCreateRasterPipelineBlitter(fDst, p, pipeline, isOpaque, &alloc, |
106 | fRC->clipShader())) { |
107 | for (int i = 0; i < count; ++i) { |
108 | if (colors) { |
109 | SkColor4f c4 = SkColor4f::FromColor(colors[i]); |
110 | steps.apply(c4.vec()); |
111 | load_color(uniformCtx, c4.premul().vec()); |
112 | } |
113 | |
114 | SkMatrix mx; |
115 | mx.setRSXform(xform[i]); |
116 | mx.preTranslate(-textures[i].fLeft, -textures[i].fTop); |
117 | mx.postConcat(fMatrixProvider->localToDevice()); |
118 | |
119 | if (updator->update(mx, nullptr)) { |
120 | fill_rect(mx, *fRC, textures[i], blitter); |
121 | } |
122 | } |
123 | } |
124 | } |
125 | |