• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015 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/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkColorSpace.h"
13 #include "include/core/SkFont.h"
14 #include "include/core/SkFontTypes.h"
15 #include "include/core/SkImage.h"
16 #include "include/core/SkImageGenerator.h"
17 #include "include/core/SkImageInfo.h"
18 #include "include/core/SkMatrix.h"
19 #include "include/core/SkPaint.h"
20 #include "include/core/SkPath.h"
21 #include "include/core/SkPicture.h"
22 #include "include/core/SkPictureRecorder.h"
23 #include "include/core/SkPoint.h"
24 #include "include/core/SkRect.h"
25 #include "include/core/SkRefCnt.h"
26 #include "include/core/SkScalar.h"
27 #include "include/core/SkShader.h"
28 #include "include/core/SkSize.h"
29 #include "include/core/SkString.h"
30 #include "include/core/SkTileMode.h"
31 #include "include/core/SkTypeface.h"
32 #include "include/core/SkTypes.h"
33 #include "include/effects/SkGradientShader.h"
34 #include "include/utils/SkTextUtils.h"
35 #include "src/image/SkImageGeneratorPriv.h"
36 #include "tools/ToolUtils.h"
37 #include "tools/fonts/FontToolUtils.h"
38 
39 #include <string.h>
40 #include <memory>
41 
draw_vector_logo(SkCanvas * canvas,const SkRect & viewBox)42 static void draw_vector_logo(SkCanvas* canvas, const SkRect& viewBox) {
43     constexpr char kSkiaStr[] = "SKIA";
44     constexpr SkScalar kGradientPad = .1f;
45     constexpr SkScalar kVerticalSpacing = 0.25f;
46     constexpr SkScalar kAccentScale = 1.20f;
47 
48     SkPaint paint;
49     paint.setAntiAlias(true);
50 
51     SkFont font = ToolUtils::DefaultPortableFont();
52     font.setSubpixel(true);
53     font.setEmbolden(true);
54 
55     SkPath path;
56     SkTextUtils::GetPath("SKI", 3, SkTextEncoding::kUTF8, 0, 0, font, &path);
57     auto skiBox = path.computeTightBounds();
58 
59     SkTextUtils::GetPath("I", 1, SkTextEncoding::kUTF8, 0, 0, font, &path);
60     auto iBox = path.computeTightBounds();
61     iBox.offsetTo(skiBox.fRight - iBox.width(), iBox.fTop);
62 
63     const size_t textLen = strlen(kSkiaStr);
64     SkTextUtils::GetPath(kSkiaStr, textLen, SkTextEncoding::kUTF8, 0, 0, font, &path);
65     auto skiaBox = path.computeTightBounds();
66     skiaBox.outset(0, 2 * iBox.width() * (kVerticalSpacing + 1));
67 
68     const SkScalar accentSize = iBox.width() * kAccentScale;
69     const SkScalar underlineY = iBox.bottom() +
70         (kVerticalSpacing + SkScalarSqrt(3) / 2) * accentSize;
71     SkAutoCanvasRestore acr(canvas, true);
72     canvas->concat(SkMatrix::RectToRect(skiaBox, viewBox));
73 
74     canvas->drawCircle(iBox.centerX(),
75                        iBox.y() - (0.5f + kVerticalSpacing) * accentSize,
76                        accentSize / 2,
77                        paint);
78 
79     path.reset();
80     path.moveTo(iBox.centerX() - accentSize / 2, iBox.bottom() + kVerticalSpacing * accentSize);
81     path.rLineTo(accentSize, 0);
82     path.lineTo(iBox.centerX(), underlineY);
83     canvas->drawPath(path, paint);
84 
85     SkRect underlineRect = SkRect::MakeLTRB(iBox.centerX() - iBox.width() * accentSize * 3,
86                                             underlineY,
87                                             iBox.centerX(),
88                                             underlineY + accentSize / 10);
89     const SkPoint pts1[] = { SkPoint::Make(underlineRect.x(), 0),
90                              SkPoint::Make(iBox.centerX(), 0) };
91     const SkScalar pos1[] = { 0, 0.75f };
92     const SkColor colors1[] = { SK_ColorTRANSPARENT, SK_ColorBLACK };
93     SkASSERT(std::size(pos1) == std::size(colors1));
94     paint.setShader(SkGradientShader::MakeLinear(pts1, colors1, pos1, std::size(pos1),
95                                                  SkTileMode::kClamp));
96     canvas->drawRect(underlineRect, paint);
97 
98     const SkPoint pts2[] = { SkPoint::Make(iBox.x() - iBox.width() * kGradientPad, 0),
99                              SkPoint::Make(iBox.right() + iBox.width() * kGradientPad, 0) };
100     const SkScalar pos2[] = { 0, .01f, 1.0f/3, 1.0f/3, 2.0f/3, 2.0f/3, .99f, 1 };
101     const SkColor colors2[] = {
102         SK_ColorBLACK,
103         0xffca5139,
104         0xffca5139,
105         0xff8dbd53,
106         0xff8dbd53,
107         0xff5460a5,
108         0xff5460a5,
109         SK_ColorBLACK
110     };
111     SkASSERT(std::size(pos2) == std::size(colors2));
112     paint.setShader(SkGradientShader::MakeLinear(pts2, colors2, pos2, std::size(pos2),
113                                                  SkTileMode::kClamp));
114     canvas->drawSimpleText(kSkiaStr, textLen, SkTextEncoding::kUTF8, 0, 0, font, paint);
115 }
116 
117 // This GM exercises SkPictureImageGenerator features
118 // (in particular its matrix vs. bounds semantics).
119 class PictureGeneratorGM : public skiagm::GM {
120 protected:
getName() const121     SkString getName() const override { return SkString("pictureimagegenerator"); }
122 
getISize()123     SkISize getISize() override { return SkISize::Make(1160, 860); }
124 
onOnceBeforeDraw()125     void onOnceBeforeDraw() override {
126         const SkRect rect = SkRect::MakeWH(kPictureWidth, kPictureHeight);
127         SkPictureRecorder recorder;
128         SkCanvas* canvas = recorder.beginRecording(rect);
129         draw_vector_logo(canvas, rect);
130         fPicture = recorder.finishRecordingAsPicture();
131     }
132 
onDraw(SkCanvas * canvas)133     void onDraw(SkCanvas* canvas) override {
134         const struct {
135             SkISize  size;
136             SkScalar scaleX, scaleY;
137             SkScalar opacity;
138         } configs[] = {
139             { SkISize::Make(200, 100), 1, 1, 1 },
140             { SkISize::Make(200, 200), 1, 1, 1 },
141             { SkISize::Make(200, 200), 1, 2, 1 },
142             { SkISize::Make(400, 200), 2, 2, 1 },
143 
144             { SkISize::Make(200, 100), 1, 1, 0.9f  },
145             { SkISize::Make(200, 200), 1, 1, 0.75f },
146             { SkISize::Make(200, 200), 1, 2, 0.5f  },
147             { SkISize::Make(400, 200), 2, 2, 0.25f },
148 
149             { SkISize::Make(200, 200), 0.5f, 1,    1 },
150             { SkISize::Make(200, 200), 1,    0.5f, 1 },
151             { SkISize::Make(200, 200), 0.5f, 0.5f, 1 },
152             { SkISize::Make(200, 200), 2,    2,    1 },
153 
154             { SkISize::Make(200, 100), -1,  1, 1    },
155             { SkISize::Make(200, 100),  1, -1, 1    },
156             { SkISize::Make(200, 100), -1, -1, 1    },
157             { SkISize::Make(200, 100), -1, -1, 0.5f },
158         };
159 
160         auto srgbColorSpace = SkColorSpace::MakeSRGB();
161         const unsigned kDrawsPerRow = 4;
162         const SkScalar kDrawSize = 250;
163 
164         for (size_t i = 0; i < std::size(configs); ++i) {
165             SkPaint p;
166             p.setAlphaf(configs[i].opacity);
167 
168             SkMatrix m = SkMatrix::Scale(configs[i].scaleX, configs[i].scaleY);
169             if (configs[i].scaleX < 0) {
170                 m.postTranslate(SkIntToScalar(configs[i].size.width()), 0);
171             }
172             if (configs[i].scaleY < 0) {
173                 m.postTranslate(0, SkIntToScalar(configs[i].size.height()));
174             }
175             std::unique_ptr<SkImageGenerator> gen =
176                     SkImageGenerators::MakeFromPicture(configs[i].size,
177                                                        fPicture,
178                                                        &m,
179                                                        p.getAlpha() != 255 ? &p : nullptr,
180                                                        SkImages::BitDepth::kU8,
181                                                        srgbColorSpace);
182 
183             SkImageInfo bmInfo = gen->getInfo().makeColorSpace(canvas->imageInfo().refColorSpace());
184 
185             SkBitmap bm;
186             bm.allocPixels(bmInfo);
187             SkAssertResult(gen->getPixels(bm.info(), bm.getPixels(), bm.rowBytes()));
188 
189             const SkScalar x = kDrawSize * (i % kDrawsPerRow);
190             const SkScalar y = kDrawSize * (i / kDrawsPerRow);
191 
192             p.setColor(0xfff0f0f0);
193             p.setAlphaf(1.0f);
194             canvas->drawRect(SkRect::MakeXYWH(x, y,
195                                               SkIntToScalar(bm.width()),
196                                               SkIntToScalar(bm.height())), p);
197             canvas->drawImage(bm.asImage(), x, y);
198         }
199     }
200 
201 private:
202     sk_sp<SkPicture> fPicture;
203 
204     const SkScalar kPictureWidth = 200;
205     const SkScalar kPictureHeight = 100;
206 
207     using INHERITED = skiagm::GM;
208 };
209 
210 DEF_GM(return new PictureGeneratorGM;)
211