blob: 286dc77ef5ba08c3023f06f259d10fe5612ff0fc [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
8#include "gm.h"
9
10#include "Resources.h"
11#include "SkCanvas.h"
12#include "SkGradientShader.h"
13#include "SkStream.h"
14#include "SkTextBlob.h"
15#include "SkTypeface.h"
16
17namespace skiagm {
18class TextBlobColorTrans : public GM {
19public:
20 // This gm tests that textblobs can be translated and have their colors regenerated
21 // correctly. With smaller atlas sizes, it can also trigger regeneration of texture coords on
22 // the GPU backend
23 TextBlobColorTrans() { }
24
25protected:
26 void onOnceBeforeDraw() override {
27 SkTextBlobBuilder builder;
28
29 // make textblob
30 // Large text is used to trigger atlas eviction
31 SkPaint paint;
32 paint.setTextSize(256);
33 const char* text = "AB";
caryclark1818acb2015-07-24 12:09:25 -070034 sk_tool_utils::set_portable_typeface(&paint);
joshualitt9e36c1a2015-04-14 12:17:27 -070035
36 SkRect bounds;
37 paint.measureText(text, strlen(text), &bounds);
38
39 SkScalar yOffset = bounds.height();
40 sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, yOffset - 30);
41
42 // A8
43 paint.setTextSize(28);
44 text = "The quick brown fox jumps over the lazy dog.";
45 paint.setSubpixelText(false);
46 paint.setLCDRenderText(false);
47 paint.measureText(text, strlen(text), &bounds);
48 sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, yOffset - 8);
49
50 // build
fmalita37283c22016-09-13 10:00:23 -070051 fBlob = builder.make();
joshualitt9e36c1a2015-04-14 12:17:27 -070052 }
53
54 SkString onShortName() override {
55 return SkString("textblobcolortrans");
56 }
57
58 SkISize onISize() override {
59 return SkISize::Make(kWidth, kHeight);
60 }
61
62 void onDraw(SkCanvas* canvas) override {
63
caryclark85693c12015-07-20 10:48:01 -070064 canvas->drawColor(sk_tool_utils::color_to_565(SK_ColorGRAY));
joshualitt9e36c1a2015-04-14 12:17:27 -070065
66 SkPaint paint;
67 canvas->translate(10, 40);
68
69 SkRect bounds = fBlob->bounds();
70
71 // Colors were chosen to map to pairs of canonical colors. The GPU Backend will cache A8
72 // Texture Blobs based on the canonical color they map to. Canonical colors are used to
73 // create masks. For A8 there are 8 of them.
caryclark85693c12015-07-20 10:48:01 -070074 SkColor colors[] = {SK_ColorCYAN, sk_tool_utils::color_to_565(SK_ColorLTGRAY),
75 SK_ColorYELLOW, SK_ColorWHITE};
joshualitt9e36c1a2015-04-14 12:17:27 -070076
77 size_t count = SK_ARRAY_COUNT(colors);
78 size_t colorIndex = 0;
79 for (int y = 0; y + SkScalarFloorToInt(bounds.height()) < kHeight;
80 y += SkScalarFloorToInt(bounds.height())) {
81 paint.setColor(colors[colorIndex++ % count]);
82 canvas->save();
83 canvas->translate(0, SkIntToScalar(y));
84 canvas->drawTextBlob(fBlob, 0, 0, paint);
85 canvas->restore();
86 }
87 }
88
89private:
fmalita37283c22016-09-13 10:00:23 -070090 sk_sp<SkTextBlob> fBlob;
joshualitt9e36c1a2015-04-14 12:17:27 -070091
mtkleindbfd7ab2016-09-01 11:24:54 -070092 static constexpr int kWidth = 675;
93 static constexpr int kHeight = 1600;
joshualitt9e36c1a2015-04-14 12:17:27 -070094
95 typedef GM INHERITED;
96};
97
98//////////////////////////////////////////////////////////////////////////////
99
halcanary385fe4d2015-08-26 13:07:48 -0700100DEF_GM(return new TextBlobColorTrans;)
joshualitt9e36c1a2015-04-14 12:17:27 -0700101}