xref: /aosp_15_r20/external/skia/gm/pictureimagegenerator.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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/gm.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #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"
16 #include "include/core/SkImageGenerator.h"
17 #include "include/core/SkImageInfo.h"
18 #include "include/core/SkMatrix.h"
19 #include "include/core/SkPaint.h"
20 #include "include/core/SkPath.h"
21 #include "include/core/SkPicture.h"
22 #include "include/core/SkPictureRecorder.h"
23 #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"
33 #include "include/effects/SkGradientShader.h"
34 #include "include/pathops/SkPathOps.h"
35 #include "include/utils/SkTextUtils.h"
36 #include "src/image/SkImageGeneratorPriv.h"
37 #include "tools/ToolUtils.h"
38 #include "tools/fonts/FontToolUtils.h"
39 
40 #include <string.h>
41 #include <memory>
42 
draw_vector_logo(SkCanvas * canvas,const SkRect & viewBox)43 static void draw_vector_logo(SkCanvas* canvas, const SkRect& viewBox) {
44     constexpr char kSkiaStr[] = "SKIA";
45     constexpr SkScalar kGradientPad = .1f;
46     constexpr SkScalar kVerticalSpacing = 0.25f;
47     constexpr SkScalar kAccentScale = 1.20f;
48 
49     SkPaint paint;
50     paint.setAntiAlias(true);
51 
52     SkFont font = ToolUtils::DefaultPortableFont();
53     font.setSubpixel(true);
54     font.setEmbolden(true);
55 
56     SkPath path;
57     SkRect iBox, skiBox, skiaBox;
58     SkTextUtils::GetPath("SKI", 3, SkTextEncoding::kUTF8, 0, 0, font, &path);
59     TightBounds(path, &skiBox);
60     SkTextUtils::GetPath("I", 1, SkTextEncoding::kUTF8, 0, 0, font, &path);
61     TightBounds(path, &iBox);
62     iBox.offsetTo(skiBox.fRight - iBox.width(), iBox.fTop);
63 
64     const size_t textLen = strlen(kSkiaStr);
65     SkTextUtils::GetPath(kSkiaStr, textLen, SkTextEncoding::kUTF8, 0, 0, font, &path);
66     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;
72     SkAutoCanvasRestore acr(canvas, true);
73     canvas->concat(SkMatrix::RectToRect(skiaBox, viewBox));
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(std::size(pos1) == std::size(colors1));
95     paint.setShader(SkGradientShader::MakeLinear(pts1, colors1, pos1, std::size(pos1),
96                                                  SkTileMode::kClamp));
97     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(std::size(pos2) == std::size(colors2));
113     paint.setShader(SkGradientShader::MakeLinear(pts2, colors2, pos2, std::size(pos2),
114                                                  SkTileMode::kClamp));
115     canvas->drawSimpleText(kSkiaStr, textLen, SkTextEncoding::kUTF8, 0, 0, font, paint);
116 }
117 
118 // This GM exercises SkPictureImageGenerator features
119 // (in particular its matrix vs. bounds semantics).
120 class PictureGeneratorGM : public skiagm::GM {
121 protected:
getName() const122     SkString getName() const override { return SkString("pictureimagegenerator"); }
123 
getISize()124     SkISize getISize() override { return SkISize::Make(1160, 860); }
125 
onOnceBeforeDraw()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);
131         fPicture = recorder.finishRecordingAsPicture();
132     }
133 
onDraw(SkCanvas * canvas)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 
161         auto srgbColorSpace = SkColorSpace::MakeSRGB();
162         const unsigned kDrawsPerRow = 4;
163         const SkScalar kDrawSize = 250;
164 
165         for (size_t i = 0; i < std::size(configs); ++i) {
166             SkPaint p;
167             p.setAlphaf(configs[i].opacity);
168 
169             SkMatrix m = SkMatrix::Scale(configs[i].scaleX, configs[i].scaleY);
170             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             }
176             std::unique_ptr<SkImageGenerator> gen =
177                     SkImageGenerators::MakeFromPicture(configs[i].size,
178                                                        fPicture,
179                                                        &m,
180                                                        p.getAlpha() != 255 ? &p : nullptr,
181                                                        SkImages::BitDepth::kU8,
182                                                        srgbColorSpace);
183 
184             SkImageInfo bmInfo = gen->getInfo().makeColorSpace(canvas->imageInfo().refColorSpace());
185 
186             SkBitmap bm;
187             bm.allocPixels(bmInfo);
188             SkAssertResult(gen->getPixels(bm.info(), bm.getPixels(), bm.rowBytes()));
189 
190             const SkScalar x = kDrawSize * (i % kDrawsPerRow);
191             const SkScalar y = kDrawSize * (i / kDrawsPerRow);
192 
193             p.setColor(0xfff0f0f0);
194             p.setAlphaf(1.0f);
195             canvas->drawRect(SkRect::MakeXYWH(x, y,
196                                               SkIntToScalar(bm.width()),
197                                               SkIntToScalar(bm.height())), p);
198             canvas->drawImage(bm.asImage(), x, y);
199         }
200     }
201 
202 private:
203     sk_sp<SkPicture> fPicture;
204 
205     const SkScalar kPictureWidth = 200;
206     const SkScalar kPictureHeight = 100;
207 
208     using INHERITED = skiagm::GM;
209 };
210 
211 DEF_GM(return new PictureGeneratorGM;)
212