Flutter Engine
The Flutter Engine
dftext_blob_persp.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2017 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"
12#include "include/core/SkFont.h"
16#include "include/core/SkRect.h"
19#include "include/core/SkSize.h"
28#include "tools/ToolUtils.h"
30
31#include <initializer_list>
32
33using namespace skia_private;
34
35/**
36 * This GM tests reusing the same text blobs with distance fields rendering using various
37 * combinations of perspective and non-perspetive matrices, scissor clips, and different x,y params
38 * passed to the draw.
39 */
41public:
42 DFTextBlobPerspGM() { this->setBGColor(0xFFFFFFFF); }
43
44protected:
45 SkString getName() const override { return SkString("dftext_blob_persp"); }
46
47 SkISize getISize() override { return SkISize::Make(900, 350); }
48
49 void onOnceBeforeDraw() override {
50 for (int i = 0; i < 3; ++i) {
52 font.setSize(32);
53 font.setEdging(i == 0 ? SkFont::Edging::kAlias :
56 font.setSubpixel(true);
58 ToolUtils::add_to_text_blob(&builder, "SkiaText", font, 0, 0);
59 fBlobs.emplace_back(builder.make());
60 }
61 }
62
63 void onDraw(SkCanvas* inputCanvas) override {
64 // set up offscreen rendering with distance field text
65 auto ctx = inputCanvas->recordingContext();
66 SkISize size = this->getISize();
67 if (!inputCanvas->getBaseLayerSize().isEmpty()) {
68 size = inputCanvas->getBaseLayerSize();
69 }
71 inputCanvas->imageInfo().refColorSpace());
72 SkSurfaceProps inputProps;
73 inputCanvas->getProps(&inputProps);
75 inputProps.pixelGeometry());
77 SkCanvas* canvas = surface ? surface->getCanvas() : inputCanvas;
78 // init our new canvas with the old canvas's matrix
79 canvas->setMatrix(inputCanvas->getLocalToDeviceAs3x3());
80 SkScalar x = 0, y = 0;
81 SkScalar maxH = 0;
82 for (auto twm : {TranslateWithMatrix::kNo, TranslateWithMatrix::kYes}) {
83 for (auto pm : {PerspMode::kNone, PerspMode::kX, PerspMode::kY, PerspMode::kXY}) {
84 for (auto& blob : fBlobs) {
85 for (bool clip : {false, true}) {
86 SkAutoCanvasRestore acr(canvas, true);
87 SkScalar w = blob->bounds().width();
88 SkScalar h = blob->bounds().height();
89 if (clip) {
90 auto rect =
91 SkRect::MakeXYWH(x + 5, y + 5, w * 3.f / 4.f, h * 3.f / 4.f);
92 canvas->clipRect(rect, false);
93 }
94 this->drawBlob(canvas, blob.get(), SK_ColorBLACK, x, y + h, pm, twm);
95 x += w + 20.f;
96 maxH = std::max(h, maxH);
97 }
98 }
99 x = 0;
100 y += maxH + 20.f;
101 maxH = 0;
102 }
103 }
104 // render offscreen buffer
105 if (surface) {
106 SkAutoCanvasRestore acr(inputCanvas, true);
107 // since we prepended this matrix already, we blit using identity
108 inputCanvas->resetMatrix();
109 inputCanvas->drawImage(surface->makeImageSnapshot().get(), 0, 0);
110 }
111 }
112
113private:
114 enum class PerspMode { kNone, kX, kY, kXY };
115
116 enum class TranslateWithMatrix : bool { kNo, kYes };
117
118 void drawBlob(SkCanvas* canvas, SkTextBlob* blob, SkColor color, SkScalar x, SkScalar y,
119 PerspMode perspMode, TranslateWithMatrix translateWithMatrix) {
120 canvas->save();
121 SkMatrix persp = SkMatrix::I();
122 switch (perspMode) {
123 case PerspMode::kNone:
124 break;
125 case PerspMode::kX:
126 persp.setPerspX(0.005f);
127 break;
128 case PerspMode::kY:
129 persp.setPerspY(00.005f);
130 break;
131 case PerspMode::kXY:
132 persp.setPerspX(-0.001f);
133 persp.setPerspY(-0.0015f);
134 break;
135 }
136 persp = SkMatrix::Concat(persp, SkMatrix::Translate(-x, -y));
137 persp = SkMatrix::Concat(SkMatrix::Translate(x, y), persp);
138 canvas->concat(persp);
139 if (TranslateWithMatrix::kYes == translateWithMatrix) {
140 canvas->translate(x, y);
141 x = 0;
142 y = 0;
143 }
145 paint.setColor(color);
146 canvas->drawTextBlob(blob, x, y, paint);
147 canvas->restore();
148 }
149
151 using INHERITED = skiagm::GM;
152};
153
154DEF_GM(return new DFTextBlobPerspGM;)
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
uint32_t SkColor
Definition: SkColor.h:37
constexpr SkColor SK_ColorBLACK
Definition: SkColor.h:103
@ kYes
Do pre-clip the geometry before applying the (perspective) matrix.
@ kNo
Don't pre-clip the geometry before applying the (perspective) matrix.
static SkPath clip(const SkPath &path, const SkHalfPlane &plane)
Definition: SkPath.cpp:3892
void onOnceBeforeDraw() override
SkISize getISize() override
SkString getName() const override
void onDraw(SkCanvas *inputCanvas) override
bool getProps(SkSurfaceProps *props) const
Definition: SkCanvas.cpp:1214
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
Definition: SkCanvas.cpp:1361
void restore()
Definition: SkCanvas.cpp:461
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
SkMatrix getLocalToDeviceAs3x3() const
Definition: SkCanvas.h:2222
virtual GrRecordingContext * recordingContext() const
Definition: SkCanvas.cpp:1637
virtual SkISize getBaseLayerSize() const
Definition: SkCanvas.cpp:369
void resetMatrix()
Definition: SkCanvas.cpp:1355
int save()
Definition: SkCanvas.cpp:447
void setMatrix(const SkM44 &matrix)
Definition: SkCanvas.cpp:1349
void concat(const SkMatrix &matrix)
Definition: SkCanvas.cpp:1318
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
@ kAntiAlias
may have transparent pixels on glyph edges
@ kAlias
no transparent pixels on glyph edges
@ kSubpixelAntiAlias
glyph positioned in pixel using transparency
static SkMatrix Translate(SkScalar dx, SkScalar dy)
Definition: SkMatrix.h:91
SkMatrix & setPerspX(SkScalar v)
Definition: SkMatrix.h:537
static SkMatrix Concat(const SkMatrix &a, const SkMatrix &b)
Definition: SkMatrix.h:1775
SkMatrix & setPerspY(SkScalar v)
Definition: SkMatrix.h:544
static const SkMatrix & I()
Definition: SkMatrix.cpp:1544
@ kUseDeviceIndependentFonts_Flag
uint32_t flags() const
SkPixelGeometry pixelGeometry() const
T & emplace_back(Args &&... args)
Definition: SkTArray.h:248
Definition: gm.h:110
void setBGColor(SkColor)
Definition: gm.cpp:159
const Paint & paint
Definition: color_source.cc:38
DlColor color
VkSurfaceKHR surface
Definition: main.cc:49
float SkScalar
Definition: extension.cpp:12
#define DEF_GM(CODE)
Definition: gm.h:40
static float max(float r, float g, float b)
Definition: hsl.cpp:49
double y
double x
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
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)
SkFont DefaultPortableFont()
void add_to_text_blob(SkTextBlobBuilder *builder, const char *text, const SkFont &font, SkScalar x, SkScalar y)
Definition: ToolUtils.cpp:228
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.
SkScalar w
SkScalar h
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)
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659