• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #include "gm/gm.h"
9 #include "include/core/SkBlendMode.h"
10 #include "include/core/SkCanvas.h"
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"
19 #include "include/core/SkShader.h"
20 #include "include/core/SkSurface.h"
21 #include "include/core/SkTypes.h"
22 #include "tools/ToolUtils.h"
23 
make_image(SkCanvas * rootCanvas)24 static sk_sp<SkImage> make_image(SkCanvas* rootCanvas) {
25     static constexpr SkScalar kSize = 50;
26     SkImageInfo info = SkImageInfo::MakeN32Premul(kSize, kSize);
27     auto                      surface = ToolUtils::makeSurface(rootCanvas, info);
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)
__anon87bd32bb0102() 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)
__anon87bd32bb0202() 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)
__anon87bd32bb0302() 61         [](const sk_sp<SkImage>& img, const SkMatrix& inner, const SkMatrix& outer) {
62             return SkShaders::Blend(SkBlendMode::kSrcOver,
63                                     SkShaders::Color(SK_ColorTRANSPARENT),
64                                     img->makeShader(&inner))
65                    ->makeWithLocalMatrix(outer);
66         },
67 
68         // SkLocalMatrixShader(SkComposeShader(SkLocalMatrixShader(SkImageShader(I), inner)), outer)
__anon87bd32bb0402() 69         [](const sk_sp<SkImage>& img, const SkMatrix& inner, const SkMatrix& outer) {
70             return SkShaders::Blend(SkBlendMode::kSrcOver,
71                                     SkShaders::Color(SK_ColorTRANSPARENT),
72                                     img->makeShader()->makeWithLocalMatrix(inner))
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 
__anon87bd32bb0502() 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 }
112