Florin Malita | 325ea32 | 2018-04-04 14:17:30 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #include "include/core/SkBlendMode.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 11 | #include "include/core/SkColor.h" |
| 12 | #include "include/core/SkImage.h" |
| 13 | #include "include/core/SkImageInfo.h" |
| 14 | #include "include/core/SkMatrix.h" |
| 15 | #include "include/core/SkPaint.h" |
| 16 | #include "include/core/SkRect.h" |
| 17 | #include "include/core/SkRefCnt.h" |
| 18 | #include "include/core/SkScalar.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "include/core/SkShader.h" |
| 20 | #include "include/core/SkSurface.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 21 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "tools/ToolUtils.h" |
Florin Malita | 325ea32 | 2018-04-04 14:17:30 -0400 | [diff] [blame] | 23 | |
| 24 | static sk_sp<SkImage> make_image(SkCanvas* rootCanvas) { |
| 25 | static constexpr SkScalar kSize = 50; |
| 26 | SkImageInfo info = SkImageInfo::MakeN32Premul(kSize, kSize); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 27 | auto surface = ToolUtils::makeSurface(rootCanvas, info); |
Florin Malita | 325ea32 | 2018-04-04 14:17:30 -0400 | [diff] [blame] | 28 | |
| 29 | SkPaint p; |
| 30 | p.setAntiAlias(true); |
| 31 | p.setColor(SK_ColorGREEN); |
| 32 | |
| 33 | surface->getCanvas()->drawCircle(kSize / 2, kSize / 2, kSize / 2, p); |
| 34 | |
| 35 | p.setStyle(SkPaint::kStroke_Style); |
| 36 | p.setColor(SK_ColorRED); |
| 37 | surface->getCanvas()->drawLine(kSize * .25f, kSize * .50f, kSize * .75f, kSize * .50f, p); |
| 38 | surface->getCanvas()->drawLine(kSize * .50f, kSize * .25f, kSize * .50f, kSize * .75f, p); |
| 39 | |
| 40 | return surface->makeImageSnapshot(); |
| 41 | } |
| 42 | |
| 43 | DEF_SIMPLE_GM(localmatrixshader_nested, canvas, 450, 1200) { |
| 44 | auto image = make_image(canvas); |
| 45 | |
| 46 | using FactoryT = sk_sp<SkShader> (*)(const sk_sp<SkImage>&, |
| 47 | const SkMatrix& inner, |
| 48 | const SkMatrix& outer); |
| 49 | static const FactoryT gFactories[] = { |
| 50 | // SkLocalMatrixShader(SkImageShader(inner), outer) |
| 51 | [](const sk_sp<SkImage>& img, const SkMatrix& inner, const SkMatrix& outer) { |
| 52 | return img->makeShader(&inner)->makeWithLocalMatrix(outer); |
| 53 | }, |
| 54 | |
| 55 | // SkLocalMatrixShader(SkLocalMatrixShader(SkImageShader(I), inner), outer) |
| 56 | [](const sk_sp<SkImage>& img, const SkMatrix& inner, const SkMatrix& outer) { |
| 57 | return img->makeShader()->makeWithLocalMatrix(inner)->makeWithLocalMatrix(outer); |
| 58 | }, |
| 59 | |
| 60 | // SkLocalMatrixShader(SkComposeShader(SkImageShader(inner)), outer) |
| 61 | [](const sk_sp<SkImage>& img, const SkMatrix& inner, const SkMatrix& outer) { |
Mike Reed | c8bea7d | 2019-04-09 13:55:36 -0400 | [diff] [blame] | 62 | return SkShaders::Blend(SkBlendMode::kSrcOver, |
| 63 | SkShaders::Color(SK_ColorTRANSPARENT), |
| 64 | img->makeShader(&inner)) |
Florin Malita | 325ea32 | 2018-04-04 14:17:30 -0400 | [diff] [blame] | 65 | ->makeWithLocalMatrix(outer); |
| 66 | }, |
| 67 | |
| 68 | // SkLocalMatrixShader(SkComposeShader(SkLocalMatrixShader(SkImageShader(I), inner)), outer) |
| 69 | [](const sk_sp<SkImage>& img, const SkMatrix& inner, const SkMatrix& outer) { |
Mike Reed | c8bea7d | 2019-04-09 13:55:36 -0400 | [diff] [blame] | 70 | return SkShaders::Blend(SkBlendMode::kSrcOver, |
| 71 | SkShaders::Color(SK_ColorTRANSPARENT), |
| 72 | img->makeShader()->makeWithLocalMatrix(inner)) |
Florin Malita | 325ea32 | 2018-04-04 14:17:30 -0400 | [diff] [blame] | 73 | ->makeWithLocalMatrix(outer); |
| 74 | }, |
| 75 | }; |
| 76 | |
| 77 | static const auto inner = SkMatrix::MakeScale(2, 2), |
| 78 | outer = SkMatrix::MakeTrans(20, 20); |
| 79 | |
| 80 | SkPaint border; |
| 81 | border.setAntiAlias(true); |
| 82 | border.setStyle(SkPaint::kStroke_Style); |
| 83 | |
| 84 | auto rect = SkRect::Make(image->bounds()); |
| 85 | SkAssertResult(SkMatrix::Concat(inner, outer).mapRect(&rect)); |
| 86 | |
| 87 | const auto drawColumn = [&]() { |
| 88 | SkAutoCanvasRestore acr(canvas, true); |
| 89 | for (const auto& f : gFactories) { |
| 90 | SkPaint p; |
| 91 | p.setShader(f(image, inner, outer)); |
| 92 | |
| 93 | canvas->drawRect(rect, p); |
| 94 | canvas->drawRect(rect, border); |
| 95 | |
| 96 | canvas->translate(0, rect.height() * 1.5f); |
| 97 | } |
| 98 | }; |
| 99 | |
| 100 | drawColumn(); |
| 101 | |
| 102 | { |
| 103 | SkAutoCanvasRestore acr(canvas, true); |
| 104 | canvas->translate(0, rect.height() * SK_ARRAY_COUNT(gFactories) * 1.5f); |
| 105 | drawColumn(); |
| 106 | } |
| 107 | |
| 108 | canvas->translate(rect.width() * 1.5f, 0); |
| 109 | canvas->scale(2, 2); |
| 110 | drawColumn(); |
| 111 | } |