Debian Codegraph @

gm/dftext_blob_persp.cpp

raw
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"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkColorSpace.h"
12 #include "include/core/SkFont.h"
13 #include "include/core/SkImageInfo.h"
14 #include "include/core/SkMatrix.h"
15 #include "include/core/SkPaint.h"
16 #include "include/core/SkRect.h"
17 #include "include/core/SkRefCnt.h"
18 #include "include/core/SkScalar.h"
19 #include "include/core/SkSize.h"
20 #include "include/core/SkString.h"
21 #include "include/core/SkSurface.h"
22 #include "include/core/SkSurfaceProps.h"
23 #include "include/core/SkTextBlob.h"
24 #include "include/core/SkTypes.h"
25 #include "include/gpu/GpuTypes.h"
26 #include "include/private/base/SkTArray.h"
27 #include "tools/ToolUtils.h"
28 #include "tools/fonts/FontToolUtils.h"
29
30 #if defined(SK_GANESH)
31 #include "include/gpu/ganesh/SkSurfaceGanesh.h"
32 #endif
33
34 #include <initializer_list>
35
36 using namespace skia_private;
37
38 /**
39 * This GM tests reusing the same text blobs with distance fields rendering using various
40 * combinations of perspective and non-perspetive matrices, scissor clips, and different x,y params
41 * passed to the draw.
42 */
43 class DFTextBlobPerspGM : public skiagm::GM {
44 public:
45 DFTextBlobPerspGM() { this->setBGColor(0xFFFFFFFF); }
46
47 protected:
48 SkString getName() const override { return SkString("dftext_blob_persp"); }
49
50 SkISize getISize() override { return SkISize::Make(900, 350); }
51
52 void onOnceBeforeDraw() override {
53 for (int i = 0; i < 3; ++i) {
54 SkFont font = ToolUtils::DefaultPortableFont();
55 font.setSize(32);
56 font.setEdging(i == 0 ? SkFont::Edging::kAlias :
57 (i == 1 ? SkFont::Edging::kAntiAlias :
58 SkFont::Edging::kSubpixelAntiAlias));
59 font.setSubpixel(true);
60 SkTextBlobBuilder builder;
61 ToolUtils::add_to_text_blob(&builder, "SkiaText", font, 0, 0);
62 fBlobs.emplace_back(builder.make());
63 }
64 }
65
66 void onDraw(SkCanvas* inputCanvas) override {
67 // set up offscreen rendering with distance field text
68 SkISize size = this->getISize();
69 if (!inputCanvas->getBaseLayerSize().isEmpty()) {
70 size = inputCanvas->getBaseLayerSize();
71 }
72 SkImageInfo info = SkImageInfo::MakeN32(size.width(), size.height(), kPremul_SkAlphaType,
73 inputCanvas->imageInfo().refColorSpace());
74 SkSurfaceProps inputProps;
75 inputCanvas->getProps(&inputProps);
76 SkSurfaceProps props(SkSurfaceProps::kUseDeviceIndependentFonts_Flag | inputProps.flags(),
77 inputProps.pixelGeometry());
78 sk_sp<SkSurface> surface = nullptr;
79 #if defined(SK_GANESH)
80 surface = SkSurfaces::RenderTarget(
81 inputCanvas->recordingContext(), skgpu::Budgeted::kNo, info, 0, &props);
82 #endif
83 SkCanvas* canvas = surface ? surface->getCanvas() : inputCanvas;
84 // init our new canvas with the old canvas's matrix
85 canvas->setMatrix(inputCanvas->getLocalToDeviceAs3x3());
86 SkScalar x = 0, y = 0;
87 SkScalar maxH = 0;
88 for (auto twm : {TranslateWithMatrix::kNo, TranslateWithMatrix::kYes}) {
89 for (auto pm : {PerspMode::kNone, PerspMode::kX, PerspMode::kY, PerspMode::kXY}) {
90 for (auto& blob : fBlobs) {
91 for (bool clip : {false, true}) {
92 SkAutoCanvasRestore acr(canvas, true);
93 SkScalar w = blob->bounds().width();
94 SkScalar h = blob->bounds().height();
95 if (clip) {
96 auto rect =
97 SkRect::MakeXYWH(x + 5, y + 5, w * 3.f / 4.f, h * 3.f / 4.f);
98 canvas->clipRect(rect, false);
99 }
100 this->drawBlob(canvas, blob.get(), SK_ColorBLACK, x, y + h, pm, twm);
101 x += w + 20.f;
102 maxH = std::max(h, maxH);
103 }
104 }
105 x = 0;
106 y += maxH + 20.f;
107 maxH = 0;
108 }
109 }
110 // render offscreen buffer
111 if (surface) {
112 SkAutoCanvasRestore acr(inputCanvas, true);
113 // since we prepended this matrix already, we blit using identity
114 inputCanvas->resetMatrix();
115 inputCanvas->drawImage(surface->makeImageSnapshot().get(), 0, 0);
116 }
117 }
118
119 private:
120 enum class PerspMode { kNone, kX, kY, kXY };
121
122 enum class TranslateWithMatrix : bool { kNo, kYes };
123
124 void drawBlob(SkCanvas* canvas, SkTextBlob* blob, SkColor color, SkScalar x, SkScalar y,
125 PerspMode perspMode, TranslateWithMatrix translateWithMatrix) {
126 canvas->save();
127 SkMatrix persp = SkMatrix::I();
128 switch (perspMode) {
129 case PerspMode::kNone:
130 break;
131 case PerspMode::kX:
132 persp.setPerspX(0.005f);
133 break;
134 case PerspMode::kY:
135 persp.setPerspY(00.005f);
136 break;
137 case PerspMode::kXY:
138 persp.setPerspX(-0.001f);
139 persp.setPerspY(-0.0015f);
140 break;
141 }
142 persp = SkMatrix::Concat(persp, SkMatrix::Translate(-x, -y));
143 persp = SkMatrix::Concat(SkMatrix::Translate(x, y), persp);
144 canvas->concat(persp);
145 if (TranslateWithMatrix::kYes == translateWithMatrix) {
146 canvas->translate(x, y);
147 x = 0;
148 y = 0;
149 }
150 SkPaint paint;
151 paint.setColor(color);
152 canvas->drawTextBlob(blob, x, y, paint);
153 canvas->restore();
154 }
155
156 TArray<sk_sp<SkTextBlob>> fBlobs;
157 using INHERITED = skiagm::GM;
158 };
159
160 DEF_GM(return new DFTextBlobPerspGM;)