Flutter Engine
The Flutter Engine
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
textblobmixedsizes.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"
18#include "include/core/SkRect.h"
21#include "include/core/SkSize.h"
30#if defined(SK_GRAPHITE)
32#endif
33#include "src/base/SkRandom.h"
34#include "src/core/SkBlurMask.h"
35#include "tools/Resources.h"
36#include "tools/ToolUtils.h"
38
39#include <string.h>
40
41namespace skiagm {
42class TextBlobMixedSizes : public GM {
43public:
44 // This gm tests that textblobs of mixed sizes with a large glyph will render properly
45 TextBlobMixedSizes(bool useDFT) : fUseDFT(useDFT) {}
46
47protected:
48 void onOnceBeforeDraw() override {
50
51 // make textblob. To stress distance fields, we choose sizes appropriately
53 if (!tf) {
55 }
56 SkFont font(tf, 262);
57 font.setSubpixel(true);
59
60 const char* text = "Skia";
61
63
64 // large
66 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
67 SkScalar yOffset = bounds.height();
68 font.setSize(162);
69
71
72 // Medium
73 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
74 yOffset += bounds.height();
75 font.setSize(72);
76
78
79 // Small
80 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
81 yOffset += bounds.height();
82 font.setSize(32);
83
85
86 // micro (will fall out of distance field text even if distance field text is enabled)
87 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
88 yOffset += bounds.height();
89 font.setSize(14);
90
92
93 // Zero size.
94 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
95 yOffset += bounds.height();
96 font.setSize(0);
97
99
100 // build
101 fBlob = builder.make();
102 }
103
104 SkString getName() const override {
105 return SkStringPrintf("textblobmixedsizes%s",
106 fUseDFT ? "_df" : "");
107 }
108
109 SkISize getISize() override { return SkISize::Make(kWidth, kHeight); }
110
111 void onDraw(SkCanvas* inputCanvas) override {
112 SkCanvas* canvas = inputCanvas;
114 if (fUseDFT) {
115 // Create a new Canvas to enable DFT
116 auto ctx = inputCanvas->recordingContext();
117#if defined(SK_GRAPHITE)
118 auto recorder = inputCanvas->recorder();
119#endif
120 SkISize size = this->getISize();
121 if (!inputCanvas->getBaseLayerSize().isEmpty()) {
122 size = inputCanvas->getBaseLayerSize();
123 }
124 sk_sp<SkColorSpace> colorSpace = inputCanvas->imageInfo().refColorSpace();
125 SkImageInfo info = SkImageInfo::MakeN32(size.width(), size.height(),
126 kPremul_SkAlphaType, colorSpace);
127 SkSurfaceProps inputProps;
128 inputCanvas->getProps(&inputProps);
129 SkSurfaceProps props(
131 inputProps.pixelGeometry());
132#if defined(SK_GRAPHITE)
133 if (recorder) {
135 } else
136#endif
137 {
139 }
140 canvas = surface ? surface->getCanvas() : inputCanvas;
141 // init our new canvas with the old canvas's matrix
142 canvas->setMatrix(inputCanvas->getTotalMatrix());
143 }
144 canvas->drawColor(SK_ColorWHITE);
145
146 SkRect bounds = fBlob->bounds();
147
148 const int kPadX = SkScalarFloorToInt(bounds.width() / 3);
149 const int kPadY = SkScalarFloorToInt(bounds.height() / 3);
150
151 int rowCount = 0;
152 canvas->translate(SkIntToScalar(kPadX), SkIntToScalar(kPadY));
153 canvas->save();
154 SkRandom random;
155
157 if (!fUseDFT) {
158 paint.setColor(SK_ColorWHITE);
159 }
160 paint.setAntiAlias(false);
161
163
164 // setup blur paint
165 SkPaint blurPaint(paint);
166 blurPaint.setColor(SK_ColorBLACK);
168
169 for (int i = 0; i < 4; i++) {
170 canvas->save();
171 switch (i % 2) {
172 case 0:
173 canvas->rotate(random.nextF() * 45.f);
174 break;
175 case 1:
176 canvas->rotate(-random.nextF() * 45.f);
177 break;
178 }
179 if (!fUseDFT) {
180 canvas->drawTextBlob(fBlob, 0, 0, blurPaint);
181 }
182 canvas->drawTextBlob(fBlob, 0, 0, paint);
183 canvas->restore();
184 canvas->translate(bounds.width() + SK_Scalar1 * kPadX, 0);
185 ++rowCount;
186 if ((bounds.width() + 2 * kPadX) * rowCount > kWidth) {
187 canvas->restore();
188 canvas->translate(0, bounds.height() + SK_Scalar1 * kPadY);
189 canvas->save();
190 rowCount = 0;
191 }
192 }
193 canvas->restore();
194
195 // render offscreen buffer
196 if (surface) {
197 SkAutoCanvasRestore acr(inputCanvas, true);
198 // since we prepended this matrix already, we blit using identity
199 inputCanvas->resetMatrix();
200 inputCanvas->drawImage(surface->makeImageSnapshot().get(), 0, 0);
201 }
202 }
203
204private:
205 sk_sp<SkTextBlob> fBlob;
206
207 static constexpr int kWidth = 2100;
208 static constexpr int kHeight = 1900;
209
210 bool fUseDFT;
211
212 using INHERITED = GM;
213};
214
215//////////////////////////////////////////////////////////////////////////////
216
217DEF_GM( return new TextBlobMixedSizes(false); )
218DEF_GM( return new TextBlobMixedSizes(true); )
219} // namespace skiagm
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
@ kNormal_SkBlurStyle
fuzzy inside and outside
Definition: SkBlurTypes.h:12
constexpr SkColor SK_ColorBLACK
Definition: SkColor.h:103
constexpr SkColor SK_ColorWHITE
Definition: SkColor.h:122
@ kUTF8
uses bytes to represent UTF-8 or ASCII
#define SK_Scalar1
Definition: SkScalar.h:18
#define SkIntToScalar(x)
Definition: SkScalar.h:57
#define SkScalarFloorToInt(x)
Definition: SkScalar.h:35
SK_API SkString SkStringPrintf(const char *format,...) SK_PRINTF_LIKE(1
Creates a new string and writes into it using a printf()-style format.
static SkScalar SK_SPI ConvertRadiusToSigma(SkScalar radius)
Definition: SkBlurMask.cpp:39
bool getProps(SkSurfaceProps *props) const
Definition: SkCanvas.cpp:1214
void restore()
Definition: SkCanvas.cpp:461
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
void drawColor(SkColor color, SkBlendMode mode=SkBlendMode::kSrcOver)
Definition: SkCanvas.h:1182
virtual GrRecordingContext * recordingContext() const
Definition: SkCanvas.cpp:1637
virtual skgpu::graphite::Recorder * recorder() const
Definition: SkCanvas.cpp:1641
virtual SkISize getBaseLayerSize() const
Definition: SkCanvas.cpp:369
void rotate(SkScalar degrees)
Definition: SkCanvas.cpp:1300
SkMatrix getTotalMatrix() const
Definition: SkCanvas.cpp:1629
void resetMatrix()
Definition: SkCanvas.cpp:1355
int save()
Definition: SkCanvas.cpp:447
void setMatrix(const SkM44 &matrix)
Definition: SkCanvas.cpp:1349
SkImageInfo imageInfo() const
Definition: SkCanvas.cpp:1206
void drawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y, const SkPaint &paint)
Definition: SkCanvas.cpp:2484
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition: SkCanvas.h:1528
Definition: SkFont.h:35
@ kSubpixelAntiAlias
glyph positioned in pixel using transparency
static sk_sp< SkMaskFilter > MakeBlur(SkBlurStyle style, SkScalar sigma, bool respectCTM=true)
void setColor(SkColor color)
Definition: SkPaint.cpp:119
void setMaskFilter(sk_sp< SkMaskFilter > maskFilter)
float nextF()
Definition: SkRandom.h:55
@ kUseDeviceIndependentFonts_Flag
uint32_t flags() const
SkPixelGeometry pixelGeometry() const
const SkRect & bounds() const
Definition: SkTextBlob.h:53
Definition: gm.h:110
GM(SkColor backgroundColor=SK_ColorWHITE)
Definition: gm.cpp:81
void onDraw(SkCanvas *inputCanvas) override
SkString getName() const override
const Paint & paint
Definition: color_source.cc:38
VkSurfaceKHR surface
Definition: main.cc:49
float SkScalar
Definition: extension.cpp:12
std::u16string text
Optional< SkRect > bounds
Definition: SkRecords.h:189
SK_API sk_sp< SkSurface > RenderTarget(GrRecordingContext *context, skgpu::Budgeted budgeted, const SkImageInfo &imageInfo, int sampleCount, GrSurfaceOrigin surfaceOrigin, const SkSurfaceProps *surfaceProps, bool shouldCreateWithMips=false, bool isProtected=false)
sk_sp< SkTypeface > DefaultPortableTypeface()
void add_to_text_blob(SkTextBlobBuilder *builder, const char *text, const SkFont &font, SkScalar x, SkScalar y)
Definition: ToolUtils.cpp:228
sk_sp< SkTypeface > CreateTypefaceFromResource(const char *resource, int ttcIndex)
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.
DEF_GM(return F(C(clipbox), 0.0f, 0.0f, {})) DEF_GM(return F(C(clipbox)
Definition: SkSize.h:16
bool isEmpty() const
Definition: SkSize.h:31
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
sk_sp< SkColorSpace > refColorSpace() const
static SkImageInfo MakeN32(int width, int height, SkAlphaType at)