• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015 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/SkFontStyle.h"
14 #include "include/core/SkFontTypes.h"
15 #include "include/core/SkImageInfo.h"
16 #include "include/core/SkPaint.h"
17 #include "include/core/SkRect.h"
18 #include "include/core/SkRefCnt.h"
19 #include "include/core/SkScalar.h"
20 #include "include/core/SkSize.h"
21 #include "include/core/SkString.h"
22 #include "include/core/SkSurface.h"
23 #include "include/core/SkSurfaceProps.h"
24 #include "include/core/SkTextBlob.h"
25 #include "include/core/SkTypeface.h"
26 #include "include/gpu/GrDirectContext.h"
27 #include "include/gpu/GrRecordingContext.h"
28 #include "tools/ToolUtils.h"
29 #include "tools/fonts/RandomScalerContext.h"
30 
31 #include <string.h>
32 #include <utility>
33 
34 class GrSurfaceDrawContext;
35 
36 namespace skiagm {
37 class TextBlobRandomFont : public GpuGM {
38 public:
39     // This gm tests that textblobs can be translated and scaled with a font that returns random
40     // but deterministic masks
TextBlobRandomFont()41     TextBlobRandomFont() { }
42 
43 protected:
onOnceBeforeDraw()44     void onOnceBeforeDraw() override {
45         SkTextBlobBuilder builder;
46 
47         const char* text = "The quick brown fox jumps over the lazy dog.";
48 
49         SkPaint paint;
50         paint.setAntiAlias(true);
51         paint.setColor(SK_ColorMAGENTA);
52 
53         // make textbloben
54         SkFont font;
55         font.setSize(32);
56         font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
57 
58         // Setup our random scaler context
59         auto typeface = ToolUtils::create_portable_typeface("sans-serif", SkFontStyle::Bold());
60         if (!typeface) {
61             typeface = SkTypeface::MakeDefault();
62         }
63         font.setTypeface(sk_make_sp<SkRandomTypeface>(std::move(typeface), paint, false));
64 
65         SkScalar y = 0;
66         SkRect bounds;
67         font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
68         y -= bounds.fTop;
69         ToolUtils::add_to_text_blob(&builder, text, font, 0, y);
70         y += bounds.fBottom;
71 
72         // A8
73         const char* bigtext1 = "The quick brown fox";
74         const char* bigtext2 = "jumps over the lazy dog.";
75         font.setSize(160);
76         font.setSubpixel(false);
77         font.setEdging(SkFont::Edging::kAntiAlias);
78         font.measureText(bigtext1, strlen(bigtext1), SkTextEncoding::kUTF8, &bounds);
79         y -= bounds.fTop;
80         ToolUtils::add_to_text_blob(&builder, bigtext1, font, 0, y);
81         y += bounds.fBottom;
82 
83         font.measureText(bigtext2, strlen(bigtext2), SkTextEncoding::kUTF8, &bounds);
84         y -= bounds.fTop;
85         ToolUtils::add_to_text_blob(&builder, bigtext2, font, 0, y);
86         y += bounds.fBottom;
87 
88         // color emoji
89         if (sk_sp<SkTypeface> origEmoji = ToolUtils::emoji_typeface()) {
90             font.setTypeface(sk_make_sp<SkRandomTypeface>(origEmoji, paint, false));
91             const char* emojiText = ToolUtils::emoji_sample_text();
92             font.measureText(emojiText, strlen(emojiText), SkTextEncoding::kUTF8, &bounds);
93             y -= bounds.fTop;
94             ToolUtils::add_to_text_blob(&builder, emojiText, font, 0, y);
95             y += bounds.fBottom;
96         }
97 
98         // build
99         fBlob = builder.make();
100     }
101 
onShortName()102     SkString onShortName() override {
103         return SkString("textblobrandomfont");
104     }
105 
onISize()106     SkISize onISize() override {
107         return SkISize::Make(kWidth, kHeight);
108     }
109 
onDraw(GrRecordingContext * context,GrSurfaceDrawContext *,SkCanvas * canvas,SkString * errorMsg)110     DrawResult onDraw(GrRecordingContext* context,
111                       GrSurfaceDrawContext*, SkCanvas* canvas,
112                       SkString* errorMsg) override {
113         // This GM exists to test a specific feature of the GPU backend.
114         // This GM uses ToolUtils::makeSurface which doesn't work well with vias.
115         // This GM uses SkRandomTypeface which doesn't work well with serialization.
116         canvas->drawColor(SK_ColorWHITE);
117 
118         SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, canvas->imageInfo().colorType(),
119                                              kPremul_SkAlphaType,
120                                              canvas->imageInfo().refColorSpace());
121         SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
122         auto           surface(ToolUtils::makeSurface(canvas, info, &props));
123         if (!surface) {
124             *errorMsg = "This test requires a surface";
125             return DrawResult::kFail;
126         }
127 
128         SkPaint paint;
129         paint.setAntiAlias(true);
130 
131         SkCanvas* surfaceCanvas = surface->getCanvas();
132 
133         SkScalar stride = SkScalarCeilToScalar(fBlob->bounds().height());
134         SkScalar yOffset = 5;
135 
136         canvas->save();
137         // Originally we would alternate between rotating and not to force blob regeneration,
138         // but that code seems to have rotted. Keeping the rotate to match the old GM as
139         // much as possible, and it seems like a reasonable stress test for transformed
140         // color emoji.
141         canvas->rotate(-0.05f);
142         canvas->drawTextBlob(fBlob, 10, yOffset, paint);
143         yOffset += stride;
144         canvas->restore();
145 
146         // Rotate in the surface canvas, not the final canvas, to avoid aliasing
147         surfaceCanvas->rotate(-0.05f);
148         surfaceCanvas->drawTextBlob(fBlob, 10, yOffset, paint);
149         surface->draw(canvas, 0, 0);
150         yOffset += stride;
151 
152         if (auto direct = context->asDirectContext()) {
153             // free gpu resources and verify
154             direct->freeGpuResources();
155         }
156 
157         canvas->rotate(-0.05f);
158         canvas->drawTextBlob(fBlob, 10, yOffset, paint);
159         yOffset += stride;
160         return DrawResult::kOk;
161     }
162 
163 private:
164     sk_sp<SkTextBlob> fBlob;
165 
166     static constexpr int kWidth = 2000;
167     static constexpr int kHeight = 1600;
168 
169     using INHERITED = GM;
170 };
171 
172 //////////////////////////////////////////////////////////////////////////////
173 
174 DEF_GM(return new TextBlobRandomFont;)
175 }  // namespace skiagm
176