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