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