Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
textblobcolortrans.cpp
Go to the documentation of this file.
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"
11#include "include/core/SkFont.h"
14#include "include/core/SkRect.h"
17#include "include/core/SkSize.h"
22#include "tools/ToolUtils.h"
24
25#include <string.h>
26
27namespace skiagm {
28class TextBlobColorTrans : public GM {
29public:
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
34
35protected:
36 void onOnceBeforeDraw() override {
37 SkTextBlobBuilder builder;
38
39 // make textblob
40 // Large text is used to trigger atlas eviction
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
61 SkString getName() const override { return SkString("textblobcolortrans"); }
62
63 SkISize getISize() override { return SkISize::Make(kWidth, kHeight); }
64
65 void onDraw(SkCanvas* canvas) override {
66
67 canvas->drawColor(SK_ColorGRAY);
68
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.
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
91private:
93
94 inline static constexpr int kWidth = 675;
95 inline static constexpr int kHeight = 1600;
96
97 using INHERITED = GM;
98};
99
100//////////////////////////////////////////////////////////////////////////////
101
102DEF_GM(return new TextBlobColorTrans;)
103} // namespace skiagm
int count
constexpr SkColor SK_ColorYELLOW
Definition SkColor.h:139
constexpr SkColor SK_ColorLTGRAY
Definition SkColor.h:118
uint32_t SkColor
Definition SkColor.h:37
constexpr SkColor SK_ColorCYAN
Definition SkColor.h:143
constexpr SkColor SK_ColorGRAY
Definition SkColor.h:113
constexpr SkColor SK_ColorWHITE
Definition SkColor.h:122
@ kUTF8
uses bytes to represent UTF-8 or ASCII
#define SkIntToScalar(x)
Definition SkScalar.h:57
#define SkScalarFloorToInt(x)
Definition SkScalar.h:35
void restore()
Definition SkCanvas.cpp:465
void translate(SkScalar dx, SkScalar dy)
void drawColor(SkColor color, SkBlendMode mode=SkBlendMode::kSrcOver)
Definition SkCanvas.h:1182
int save()
Definition SkCanvas.cpp:451
void drawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y, const SkPaint &paint)
@ kAlias
no transparent pixels on glyph edges
const SkRect & bounds() const
Definition SkTextBlob.h:53
void onDraw(SkCanvas *canvas) override
SkString getName() const override
const Paint & paint
float SkScalar
Definition extension.cpp:12
#define DEF_GM(CODE)
Definition gm.h:40
std::u16string text
double y
sk_sp< SkTypeface > DefaultPortableTypeface()
void add_to_text_blob(SkTextBlobBuilder *builder, const char *text, const SkFont &font, SkScalar x, SkScalar y)
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20