blob: b4534207457b831bf47b4bd479f4e6b2b210d67b [file] [log] [blame]
fmalita1dedc3d2015-08-04 13:53:14 -07001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
9#include "include/core/SkBitmap.h"
10#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040011#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/core/SkImageGenerator.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040017#include "include/core/SkImageInfo.h"
18#include "include/core/SkMatrix.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/core/SkPaint.h"
20#include "include/core/SkPath.h"
21#include "include/core/SkPicture.h"
22#include "include/core/SkPictureRecorder.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040023#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "include/effects/SkGradientShader.h"
34#include "include/pathops/SkPathOps.h"
35#include "include/utils/SkTextUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "tools/ToolUtils.h"
fmalita1dedc3d2015-08-04 13:53:14 -070037
Ben Wagner7fde8e12019-05-01 17:28:53 -040038#include <string.h>
39#include <memory>
40
fmalita1dedc3d2015-08-04 13:53:14 -070041static void draw_vector_logo(SkCanvas* canvas, const SkRect& viewBox) {
mtkleindbfd7ab2016-09-01 11:24:54 -070042 constexpr char kSkiaStr[] = "SKIA";
43 constexpr SkScalar kGradientPad = .1f;
44 constexpr SkScalar kVerticalSpacing = 0.25f;
45 constexpr SkScalar kAccentScale = 1.20f;
fmalita1dedc3d2015-08-04 13:53:14 -070046
47 SkPaint paint;
48 paint.setAntiAlias(true);
Mike Reedf78b7ea2018-12-25 22:06:17 -050049
Mike Kleinea3f0142019-03-20 11:12:10 -050050 SkFont font(ToolUtils::create_portable_typeface());
Mike Reedf78b7ea2018-12-25 22:06:17 -050051 font.setSubpixel(true);
52 font.setEmbolden(true);
fmalita1dedc3d2015-08-04 13:53:14 -070053
54 SkPath path;
55 SkRect iBox, skiBox, skiaBox;
Ben Wagner51e15a62019-05-07 15:38:46 -040056 SkTextUtils::GetPath("SKI", 3, SkTextEncoding::kUTF8, 0, 0, font, &path);
fmalita1dedc3d2015-08-04 13:53:14 -070057 TightBounds(path, &skiBox);
Ben Wagner51e15a62019-05-07 15:38:46 -040058 SkTextUtils::GetPath("I", 1, SkTextEncoding::kUTF8, 0, 0, font, &path);
fmalita1dedc3d2015-08-04 13:53:14 -070059 TightBounds(path, &iBox);
60 iBox.offsetTo(skiBox.fRight - iBox.width(), iBox.fTop);
61
62 const size_t textLen = strlen(kSkiaStr);
Ben Wagner51e15a62019-05-07 15:38:46 -040063 SkTextUtils::GetPath(kSkiaStr, textLen, SkTextEncoding::kUTF8, 0, 0, font, &path);
fmalita1dedc3d2015-08-04 13:53:14 -070064 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 SkMatrix m;
71 m.setRectToRect(skiaBox, viewBox, SkMatrix::kFill_ScaleToFit);
72 SkAutoCanvasRestore acr(canvas, true);
73 canvas->concat(m);
74
75 canvas->drawCircle(iBox.centerX(),
76 iBox.y() - (0.5f + kVerticalSpacing) * accentSize,
77 accentSize / 2,
78 paint);
79
80 path.reset();
81 path.moveTo(iBox.centerX() - accentSize / 2, iBox.bottom() + kVerticalSpacing * accentSize);
82 path.rLineTo(accentSize, 0);
83 path.lineTo(iBox.centerX(), underlineY);
84 canvas->drawPath(path, paint);
85
86 SkRect underlineRect = SkRect::MakeLTRB(iBox.centerX() - iBox.width() * accentSize * 3,
87 underlineY,
88 iBox.centerX(),
89 underlineY + accentSize / 10);
90 const SkPoint pts1[] = { SkPoint::Make(underlineRect.x(), 0),
91 SkPoint::Make(iBox.centerX(), 0) };
92 const SkScalar pos1[] = { 0, 0.75f };
93 const SkColor colors1[] = { SK_ColorTRANSPARENT, SK_ColorBLACK };
94 SkASSERT(SK_ARRAY_COUNT(pos1) == SK_ARRAY_COUNT(colors1));
reed1a9b9642016-03-13 14:13:58 -070095 paint.setShader(SkGradientShader::MakeLinear(pts1, colors1, pos1, SK_ARRAY_COUNT(pos1),
Mike Reedfae8fce2019-04-03 10:27:45 -040096 SkTileMode::kClamp));
fmalita1dedc3d2015-08-04 13:53:14 -070097 canvas->drawRect(underlineRect, paint);
98
99 const SkPoint pts2[] = { SkPoint::Make(iBox.x() - iBox.width() * kGradientPad, 0),
100 SkPoint::Make(iBox.right() + iBox.width() * kGradientPad, 0) };
101 const SkScalar pos2[] = { 0, .01f, 1.0f/3, 1.0f/3, 2.0f/3, 2.0f/3, .99f, 1 };
102 const SkColor colors2[] = {
103 SK_ColorBLACK,
104 0xffca5139,
105 0xffca5139,
106 0xff8dbd53,
107 0xff8dbd53,
108 0xff5460a5,
109 0xff5460a5,
110 SK_ColorBLACK
111 };
112 SkASSERT(SK_ARRAY_COUNT(pos2) == SK_ARRAY_COUNT(colors2));
reed1a9b9642016-03-13 14:13:58 -0700113 paint.setShader(SkGradientShader::MakeLinear(pts2, colors2, pos2, SK_ARRAY_COUNT(pos2),
Mike Reedfae8fce2019-04-03 10:27:45 -0400114 SkTileMode::kClamp));
Ben Wagner51e15a62019-05-07 15:38:46 -0400115 canvas->drawSimpleText(kSkiaStr, textLen, SkTextEncoding::kUTF8, 0, 0, font, paint);
fmalita1dedc3d2015-08-04 13:53:14 -0700116}
117
118// This GM exercises SkPictureImageGenerator features
119// (in particular its matrix vs. bounds semantics).
120class PictureGeneratorGM : public skiagm::GM {
121protected:
122 SkString onShortName() override {
123 return SkString("pictureimagegenerator");
124 }
125
126 SkISize onISize() override {
127 return SkISize::Make(1160, 860);
128 }
129
130 void onOnceBeforeDraw() override {
131 const SkRect rect = SkRect::MakeWH(kPictureWidth, kPictureHeight);
132 SkPictureRecorder recorder;
133 SkCanvas* canvas = recorder.beginRecording(rect);
134 draw_vector_logo(canvas, rect);
reedca2622b2016-03-18 07:25:55 -0700135 fPicture = recorder.finishRecordingAsPicture();
fmalita1dedc3d2015-08-04 13:53:14 -0700136 }
137
138 void onDraw(SkCanvas* canvas) override {
139 const struct {
140 SkISize size;
141 SkScalar scaleX, scaleY;
142 SkScalar opacity;
143 } configs[] = {
144 { SkISize::Make(200, 100), 1, 1, 1 },
145 { SkISize::Make(200, 200), 1, 1, 1 },
146 { SkISize::Make(200, 200), 1, 2, 1 },
147 { SkISize::Make(400, 200), 2, 2, 1 },
148
149 { SkISize::Make(200, 100), 1, 1, 0.9f },
150 { SkISize::Make(200, 200), 1, 1, 0.75f },
151 { SkISize::Make(200, 200), 1, 2, 0.5f },
152 { SkISize::Make(400, 200), 2, 2, 0.25f },
153
154 { SkISize::Make(200, 200), 0.5f, 1, 1 },
155 { SkISize::Make(200, 200), 1, 0.5f, 1 },
156 { SkISize::Make(200, 200), 0.5f, 0.5f, 1 },
157 { SkISize::Make(200, 200), 2, 2, 1 },
158
159 { SkISize::Make(200, 100), -1, 1, 1 },
160 { SkISize::Make(200, 100), 1, -1, 1 },
161 { SkISize::Make(200, 100), -1, -1, 1 },
162 { SkISize::Make(200, 100), -1, -1, 0.5f },
163 };
164
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500165 auto srgbColorSpace = SkColorSpace::MakeSRGB();
fmalita1dedc3d2015-08-04 13:53:14 -0700166 const unsigned kDrawsPerRow = 4;
167 const SkScalar kDrawSize = 250;
168
169 for (size_t i = 0; i < SK_ARRAY_COUNT(configs); ++i) {
170 SkPaint p;
Mike Reed9407e242019-02-15 16:13:57 -0500171 p.setAlphaf(configs[i].opacity);
fmalita1dedc3d2015-08-04 13:53:14 -0700172
Mike Reed1f607332020-05-21 12:11:27 -0400173 SkMatrix m = SkMatrix::Scale(configs[i].scaleX, configs[i].scaleY);
fmalita1dedc3d2015-08-04 13:53:14 -0700174 if (configs[i].scaleX < 0) {
175 m.postTranslate(SkIntToScalar(configs[i].size.width()), 0);
176 }
177 if (configs[i].scaleY < 0) {
178 m.postTranslate(0, SkIntToScalar(configs[i].size.height()));
179 }
Mike Reed185130c2017-02-15 15:14:16 -0500180 std::unique_ptr<SkImageGenerator> gen =
181 SkImageGenerator::MakeFromPicture(configs[i].size, fPicture, &m,
Brian Osman138ea972016-12-16 11:55:18 -0500182 p.getAlpha() != 255 ? &p : nullptr,
Mike Reed185130c2017-02-15 15:14:16 -0500183 SkImage::BitDepth::kU8, srgbColorSpace);
Brian Osman138ea972016-12-16 11:55:18 -0500184
Mike Reed693fdbd2017-01-12 10:13:40 -0500185 SkImageInfo bmInfo = gen->getInfo().makeColorSpace(canvas->imageInfo().refColorSpace());
Brian Osman138ea972016-12-16 11:55:18 -0500186
fmalita1dedc3d2015-08-04 13:53:14 -0700187 SkBitmap bm;
Mike Reed4e3abc12017-04-07 12:04:23 -0400188 bm.allocPixels(bmInfo);
189 SkAssertResult(gen->getPixels(bm.info(), bm.getPixels(), bm.rowBytes()));
fmalita1dedc3d2015-08-04 13:53:14 -0700190
191 const SkScalar x = kDrawSize * (i % kDrawsPerRow);
192 const SkScalar y = kDrawSize * (i / kDrawsPerRow);
193
194 p.setColor(0xfff0f0f0);
Mike Reed9407e242019-02-15 16:13:57 -0500195 p.setAlphaf(1.0f);
fmalita1dedc3d2015-08-04 13:53:14 -0700196 canvas->drawRect(SkRect::MakeXYWH(x, y,
197 SkIntToScalar(bm.width()),
198 SkIntToScalar(bm.height())), p);
199 canvas->drawBitmap(bm, x, y);
200 }
201 }
202
203private:
reedca2622b2016-03-18 07:25:55 -0700204 sk_sp<SkPicture> fPicture;
fmalita1dedc3d2015-08-04 13:53:14 -0700205
206 const SkScalar kPictureWidth = 200;
207 const SkScalar kPictureHeight = 100;
208
209 typedef skiagm::GM INHERITED;
210};
211
halcanary385fe4d2015-08-26 13:07:48 -0700212DEF_GM(return new PictureGeneratorGM;)