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