Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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);
57 SkTextBlobBuilder builder;
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 }
70 SkImageInfo info = SkImageInfo::MakeN32(size.width(), size.height(), kPremul_SkAlphaType,
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 canvas->save();
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 canvas->restore();
98 }
99 }
100 x = 0;
101 y += maxH + 20.f;
102 maxH = 0;
103 }
104 }
105 // render offscreen buffer
106 if (surface) {
107 SkAutoCanvasRestore acr(inputCanvas, true);
108 // since we prepended this matrix already, we blit using identity
109 inputCanvas->resetMatrix();
110 inputCanvas->drawImage(surface->makeImageSnapshot().get(), 0, 0);
111 }
112 }
113
114private:
115 enum class PerspMode { kNone, kX, kY, kXY };
116
117 enum class TranslateWithMatrix : bool { kNo, kYes };
118
119 void drawBlob(SkCanvas* canvas, SkTextBlob* blob, SkColor color, SkScalar x, SkScalar y,
120 PerspMode perspMode, TranslateWithMatrix translateWithMatrix) {
121 canvas->save();
122 SkMatrix persp = SkMatrix::I();
123 switch (perspMode) {
124 case PerspMode::kNone:
125 break;
126 case PerspMode::kX:
127 persp.setPerspX(0.005f);
128 break;
129 case PerspMode::kY:
130 persp.setPerspY(00.005f);
131 break;
132 case PerspMode::kXY:
133 persp.setPerspX(-0.001f);
134 persp.setPerspY(-0.0015f);
135 break;
136 }
137 persp = SkMatrix::Concat(persp, SkMatrix::Translate(-x, -y));
138 persp = SkMatrix::Concat(SkMatrix::Translate(x, y), persp);
139 canvas->concat(persp);
140 if (TranslateWithMatrix::kYes == translateWithMatrix) {
141 canvas->translate(x, y);
142 x = 0;
143 y = 0;
144 }
146 paint.setColor(color);
147 canvas->drawTextBlob(blob, x, y, paint);
148 canvas->restore();
149 }
150
152 using INHERITED = skiagm::GM;
153};
154
155DEF_GM(return new DFTextBlobPerspGM;)
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
SkColor4f color
@ 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:3824
void onOnceBeforeDraw() override
SkISize getISize() override
SkString getName() const override
void onDraw(SkCanvas *inputCanvas) override
bool getProps(SkSurfaceProps *props) const
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
void restore()
Definition SkCanvas.cpp:465
void translate(SkScalar dx, SkScalar dy)
SkMatrix getLocalToDeviceAs3x3() const
Definition SkCanvas.h:2222
virtual GrRecordingContext * recordingContext() const
virtual SkISize getBaseLayerSize() const
Definition SkCanvas.cpp:373
void resetMatrix()
int save()
Definition SkCanvas.cpp:451
void setMatrix(const SkM44 &matrix)
void concat(const SkMatrix &matrix)
SkImageInfo imageInfo() const
void drawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y, const SkPaint &paint)
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition SkCanvas.h:1528
@ 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()
@ kUseDeviceIndependentFonts_Flag
uint32_t flags() const
SkPixelGeometry pixelGeometry() const
T & emplace_back(Args &&... args)
Definition SkTArray.h:243
void setBGColor(SkColor)
Definition gm.cpp:159
const Paint & paint
VkSurfaceKHR surface
Definition main.cc:49
float SkScalar
Definition extension.cpp:12
#define DEF_GM(CODE)
Definition gm.h:40
double y
double x
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)
SkScalar w
SkScalar h
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