blob: b41dd238bd5279079ab6b54d8350de23867eff51 [file] [log] [blame]
joshualitt9e36c1a2015-04-14 12:17:27 -07001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/core/SkTextBlob.h"
20#include "include/core/SkTypeface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040021#include "include/core/SkTypes.h"
22#include "tools/ToolUtils.h"
23
24#include <string.h>
joshualitt9e36c1a2015-04-14 12:17:27 -070025
26namespace skiagm {
27class TextBlobColorTrans : public GM {
28public:
29 // This gm tests that textblobs can be translated and have their colors regenerated
30 // correctly. With smaller atlas sizes, it can also trigger regeneration of texture coords on
31 // the GPU backend
32 TextBlobColorTrans() { }
33
34protected:
35 void onOnceBeforeDraw() override {
36 SkTextBlobBuilder builder;
37
38 // make textblob
39 // Large text is used to trigger atlas eviction
Mike Kleinea3f0142019-03-20 11:12:10 -050040 SkFont font(ToolUtils::create_portable_typeface(), 256);
Mike Reed28bd8822018-12-22 22:29:45 -050041 font.setEdging(SkFont::Edging::kAlias);
joshualitt9e36c1a2015-04-14 12:17:27 -070042 const char* text = "AB";
joshualitt9e36c1a2015-04-14 12:17:27 -070043
44 SkRect bounds;
Ben Wagner51e15a62019-05-07 15:38:46 -040045 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
joshualitt9e36c1a2015-04-14 12:17:27 -070046
47 SkScalar yOffset = bounds.height();
Mike Kleinea3f0142019-03-20 11:12:10 -050048 ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset - 30);
joshualitt9e36c1a2015-04-14 12:17:27 -070049
50 // A8
Mike Reed28bd8822018-12-22 22:29:45 -050051 font.setSize(28);
joshualitt9e36c1a2015-04-14 12:17:27 -070052 text = "The quick brown fox jumps over the lazy dog.";
Ben Wagner51e15a62019-05-07 15:38:46 -040053 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
Mike Kleinea3f0142019-03-20 11:12:10 -050054 ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset - 8);
joshualitt9e36c1a2015-04-14 12:17:27 -070055
56 // build
fmalita37283c22016-09-13 10:00:23 -070057 fBlob = builder.make();
joshualitt9e36c1a2015-04-14 12:17:27 -070058 }
59
60 SkString onShortName() override {
61 return SkString("textblobcolortrans");
62 }
63
64 SkISize onISize() override {
65 return SkISize::Make(kWidth, kHeight);
66 }
67
68 void onDraw(SkCanvas* canvas) override {
69
Mike Kleind46dce32018-08-16 10:17:03 -040070 canvas->drawColor(SK_ColorGRAY);
joshualitt9e36c1a2015-04-14 12:17:27 -070071
72 SkPaint paint;
73 canvas->translate(10, 40);
74
75 SkRect bounds = fBlob->bounds();
76
77 // Colors were chosen to map to pairs of canonical colors. The GPU Backend will cache A8
78 // Texture Blobs based on the canonical color they map to. Canonical colors are used to
79 // create masks. For A8 there are 8 of them.
Mike Kleind46dce32018-08-16 10:17:03 -040080 SkColor colors[] = {SK_ColorCYAN, SK_ColorLTGRAY, SK_ColorYELLOW, SK_ColorWHITE};
joshualitt9e36c1a2015-04-14 12:17:27 -070081
82 size_t count = SK_ARRAY_COUNT(colors);
83 size_t colorIndex = 0;
84 for (int y = 0; y + SkScalarFloorToInt(bounds.height()) < kHeight;
85 y += SkScalarFloorToInt(bounds.height())) {
86 paint.setColor(colors[colorIndex++ % count]);
87 canvas->save();
88 canvas->translate(0, SkIntToScalar(y));
89 canvas->drawTextBlob(fBlob, 0, 0, paint);
90 canvas->restore();
91 }
92 }
93
94private:
fmalita37283c22016-09-13 10:00:23 -070095 sk_sp<SkTextBlob> fBlob;
joshualitt9e36c1a2015-04-14 12:17:27 -070096
mtkleindbfd7ab2016-09-01 11:24:54 -070097 static constexpr int kWidth = 675;
98 static constexpr int kHeight = 1600;
joshualitt9e36c1a2015-04-14 12:17:27 -070099
100 typedef GM INHERITED;
101};
102
103//////////////////////////////////////////////////////////////////////////////
104
halcanary385fe4d2015-08-26 13:07:48 -0700105DEF_GM(return new TextBlobColorTrans;)
joshualitt9e36c1a2015-04-14 12:17:27 -0700106}