blob: 5c42218b8f89b5acbf6c9a660c283876630b430c [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
9#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "include/core/SkSurface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040022#include "include/core/SkSurfaceProps.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "include/core/SkTextBlob.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040024#include "include/core/SkTypes.h"
25#include "include/private/SkTArray.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "tools/ToolUtils.h"
Brian Salomon5c6ac642017-12-19 11:09:32 -050027
Ben Wagner7fde8e12019-05-01 17:28:53 -040028#include <initializer_list>
29
30class GrContext;
31
Brian Salomon5c6ac642017-12-19 11:09:32 -050032/**
33 * This GM tests reusing the same text blobs with distance fields rendering using various
34 * combinations of perspective and non-perspetive matrices, scissor clips, and different x,y params
35 * passed to the draw.
36 */
37class DFTextBlobPerspGM : public skiagm::GM {
38public:
39 DFTextBlobPerspGM() { this->setBGColor(0xFFFFFFFF); }
40
41protected:
42 SkString onShortName() override {
Mike Kleinbea1f942019-03-08 11:11:55 -060043 return SkString("dftext_blob_persp");
Brian Salomon5c6ac642017-12-19 11:09:32 -050044 }
45
46 SkISize onISize() override { return SkISize::Make(900, 350); }
47
48 void onOnceBeforeDraw() override {
49 for (int i = 0; i < 3; ++i) {
Mike Reedea8900e2018-12-22 17:37:30 -050050 SkFont font;
51 font.setSize(32);
52 font.setEdging(i == 0 ? SkFont::Edging::kAlias :
53 (i == 1 ? SkFont::Edging::kAntiAlias :
54 SkFont::Edging::kSubpixelAntiAlias));
55 font.setSubpixel(true);
Brian Salomon5c6ac642017-12-19 11:09:32 -050056 SkTextBlobBuilder builder;
Mike Kleinea3f0142019-03-20 11:12:10 -050057 ToolUtils::add_to_text_blob(&builder, "SkiaText", font, 0, 0);
Brian Salomon5c6ac642017-12-19 11:09:32 -050058 fBlobs.emplace_back(builder.make());
59 }
60 }
61
Mike Reedea8900e2018-12-22 17:37:30 -050062 void onDraw(SkCanvas* inputCanvas) override {
Brian Salomon5c6ac642017-12-19 11:09:32 -050063 // set up offscreen rendering with distance field text
Brian Salomon5c6ac642017-12-19 11:09:32 -050064 GrContext* ctx = inputCanvas->getGrContext();
65 SkISize size = this->onISize();
66 if (!inputCanvas->getBaseLayerSize().isEmpty()) {
67 size = inputCanvas->getBaseLayerSize();
68 }
69 SkImageInfo info = SkImageInfo::MakeN32(size.width(), size.height(), kPremul_SkAlphaType,
70 inputCanvas->imageInfo().refColorSpace());
71 SkSurfaceProps props(SkSurfaceProps::kUseDeviceIndependentFonts_Flag,
72 SkSurfaceProps::kLegacyFontHost_InitType);
73 auto surface = SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info, 0, &props);
74 SkCanvas* canvas = surface ? surface->getCanvas() : inputCanvas;
75 // init our new canvas with the old canvas's matrix
76 canvas->setMatrix(inputCanvas->getTotalMatrix());
Brian Salomon5c6ac642017-12-19 11:09:32 -050077 SkScalar x = 0, y = 0;
78 SkScalar maxH = 0;
79 for (auto twm : {TranslateWithMatrix::kNo, TranslateWithMatrix::kYes}) {
80 for (auto pm : {PerspMode::kNone, PerspMode::kX, PerspMode::kY, PerspMode::kXY}) {
81 for (auto& blob : fBlobs) {
82 for (bool clip : {false, true}) {
83 canvas->save();
84 SkScalar w = blob->bounds().width();
85 SkScalar h = blob->bounds().height();
86 if (clip) {
87 auto rect =
88 SkRect::MakeXYWH(x + 5, y + 5, w * 3.f / 4.f, h * 3.f / 4.f);
89 canvas->clipRect(rect, false);
90 }
91 this->drawBlob(canvas, blob.get(), SK_ColorBLACK, x, y + h, pm, twm);
92 x += w + 20.f;
93 maxH = SkTMax(h, maxH);
94 canvas->restore();
95 }
96 }
97 x = 0;
98 y += maxH + 20.f;
99 maxH = 0;
100 }
101 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500102 // render offscreen buffer
103 if (surface) {
104 SkAutoCanvasRestore acr(inputCanvas, true);
105 // since we prepended this matrix already, we blit using identity
106 inputCanvas->resetMatrix();
107 inputCanvas->drawImage(surface->makeImageSnapshot().get(), 0, 0, nullptr);
108 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500109 }
110
111private:
112 enum class PerspMode { kNone, kX, kY, kXY };
113
114 enum class TranslateWithMatrix : bool { kNo, kYes };
115
116 void drawBlob(SkCanvas* canvas, SkTextBlob* blob, SkColor color, SkScalar x, SkScalar y,
117 PerspMode perspMode, TranslateWithMatrix translateWithMatrix) {
118 canvas->save();
119 SkMatrix persp = SkMatrix::I();
120 switch (perspMode) {
121 case PerspMode::kNone:
122 break;
123 case PerspMode::kX:
124 persp.setPerspX(0.005f);
125 break;
126 case PerspMode::kY:
127 persp.setPerspY(00.005f);
128 break;
129 case PerspMode::kXY:
130 persp.setPerspX(-0.001f);
131 persp.setPerspY(-0.0015f);
132 break;
133 }
134 persp = SkMatrix::Concat(persp, SkMatrix::MakeTrans(-x, -y));
135 persp = SkMatrix::Concat(SkMatrix::MakeTrans(x, y), persp);
136 canvas->concat(persp);
137 if (TranslateWithMatrix::kYes == translateWithMatrix) {
138 canvas->translate(x, y);
139 x = 0;
140 y = 0;
141 }
142 SkPaint paint;
143 paint.setColor(color);
144 canvas->drawTextBlob(blob, x, y, paint);
145 canvas->restore();
146 }
147
Brian Salomon343553a2018-09-05 15:41:23 -0400148 SkTArray<sk_sp<SkTextBlob>> fBlobs;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500149 typedef skiagm::GM INHERITED;
150};
151
152DEF_GM(return new DFTextBlobPerspGM;)