Flutter Engine
The Flutter Engine
pictureimagegenerator.cpp
Go to the documentation of this file.
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"
13#include "include/core/SkFont.h"
20#include "include/core/SkPath.h"
24#include "include/core/SkRect.h"
28#include "include/core/SkSize.h"
37#include "tools/ToolUtils.h"
39
40#include <string.h>
41#include <memory>
42
43static 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
50 paint.setAntiAlias(true);
51
53 font.setSubpixel(true);
54 font.setEmbolden(true);
55
57 SkRect iBox, skiBox, skiaBox;
59 TightBounds(path, &skiBox);
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),
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[] = {
104 0xffca5139,
105 0xffca5139,
106 0xff8dbd53,
107 0xff8dbd53,
108 0xff5460a5,
109 0xff5460a5,
111 };
112 SkASSERT(std::size(pos2) == std::size(colors2));
113 paint.setShader(SkGradientShader::MakeLinear(pts2, colors2, pos2, std::size(pos2),
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).
121protected:
122 SkString getName() const override { return SkString("pictureimagegenerator"); }
123
124 SkISize getISize() override { return SkISize::Make(1160, 860); }
125
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
134 void onDraw(SkCanvas* canvas) override {
135 const struct {
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 =
178 fPicture,
179 &m,
180 p.getAlpha() != 255 ? &p : nullptr,
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
202private:
203 sk_sp<SkPicture> fPicture;
204
205 const SkScalar kPictureWidth = 200;
206 const SkScalar kPictureHeight = 100;
207
208 using INHERITED = skiagm::GM;
209};
210
211DEF_GM(return new PictureGeneratorGM;)
SkAssertResult(font.textToGlyphs("Hello", 5, SkTextEncoding::kUTF8, glyphs, std::size(glyphs))==count)
#define SkASSERT(cond)
Definition: SkAssert.h:116
uint32_t SkColor
Definition: SkColor.h:37
constexpr SkColor SK_ColorTRANSPARENT
Definition: SkColor.h:99
constexpr SkColor SK_ColorBLACK
Definition: SkColor.h:103
@ kUTF8
uses bytes to represent UTF-8 or ASCII
bool SK_API TightBounds(const SkPath &path, SkRect *result)
#define SkIntToScalar(x)
Definition: SkScalar.h:57
#define SkScalarSqrt(x)
Definition: SkScalar.h:42
SkString getName() const override
void onOnceBeforeDraw() override
SkISize getISize() override
void onDraw(SkCanvas *canvas) override
void allocPixels(const SkImageInfo &info, size_t rowBytes)
Definition: SkBitmap.cpp:258
sk_sp< SkImage > asImage() const
Definition: SkBitmap.cpp:645
int width() const
Definition: SkBitmap.h:149
size_t rowBytes() const
Definition: SkBitmap.h:238
void * getPixels() const
Definition: SkBitmap.h:283
const SkImageInfo & info() const
Definition: SkBitmap.h:139
int height() const
Definition: SkBitmap.h:158
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void drawSimpleText(const void *text, size_t byteLength, SkTextEncoding encoding, SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition: SkCanvas.cpp:2413
void drawPath(const SkPath &path, const SkPaint &paint)
Definition: SkCanvas.cpp:1747
void concat(const SkMatrix &matrix)
Definition: SkCanvas.cpp:1318
SkImageInfo imageInfo() const
Definition: SkCanvas.cpp:1206
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition: SkCanvas.h:1528
void drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, const SkPaint &paint)
Definition: SkCanvas.cpp:2707
static sk_sp< SkColorSpace > MakeSRGB()
Definition: SkFont.h:35
static sk_sp< SkShader > MakeLinear(const SkPoint pts[2], const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
static SkMatrix Scale(SkScalar sx, SkScalar sy)
Definition: SkMatrix.h:75
static SkMatrix RectToRect(const SkRect &src, const SkRect &dst, ScaleToFit mode=kFill_ScaleToFit)
Definition: SkMatrix.h:157
Definition: SkPath.h:59
SkCanvas * beginRecording(const SkRect &bounds, sk_sp< SkBBoxHierarchy > bbh)
sk_sp< SkPicture > finishRecordingAsPicture()
static void GetPath(const void *text, size_t length, SkTextEncoding, SkScalar x, SkScalar y, const SkFont &, SkPath *)
Definition: SkTextUtils.cpp:38
Definition: gm.h:110
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
#define DEF_GM(CODE)
Definition: gm.h:40
double y
double x
std::unique_ptr< SkImageGenerator > MakeFromPicture(const SkISize &, sk_sp< SkPicture >, const SkMatrix *, const SkPaint *, SkImages::BitDepth, sk_sp< SkColorSpace >, SkSurfaceProps props)
@ kU8
uses 8-bit unsigned int per color component
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
SkFont DefaultPortableFont()
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
font
Font Metadata and Metrics.
Definition: gen.py:1
static void draw_vector_logo(SkCanvas *canvas, const SkRect &viewBox)
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
sk_sp< SkColorSpace > refColorSpace() const
static constexpr SkPoint Make(float x, float y)
Definition: SkPoint_impl.h:173
void offsetTo(float newX, float newY)
Definition: SkRect.h:1043
constexpr float x() const
Definition: SkRect.h:720
void outset(float dx, float dy)
Definition: SkRect.h:1077
constexpr float y() const
Definition: SkRect.h:727
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659
SkScalar fRight
larger x-axis bounds
Definition: extension.cpp:16
constexpr float centerX() const
Definition: SkRect.h:776
constexpr float right() const
Definition: SkRect.h:748
constexpr float width() const
Definition: SkRect.h:762
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition: SkRect.h:646
constexpr float bottom() const
Definition: SkRect.h:755
SkScalar fTop
smaller y-axis bounds
Definition: extension.cpp:15