xref: /aosp_15_r20/external/skia/gm/textblobcolortrans.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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/SkFont.h"
12 #include "include/core/SkFontTypes.h"
13 #include "include/core/SkPaint.h"
14 #include "include/core/SkRect.h"
15 #include "include/core/SkRefCnt.h"
16 #include "include/core/SkScalar.h"
17 #include "include/core/SkSize.h"
18 #include "include/core/SkString.h"
19 #include "include/core/SkTextBlob.h"
20 #include "include/core/SkTypeface.h"
21 #include "include/core/SkTypes.h"
22 #include "tools/ToolUtils.h"
23 #include "tools/fonts/FontToolUtils.h"
24 
25 #include <string.h>
26 
27 namespace skiagm {
28 class TextBlobColorTrans : public GM {
29 public:
30     // This gm tests that textblobs can be translated and have their colors regenerated
31     // correctly.  With smaller atlas sizes, it can also trigger regeneration of texture coords on
32     // the GPU backend
TextBlobColorTrans()33     TextBlobColorTrans() { }
34 
35 protected:
onOnceBeforeDraw()36     void onOnceBeforeDraw() override {
37         SkTextBlobBuilder builder;
38 
39         // make textblob
40         // Large text is used to trigger atlas eviction
41         SkFont font(ToolUtils::DefaultPortableTypeface(), 256);
42         font.setEdging(SkFont::Edging::kAlias);
43         const char* text = "AB";
44 
45         SkRect bounds;
46         font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
47 
48         SkScalar yOffset = bounds.height();
49         ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset - 30);
50 
51         // A8
52         font.setSize(28);
53         text = "The quick brown fox jumps over the lazy dog.";
54         font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
55         ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset - 8);
56 
57         // build
58         fBlob = builder.make();
59     }
60 
getName() const61     SkString getName() const override { return SkString("textblobcolortrans"); }
62 
getISize()63     SkISize getISize() override { return SkISize::Make(kWidth, kHeight); }
64 
onDraw(SkCanvas * canvas)65     void onDraw(SkCanvas* canvas) override {
66 
67         canvas->drawColor(SK_ColorGRAY);
68 
69         SkPaint paint;
70         canvas->translate(10, 40);
71 
72         SkRect bounds = fBlob->bounds();
73 
74         // Colors were chosen to map to pairs of canonical colors.  The GPU Backend will cache A8
75         // Texture Blobs based on the canonical color they map to.  Canonical colors are used to
76         // create masks.  For A8 there are 8 of them.
77         SkColor colors[] = {SK_ColorCYAN, SK_ColorLTGRAY, SK_ColorYELLOW, SK_ColorWHITE};
78 
79         size_t count = std::size(colors);
80         size_t colorIndex = 0;
81         for (int y = 0; y + SkScalarFloorToInt(bounds.height()) < kHeight;
82              y += SkScalarFloorToInt(bounds.height())) {
83             paint.setColor(colors[colorIndex++ % count]);
84             canvas->save();
85             canvas->translate(0, SkIntToScalar(y));
86             canvas->drawTextBlob(fBlob, 0, 0, paint);
87             canvas->restore();
88         }
89     }
90 
91 private:
92     sk_sp<SkTextBlob> fBlob;
93 
94     inline static constexpr int kWidth = 675;
95     inline static constexpr int kHeight = 1600;
96 
97     using INHERITED = GM;
98 };
99 
100 //////////////////////////////////////////////////////////////////////////////
101 
102 DEF_GM(return new TextBlobColorTrans;)
103 }  // namespace skiagm
104