Mike Reed | 560e917 | 2019-08-05 13:04:18 -0400 | [diff] [blame] | 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" |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 10 | #include "src/core/SkBlendModePriv.h" |
| 11 | #include "src/core/SkColorSpacePriv.h" |
| 12 | #include "src/core/SkColorSpaceXformSteps.h" |
| 13 | #include "src/core/SkCoreBlitters.h" |
Mike Reed | 560e917 | 2019-08-05 13:04:18 -0400 | [diff] [blame] | 14 | #include "src/core/SkDraw.h" |
Brian Osman | 9aaec36 | 2020-05-08 14:54:37 -0400 | [diff] [blame] | 15 | #include "src/core/SkMatrixProvider.h" |
Mike Reed | 121c2af | 2020-03-10 14:02:56 -0400 | [diff] [blame] | 16 | #include "src/core/SkRasterClip.h" |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 17 | #include "src/core/SkRasterPipeline.h" |
Mike Reed | 560e917 | 2019-08-05 13:04:18 -0400 | [diff] [blame] | 18 | #include "src/core/SkScan.h" |
| 19 | #include "src/shaders/SkShaderBase.h" |
| 20 | |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 21 | #include "include/core/SkMatrix.h" |
| 22 | #include "src/core/SkScan.h" |
| 23 | |
| 24 | static void fill_rect(const SkMatrix& ctm, const SkRasterClip& rc, |
Mike Reed | 375721d | 2020-08-07 16:00:06 -0400 | [diff] [blame] | 25 | const SkRect& r, SkBlitter* blitter) { |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 26 | if (ctm.rectStaysRect()) { |
| 27 | SkRect dr; |
| 28 | ctm.mapRect(&dr, r); |
| 29 | SkScan::FillRect(dr, rc, blitter); |
| 30 | } else { |
Mike Reed | f6cb6ed | 2019-09-06 15:17:14 -0400 | [diff] [blame] | 31 | SkPoint pts[4]; |
| 32 | r.toQuad(pts); |
| 33 | ctm.mapPoints(pts, pts, 4); |
| 34 | |
Mike Reed | 375721d | 2020-08-07 16:00:06 -0400 | [diff] [blame] | 35 | SkRect bounds; |
| 36 | bounds.setBounds(pts, 4); |
| 37 | |
| 38 | SkScan::FillPath(SkPathView_quad(pts, bounds), rc, blitter); |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 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 | |
Mike Reed | 560e917 | 2019-08-05 13:04:18 -0400 | [diff] [blame] | 50 | void SkDraw::drawAtlas(const SkImage* atlas, const SkRSXform xform[], const SkRect textures[], |
| 51 | const SkColor colors[], int count, SkBlendMode bmode, const SkPaint& paint) { |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 52 | sk_sp<SkShader> atlasShader = atlas->makeShader(); |
| 53 | if (!atlasShader) { |
| 54 | return; |
| 55 | } |
Mike Reed | 560e917 | 2019-08-05 13:04:18 -0400 | [diff] [blame] | 56 | |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 57 | SkPaint p(paint); |
Mike Reed | 560e917 | 2019-08-05 13:04:18 -0400 | [diff] [blame] | 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 | |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 63 | SkSTArenaAlloc<256> alloc; |
| 64 | SkRasterPipeline pipeline(&alloc); |
| 65 | SkStageRec rec = { |
Brian Osman | 9aaec36 | 2020-05-08 14:54:37 -0400 | [diff] [blame] | 66 | &pipeline, &alloc, fDst.colorType(), fDst.colorSpace(), p, nullptr, *fMatrixProvider |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 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); |
Brian Osman | 9aaec36 | 2020-05-08 14:54:37 -0400 | [diff] [blame] | 81 | SkPreConcatMatrixProvider matrixProvider(*fMatrixProvider, mx); |
| 82 | draw.fMatrixProvider = &matrixProvider; |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 83 | draw.drawRect(textures[i], p); |
| 84 | } |
Mike Reed | 4197400 | 2019-08-07 11:00:19 -0400 | [diff] [blame] | 85 | return; |
Mike Reed | 560e917 | 2019-08-05 13:04:18 -0400 | [diff] [blame] | 86 | } |
| 87 | |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 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 | |
Mike Klein | e66636c | 2020-06-19 14:03:39 -0500 | [diff] [blame] | 105 | if (auto blitter = SkCreateRasterPipelineBlitter(fDst, p, pipeline, isOpaque, &alloc, |
| 106 | fRC->clipShader())) { |
Mike Klein | e66636c | 2020-06-19 14:03:39 -0500 | [diff] [blame] | 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 | } |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 113 | |
Mike Klein | e66636c | 2020-06-19 14:03:39 -0500 | [diff] [blame] | 114 | SkMatrix mx; |
| 115 | mx.setRSXform(xform[i]); |
| 116 | mx.preTranslate(-textures[i].fLeft, -textures[i].fTop); |
| 117 | mx.postConcat(fMatrixProvider->localToDevice()); |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 118 | |
Mike Klein | e66636c | 2020-06-19 14:03:39 -0500 | [diff] [blame] | 119 | if (updator->update(mx, nullptr)) { |
Mike Reed | 375721d | 2020-08-07 16:00:06 -0400 | [diff] [blame] | 120 | fill_rect(mx, *fRC, textures[i], blitter); |
Mike Klein | e66636c | 2020-06-19 14:03:39 -0500 | [diff] [blame] | 121 | } |
Mike Klein | e150888 | 2020-04-28 08:45:01 -0500 | [diff] [blame] | 122 | } |
Mike Reed | 560e917 | 2019-08-05 13:04:18 -0400 | [diff] [blame] | 123 | } |
| 124 | } |