blob: 04b70643942d880fd22f3c7664fe3cfbf938bba1 [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
8#include "gm.h"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
fmalita1dedc3d2015-08-04 13:53:14 -070010#include "SkBitmap.h"
11#include "SkCanvas.h"
Mike Reedf78b7ea2018-12-25 22:06:17 -050012#include "SkFontPriv.h"
fmalita1dedc3d2015-08-04 13:53:14 -070013#include "SkGradientShader.h"
14#include "SkImageGenerator.h"
15#include "SkPaint.h"
Mike Reedc090c642017-05-16 10:39:06 -040016#include "SkPath.h"
fmalita1dedc3d2015-08-04 13:53:14 -070017#include "SkPathOps.h"
18#include "SkPicture.h"
19#include "SkPictureRecorder.h"
Mike Reedf78b7ea2018-12-25 22:06:17 -050020#include "SkTextUtils.h"
fmalita1dedc3d2015-08-04 13:53:14 -070021
22static void draw_vector_logo(SkCanvas* canvas, const SkRect& viewBox) {
mtkleindbfd7ab2016-09-01 11:24:54 -070023 constexpr char kSkiaStr[] = "SKIA";
24 constexpr SkScalar kGradientPad = .1f;
25 constexpr SkScalar kVerticalSpacing = 0.25f;
26 constexpr SkScalar kAccentScale = 1.20f;
fmalita1dedc3d2015-08-04 13:53:14 -070027
28 SkPaint paint;
29 paint.setAntiAlias(true);
Mike Reedf78b7ea2018-12-25 22:06:17 -050030
31 SkFont font(sk_tool_utils::create_portable_typeface());
32 font.setSubpixel(true);
33 font.setEmbolden(true);
fmalita1dedc3d2015-08-04 13:53:14 -070034
35 SkPath path;
36 SkRect iBox, skiBox, skiaBox;
Mike Reedf78b7ea2018-12-25 22:06:17 -050037 SkTextUtils::GetPath("SKI", 3, kUTF8_SkTextEncoding, 0, 0, font, &path);
fmalita1dedc3d2015-08-04 13:53:14 -070038 TightBounds(path, &skiBox);
Mike Reedf78b7ea2018-12-25 22:06:17 -050039 SkTextUtils::GetPath("I", 1, kUTF8_SkTextEncoding, 0, 0, font, &path);
fmalita1dedc3d2015-08-04 13:53:14 -070040 TightBounds(path, &iBox);
41 iBox.offsetTo(skiBox.fRight - iBox.width(), iBox.fTop);
42
43 const size_t textLen = strlen(kSkiaStr);
Mike Reedf78b7ea2018-12-25 22:06:17 -050044 SkTextUtils::GetPath(kSkiaStr, textLen, kUTF8_SkTextEncoding, 0, 0, font, &path);
fmalita1dedc3d2015-08-04 13:53:14 -070045 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));
reed1a9b9642016-03-13 14:13:58 -070076 paint.setShader(SkGradientShader::MakeLinear(pts1, colors1, pos1, SK_ARRAY_COUNT(pos1),
77 SkShader::kClamp_TileMode));
fmalita1dedc3d2015-08-04 13:53:14 -070078 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));
reed1a9b9642016-03-13 14:13:58 -070094 paint.setShader(SkGradientShader::MakeLinear(pts2, colors2, pos2, SK_ARRAY_COUNT(pos2),
95 SkShader::kClamp_TileMode));
Mike Reedf78b7ea2018-12-25 22:06:17 -050096 canvas->drawSimpleText(kSkiaStr, textLen, kUTF8_SkTextEncoding, 0, 0, font, paint);
fmalita1dedc3d2015-08-04 13:53:14 -070097}
98
99// This GM exercises SkPictureImageGenerator features
100// (in particular its matrix vs. bounds semantics).
101class PictureGeneratorGM : public skiagm::GM {
102protected:
103 SkString onShortName() override {
104 return SkString("pictureimagegenerator");
105 }
106
107 SkISize onISize() override {
108 return SkISize::Make(1160, 860);
109 }
110
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);
reedca2622b2016-03-18 07:25:55 -0700116 fPicture = recorder.finishRecordingAsPicture();
fmalita1dedc3d2015-08-04 13:53:14 -0700117 }
118
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
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500146 auto srgbColorSpace = SkColorSpace::MakeSRGB();
fmalita1dedc3d2015-08-04 13:53:14 -0700147 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 }
Mike Reed185130c2017-02-15 15:14:16 -0500161 std::unique_ptr<SkImageGenerator> gen =
162 SkImageGenerator::MakeFromPicture(configs[i].size, fPicture, &m,
Brian Osman138ea972016-12-16 11:55:18 -0500163 p.getAlpha() != 255 ? &p : nullptr,
Mike Reed185130c2017-02-15 15:14:16 -0500164 SkImage::BitDepth::kU8, srgbColorSpace);
Brian Osman138ea972016-12-16 11:55:18 -0500165
Mike Reed693fdbd2017-01-12 10:13:40 -0500166 SkImageInfo bmInfo = gen->getInfo().makeColorSpace(canvas->imageInfo().refColorSpace());
Brian Osman138ea972016-12-16 11:55:18 -0500167
fmalita1dedc3d2015-08-04 13:53:14 -0700168 SkBitmap bm;
Mike Reed4e3abc12017-04-07 12:04:23 -0400169 bm.allocPixels(bmInfo);
170 SkAssertResult(gen->getPixels(bm.info(), bm.getPixels(), bm.rowBytes()));
fmalita1dedc3d2015-08-04 13:53:14 -0700171
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
184private:
reedca2622b2016-03-18 07:25:55 -0700185 sk_sp<SkPicture> fPicture;
fmalita1dedc3d2015-08-04 13:53:14 -0700186
187 const SkScalar kPictureWidth = 200;
188 const SkScalar kPictureHeight = 100;
189
190 typedef skiagm::GM INHERITED;
191};
192
halcanary385fe4d2015-08-26 13:07:48 -0700193DEF_GM(return new PictureGeneratorGM;)