blob: e9df4afd19a163a527fe3fa6d9a9612abd3209db [file] [log] [blame]
Brian Salomon5c6ac642017-12-19 11:09:32 -05001/*
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.h"
9#include "Resources.h"
10#include "SkCanvas.h"
11#include "SkSurface.h"
12#include "SkTextBlob.h"
13#include "SkTypeface.h"
14#include "sk_tool_utils.h"
15
16/**
17 * This GM tests reusing the same text blobs with distance fields rendering using various
18 * combinations of perspective and non-perspetive matrices, scissor clips, and different x,y params
19 * passed to the draw.
20 */
21class DFTextBlobPerspGM : public skiagm::GM {
22public:
23 DFTextBlobPerspGM() { this->setBGColor(0xFFFFFFFF); }
24
25protected:
26 SkString onShortName() override {
27 SkString name("dftext_blob_persp");
28 name.append(sk_tool_utils::platform_font_manager());
29 return name;
30 }
31
32 SkISize onISize() override { return SkISize::Make(900, 350); }
33
34 void onOnceBeforeDraw() override {
35 for (int i = 0; i < 3; ++i) {
36 SkPaint paint;
37 paint.setTextSize(32);
38 paint.setAntiAlias(i > 0);
39 paint.setLCDRenderText(i > 1);
40 paint.setSubpixelText(true);
41 SkTextBlobBuilder builder;
42 sk_tool_utils::add_to_text_blob(&builder, "SkiaText", paint, 0, 0);
43 fBlobs.emplace_back(builder.make());
44 }
45 }
46
47 virtual void onDraw(SkCanvas* inputCanvas) override {
48 // set up offscreen rendering with distance field text
Brian Salomon5c6ac642017-12-19 11:09:32 -050049 GrContext* ctx = inputCanvas->getGrContext();
50 SkISize size = this->onISize();
51 if (!inputCanvas->getBaseLayerSize().isEmpty()) {
52 size = inputCanvas->getBaseLayerSize();
53 }
54 SkImageInfo info = SkImageInfo::MakeN32(size.width(), size.height(), kPremul_SkAlphaType,
55 inputCanvas->imageInfo().refColorSpace());
56 SkSurfaceProps props(SkSurfaceProps::kUseDeviceIndependentFonts_Flag,
57 SkSurfaceProps::kLegacyFontHost_InitType);
58 auto surface = SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info, 0, &props);
59 SkCanvas* canvas = surface ? surface->getCanvas() : inputCanvas;
60 // init our new canvas with the old canvas's matrix
61 canvas->setMatrix(inputCanvas->getTotalMatrix());
Brian Salomon5c6ac642017-12-19 11:09:32 -050062 SkScalar x = 0, y = 0;
63 SkScalar maxH = 0;
64 for (auto twm : {TranslateWithMatrix::kNo, TranslateWithMatrix::kYes}) {
65 for (auto pm : {PerspMode::kNone, PerspMode::kX, PerspMode::kY, PerspMode::kXY}) {
66 for (auto& blob : fBlobs) {
67 for (bool clip : {false, true}) {
68 canvas->save();
69 SkScalar w = blob->bounds().width();
70 SkScalar h = blob->bounds().height();
71 if (clip) {
72 auto rect =
73 SkRect::MakeXYWH(x + 5, y + 5, w * 3.f / 4.f, h * 3.f / 4.f);
74 canvas->clipRect(rect, false);
75 }
76 this->drawBlob(canvas, blob.get(), SK_ColorBLACK, x, y + h, pm, twm);
77 x += w + 20.f;
78 maxH = SkTMax(h, maxH);
79 canvas->restore();
80 }
81 }
82 x = 0;
83 y += maxH + 20.f;
84 maxH = 0;
85 }
86 }
Brian Salomon5c6ac642017-12-19 11:09:32 -050087 // render offscreen buffer
88 if (surface) {
89 SkAutoCanvasRestore acr(inputCanvas, true);
90 // since we prepended this matrix already, we blit using identity
91 inputCanvas->resetMatrix();
92 inputCanvas->drawImage(surface->makeImageSnapshot().get(), 0, 0, nullptr);
93 }
Brian Salomon5c6ac642017-12-19 11:09:32 -050094 }
95
96private:
97 enum class PerspMode { kNone, kX, kY, kXY };
98
99 enum class TranslateWithMatrix : bool { kNo, kYes };
100
101 void drawBlob(SkCanvas* canvas, SkTextBlob* blob, SkColor color, SkScalar x, SkScalar y,
102 PerspMode perspMode, TranslateWithMatrix translateWithMatrix) {
103 canvas->save();
104 SkMatrix persp = SkMatrix::I();
105 switch (perspMode) {
106 case PerspMode::kNone:
107 break;
108 case PerspMode::kX:
109 persp.setPerspX(0.005f);
110 break;
111 case PerspMode::kY:
112 persp.setPerspY(00.005f);
113 break;
114 case PerspMode::kXY:
115 persp.setPerspX(-0.001f);
116 persp.setPerspY(-0.0015f);
117 break;
118 }
119 persp = SkMatrix::Concat(persp, SkMatrix::MakeTrans(-x, -y));
120 persp = SkMatrix::Concat(SkMatrix::MakeTrans(x, y), persp);
121 canvas->concat(persp);
122 if (TranslateWithMatrix::kYes == translateWithMatrix) {
123 canvas->translate(x, y);
124 x = 0;
125 y = 0;
126 }
127 SkPaint paint;
128 paint.setColor(color);
129 canvas->drawTextBlob(blob, x, y, paint);
130 canvas->restore();
131 }
132
133 SkTArray<sk_sp<SkTextBlob>, true> fBlobs;
134 typedef skiagm::GM INHERITED;
135};
136
137DEF_GM(return new DFTextBlobPerspGM;)