Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / skia / gm / fontregen.cpp
1 /*
2  * Copyright 2018 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 // GM to stress TextBlob regeneration and the GPU font cache
9 // It's not necessary to run this with CPU configs
10 //
11 // The point here is to draw a set of text that will fit in one Plot, and then some large
12 // text. After a flush we draw the first set of text again with a slightly different color,
13 // and then enough new large text to spill the entire atlas. What *should* happen is that
14 // the Plot with the first set of text will not get overwritten by the new large text.
15
16 #include "gm/gm.h"
17 #include "include/core/SkCanvas.h"
18 #include "include/core/SkColor.h"
19 #include "include/core/SkFont.h"
20 #include "include/core/SkFontMgr.h"
21 #include "include/core/SkFontStyle.h"
22 #include "include/core/SkFontTypes.h"
23 #include "include/core/SkPaint.h"
24 #include "include/core/SkRefCnt.h"
25 #include "include/core/SkScalar.h"
26 #include "include/core/SkSize.h"
27 #include "include/core/SkString.h"
28 #include "include/core/SkTextBlob.h"
29 #include "include/core/SkTypeface.h"
30 #include "include/core/SkTypes.h"
31 #include "include/gpu/GrContextOptions.h"
32 #include "include/gpu/GrDirectContext.h"
33 #include "include/gpu/GrRecordingContext.h"
34 #include "include/private/SkTemplates.h"
35 #include "include/private/gpu/ganesh/GrTypesPriv.h"
36 #include "src/gpu/ganesh/GrDirectContextPriv.h"
37 #include "tools/ToolUtils.h"
38
39 using MaskFormat = skgpu::MaskFormat;
40
41 static sk_sp<SkTextBlob> make_blob(const SkString& text, const SkFont& font) {
42     size_t len = text.size();
43     SkAutoTArray<SkScalar>  pos(len);
44     SkAutoTArray<SkGlyphID> glyphs(len);
45
46     font.textToGlyphs(text.c_str(), len, SkTextEncoding::kUTF8, glyphs.get(), len);
47     font.getXPos(glyphs.get(), len, pos.get());
48     return SkTextBlob::MakeFromPosTextH(text.c_str(), len, pos.get(), 0, font);
49 }
50
51 class FontRegenGM : public skiagm::GM {
52
53     void modifyGrContextOptions(GrContextOptions* options) override {
54         options->fGlyphCacheTextureMaximumBytes = 0;
55         options->fAllowMultipleGlyphCacheTextures = GrContextOptions::Enable::kNo;
56     }
57
58     SkString onShortName() override { return SkString("fontregen"); }
59
60     SkISize onISize() override { return {kSize, kSize}; }
61
62     void onOnceBeforeDraw() override {
63         this->setBGColor(SK_ColorLTGRAY);
64
65         auto tf = ToolUtils::create_portable_typeface("sans-serif", SkFontStyle::Normal());
66
67         static const SkString kTexts[] = {
68             SkString("abcdefghijklmnopqrstuvwxyz"),
69             SkString("ABCDEFGHI"),
70             SkString("NOPQRSTUV")
71         };
72
73         SkFont font;
74         font.setEdging(SkFont::Edging::kAntiAlias);
75         font.setSubpixel(false);
76         font.setSize(80);
77         font.setTypeface(tf);
78
79         fBlobs[0] = make_blob(kTexts[0], font);
80         font.setSize(162);
81         fBlobs[1] = make_blob(kTexts[1], font);
82         fBlobs[2] = make_blob(kTexts[2], font);
83     }
84
85     DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
86         auto dContext = GrAsDirectContext(canvas->recordingContext());
87         if (!dContext) {
88             *errorMsg = "GPU-specific";
89             return DrawResult::kSkip;
90         }
91
92         SkPaint paint;
93         paint.setColor(SK_ColorBLACK);
94         canvas->drawTextBlob(fBlobs[0], 10, 80, paint);
95         canvas->drawTextBlob(fBlobs[1], 10, 225, paint);
96         dContext->flushAndSubmit();
97
98         paint.setColor(0xFF010101);
99         canvas->drawTextBlob(fBlobs[0], 10, 305, paint);
100         canvas->drawTextBlob(fBlobs[2], 10, 465, paint);
101
102         //  Debugging tool for GPU.
103         static const bool kShowAtlas = false;
104         if (kShowAtlas) {
105             auto img = dContext->priv().testingOnly_getFontAtlasImage(MaskFormat::kA8);
106             canvas->drawImage(img, 200, 0);
107         }
108
109         return DrawResult::kOk;
110     }
111
112 private:
113     inline static constexpr int kSize = 512;
114
115     sk_sp<SkTextBlob> fBlobs[3];
116     using INHERITED = GM;
117 };
118
119 //////////////////////////////////////////////////////////////////////////////
120
121 DEF_GM(return new FontRegenGM())
122
123 ///////////////////////////////////////////////////////////////////////////////
124
125 class BadAppleGM : public skiagm::GM {
126
127     SkString onShortName() override { return SkString("badapple"); }
128
129     SkISize onISize() override { return {kSize, kSize}; }
130
131     void onOnceBeforeDraw() override {
132         this->setBGColor(SK_ColorWHITE);
133         auto fm = SkFontMgr::RefDefault();
134
135         static const SkString kTexts[] = {
136                 SkString("Meet"),
137                 SkString("iPad Pro"),
138         };
139
140         SkFont font;
141         font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
142         font.setSubpixel(true);
143         font.setSize(256);
144
145         fBlobs[0] = make_blob(kTexts[0], font);
146         fBlobs[1] = make_blob(kTexts[1], font);
147     }
148
149     void onDraw(SkCanvas* canvas) override {
150         SkPaint paint;
151         paint.setColor(0xFF111111);
152         canvas->drawTextBlob(fBlobs[0], 10, 260, paint);
153         canvas->drawTextBlob(fBlobs[1], 10, 500, paint);
154     }
155
156 private:
157     inline static constexpr int kSize = 512;
158
159     sk_sp<SkTextBlob> fBlobs[3];
160     using INHERITED = GM;
161 };
162
163 //////////////////////////////////////////////////////////////////////////////
164
165 DEF_GM(return new BadAppleGM())