blob: 558abf5099ef5a3f6dbb991a203559f197fc0243 [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"
Kevin Lubicke54b86b2023-04-21 08:13:07 -040036#include "src/image/SkImageGeneratorPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050037#include "tools/ToolUtils.h"
Kevin Lubicke836c3a2023-10-20 06:55:35 -040038#include "tools/fonts/FontToolUtils.h"
fmalita1dedc3d2015-08-04 13:53:14 -070039
Ben Wagner7fde8e12019-05-01 17:28:53 -040040#include <string.h>
41#include <memory>
42
fmalita1dedc3d2015-08-04 13:53:14 -070043static void draw_vector_logo(SkCanvas* canvas, const SkRect& viewBox) {
mtkleindbfd7ab2016-09-01 11:24:54 -070044 constexpr char kSkiaStr[] = "SKIA";
45 constexpr SkScalar kGradientPad = .1f;
46 constexpr SkScalar kVerticalSpacing = 0.25f;
47 constexpr SkScalar kAccentScale = 1.20f;
fmalita1dedc3d2015-08-04 13:53:14 -070048
49 SkPaint paint;
50 paint.setAntiAlias(true);
Mike Reedf78b7ea2018-12-25 22:06:17 -050051
Kevin Lubicke836c3a2023-10-20 06:55:35 -040052 SkFont font = ToolUtils::DefaultPortableFont();
Mike Reedf78b7ea2018-12-25 22:06:17 -050053 font.setSubpixel(true);
54 font.setEmbolden(true);
fmalita1dedc3d2015-08-04 13:53:14 -070055
56 SkPath path;
57 SkRect iBox, skiBox, skiaBox;
Ben Wagner51e15a62019-05-07 15:38:46 -040058 SkTextUtils::GetPath("SKI", 3, SkTextEncoding::kUTF8, 0, 0, font, &path);
fmalita1dedc3d2015-08-04 13:53:14 -070059 TightBounds(path, &skiBox);
Ben Wagner51e15a62019-05-07 15:38:46 -040060 SkTextUtils::GetPath("I", 1, SkTextEncoding::kUTF8, 0, 0, font, &path);
fmalita1dedc3d2015-08-04 13:53:14 -070061 TightBounds(path, &iBox);
62 iBox.offsetTo(skiBox.fRight - iBox.width(), iBox.fTop);
63
64 const size_t textLen = strlen(kSkiaStr);
Ben Wagner51e15a62019-05-07 15:38:46 -040065 SkTextUtils::GetPath(kSkiaStr, textLen, SkTextEncoding::kUTF8, 0, 0, font, &path);
fmalita1dedc3d2015-08-04 13:53:14 -070066 TightBounds(path, &skiaBox);
67 skiaBox.outset(0, 2 * iBox.width() * (kVerticalSpacing + 1));
68
69 const SkScalar accentSize = iBox.width() * kAccentScale;
70 const SkScalar underlineY = iBox.bottom() +
71 (kVerticalSpacing + SkScalarSqrt(3) / 2) * accentSize;
fmalita1dedc3d2015-08-04 13:53:14 -070072 SkAutoCanvasRestore acr(canvas, true);
Mike Reed2ac6ce82021-01-15 12:26:22 -050073 canvas->concat(SkMatrix::RectToRect(skiaBox, viewBox));
fmalita1dedc3d2015-08-04 13:53:14 -070074
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 };
Herb Derbyc37b3862022-06-21 09:49:17 -040094 SkASSERT(std::size(pos1) == std::size(colors1));
95 paint.setShader(SkGradientShader::MakeLinear(pts1, colors1, pos1, std::size(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 };
Herb Derbyc37b3862022-06-21 09:49:17 -0400112 SkASSERT(std::size(pos2) == std::size(colors2));
113 paint.setShader(SkGradientShader::MakeLinear(pts2, colors2, pos2, std::size(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:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +0000122 SkString getName() const override { return SkString("pictureimagegenerator"); }
fmalita1dedc3d2015-08-04 13:53:14 -0700123
Leandro Lovisolo8f023882023-08-15 21:13:52 +0000124 SkISize getISize() override { return SkISize::Make(1160, 860); }
fmalita1dedc3d2015-08-04 13:53:14 -0700125
126 void onOnceBeforeDraw() override {
127 const SkRect rect = SkRect::MakeWH(kPictureWidth, kPictureHeight);
128 SkPictureRecorder recorder;
129 SkCanvas* canvas = recorder.beginRecording(rect);
130 draw_vector_logo(canvas, rect);
reedca2622b2016-03-18 07:25:55 -0700131 fPicture = recorder.finishRecordingAsPicture();
fmalita1dedc3d2015-08-04 13:53:14 -0700132 }
133
134 void onDraw(SkCanvas* canvas) override {
135 const struct {
136 SkISize size;
137 SkScalar scaleX, scaleY;
138 SkScalar opacity;
139 } configs[] = {
140 { SkISize::Make(200, 100), 1, 1, 1 },
141 { SkISize::Make(200, 200), 1, 1, 1 },
142 { SkISize::Make(200, 200), 1, 2, 1 },
143 { SkISize::Make(400, 200), 2, 2, 1 },
144
145 { SkISize::Make(200, 100), 1, 1, 0.9f },
146 { SkISize::Make(200, 200), 1, 1, 0.75f },
147 { SkISize::Make(200, 200), 1, 2, 0.5f },
148 { SkISize::Make(400, 200), 2, 2, 0.25f },
149
150 { SkISize::Make(200, 200), 0.5f, 1, 1 },
151 { SkISize::Make(200, 200), 1, 0.5f, 1 },
152 { SkISize::Make(200, 200), 0.5f, 0.5f, 1 },
153 { SkISize::Make(200, 200), 2, 2, 1 },
154
155 { SkISize::Make(200, 100), -1, 1, 1 },
156 { SkISize::Make(200, 100), 1, -1, 1 },
157 { SkISize::Make(200, 100), -1, -1, 1 },
158 { SkISize::Make(200, 100), -1, -1, 0.5f },
159 };
160
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500161 auto srgbColorSpace = SkColorSpace::MakeSRGB();
fmalita1dedc3d2015-08-04 13:53:14 -0700162 const unsigned kDrawsPerRow = 4;
163 const SkScalar kDrawSize = 250;
164
Herb Derbyc37b3862022-06-21 09:49:17 -0400165 for (size_t i = 0; i < std::size(configs); ++i) {
fmalita1dedc3d2015-08-04 13:53:14 -0700166 SkPaint p;
Mike Reed9407e242019-02-15 16:13:57 -0500167 p.setAlphaf(configs[i].opacity);
fmalita1dedc3d2015-08-04 13:53:14 -0700168
Mike Reed1f607332020-05-21 12:11:27 -0400169 SkMatrix m = SkMatrix::Scale(configs[i].scaleX, configs[i].scaleY);
fmalita1dedc3d2015-08-04 13:53:14 -0700170 if (configs[i].scaleX < 0) {
171 m.postTranslate(SkIntToScalar(configs[i].size.width()), 0);
172 }
173 if (configs[i].scaleY < 0) {
174 m.postTranslate(0, SkIntToScalar(configs[i].size.height()));
175 }
Mike Reed185130c2017-02-15 15:14:16 -0500176 std::unique_ptr<SkImageGenerator> gen =
Kevin Lubicke54b86b2023-04-21 08:13:07 -0400177 SkImageGenerators::MakeFromPicture(configs[i].size,
178 fPicture,
179 &m,
180 p.getAlpha() != 255 ? &p : nullptr,
181 SkImages::BitDepth::kU8,
182 srgbColorSpace);
Brian Osman138ea972016-12-16 11:55:18 -0500183
Mike Reed693fdbd2017-01-12 10:13:40 -0500184 SkImageInfo bmInfo = gen->getInfo().makeColorSpace(canvas->imageInfo().refColorSpace());
Brian Osman138ea972016-12-16 11:55:18 -0500185
fmalita1dedc3d2015-08-04 13:53:14 -0700186 SkBitmap bm;
Mike Reed4e3abc12017-04-07 12:04:23 -0400187 bm.allocPixels(bmInfo);
188 SkAssertResult(gen->getPixels(bm.info(), bm.getPixels(), bm.rowBytes()));
fmalita1dedc3d2015-08-04 13:53:14 -0700189
190 const SkScalar x = kDrawSize * (i % kDrawsPerRow);
191 const SkScalar y = kDrawSize * (i / kDrawsPerRow);
192
193 p.setColor(0xfff0f0f0);
Mike Reed9407e242019-02-15 16:13:57 -0500194 p.setAlphaf(1.0f);
fmalita1dedc3d2015-08-04 13:53:14 -0700195 canvas->drawRect(SkRect::MakeXYWH(x, y,
196 SkIntToScalar(bm.width()),
197 SkIntToScalar(bm.height())), p);
Mike Reed607a3822021-01-24 19:49:21 -0500198 canvas->drawImage(bm.asImage(), x, y);
fmalita1dedc3d2015-08-04 13:53:14 -0700199 }
200 }
201
202private:
reedca2622b2016-03-18 07:25:55 -0700203 sk_sp<SkPicture> fPicture;
fmalita1dedc3d2015-08-04 13:53:14 -0700204
205 const SkScalar kPictureWidth = 200;
206 const SkScalar kPictureHeight = 100;
207
John Stiles7571f9e2020-09-02 22:42:33 -0400208 using INHERITED = skiagm::GM;
fmalita1dedc3d2015-08-04 13:53:14 -0700209};
210
halcanary385fe4d2015-08-26 13:07:48 -0700211DEF_GM(return new PictureGeneratorGM;)