• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.h"
9 #include "sk_tool_utils.h"
10 #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 
draw_vector_logo(SkCanvas * canvas,const SkRect & viewBox)19 static void draw_vector_logo(SkCanvas* canvas, const SkRect& viewBox) {
20     constexpr char kSkiaStr[] = "SKIA";
21     constexpr SkScalar kGradientPad = .1f;
22     constexpr SkScalar kVerticalSpacing = 0.25f;
23     constexpr SkScalar kAccentScale = 1.20f;
24 
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));
72     paint.setShader(SkGradientShader::MakeLinear(pts1, colors1, pos1, SK_ARRAY_COUNT(pos1),
73                                                  SkShader::kClamp_TileMode));
74     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));
90     paint.setShader(SkGradientShader::MakeLinear(pts2, colors2, pos2, SK_ARRAY_COUNT(pos2),
91                                                  SkShader::kClamp_TileMode));
92     canvas->drawText(kSkiaStr, textLen, 0, 0, paint);
93 }
94 
95 // This GM exercises SkPictureImageGenerator features
96 // (in particular its matrix vs. bounds semantics).
97 class PictureGeneratorGM : public skiagm::GM {
98 protected:
onShortName()99     SkString onShortName() override {
100         return SkString("pictureimagegenerator");
101     }
102 
onISize()103     SkISize onISize() override {
104         return SkISize::Make(1160, 860);
105     }
106 
onOnceBeforeDraw()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);
112         fPicture = recorder.finishRecordingAsPicture();
113     }
114 
onDraw(SkCanvas * canvas)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 
142         auto srgbColorSpace = SkColorSpace::MakeSRGB();
143         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             }
157             std::unique_ptr<SkImageGenerator> gen =
158                 SkImageGenerator::MakeFromPicture(configs[i].size, fPicture, &m,
159                                                  p.getAlpha() != 255 ? &p : nullptr,
160                                                  SkImage::BitDepth::kU8, srgbColorSpace);
161 
162             SkImageInfo bmInfo = gen->getInfo().makeColorSpace(canvas->imageInfo().refColorSpace());
163 
164             SkBitmap bm;
165             SkAssertResult(gen->tryGenerateBitmap(&bm, bmInfo, nullptr));
166 
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 
179 private:
180     sk_sp<SkPicture> fPicture;
181 
182     const SkScalar kPictureWidth = 200;
183     const SkScalar kPictureHeight = 100;
184 
185     typedef skiagm::GM INHERITED;
186 };
187 
188 DEF_GM(return new PictureGeneratorGM;)
189