joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 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" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "include/core/SkTextBlob.h" |
| 20 | #include "include/core/SkTypeface.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 21 | #include "include/core/SkTypes.h" |
| 22 | #include "tools/ToolUtils.h" |
| 23 | |
| 24 | #include <string.h> |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 25 | |
| 26 | namespace skiagm { |
| 27 | class TextBlobColorTrans : public GM { |
| 28 | public: |
| 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 | |
| 34 | protected: |
| 35 | void onOnceBeforeDraw() override { |
| 36 | SkTextBlobBuilder builder; |
| 37 | |
| 38 | // make textblob |
| 39 | // Large text is used to trigger atlas eviction |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 40 | SkFont font(ToolUtils::create_portable_typeface(), 256); |
Mike Reed | 28bd882 | 2018-12-22 22:29:45 -0500 | [diff] [blame] | 41 | font.setEdging(SkFont::Edging::kAlias); |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 42 | const char* text = "AB"; |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 43 | |
| 44 | SkRect bounds; |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 45 | font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds); |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 46 | |
| 47 | SkScalar yOffset = bounds.height(); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 48 | ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset - 30); |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 49 | |
| 50 | // A8 |
Mike Reed | 28bd882 | 2018-12-22 22:29:45 -0500 | [diff] [blame] | 51 | font.setSize(28); |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 52 | text = "The quick brown fox jumps over the lazy dog."; |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 53 | font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 54 | ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset - 8); |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 55 | |
| 56 | // build |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 57 | fBlob = builder.make(); |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 58 | } |
| 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 Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 70 | canvas->drawColor(SK_ColorGRAY); |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 71 | |
| 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 Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 80 | SkColor colors[] = {SK_ColorCYAN, SK_ColorLTGRAY, SK_ColorYELLOW, SK_ColorWHITE}; |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 81 | |
| 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 | |
| 94 | private: |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 95 | sk_sp<SkTextBlob> fBlob; |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 96 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 97 | static constexpr int kWidth = 675; |
| 98 | static constexpr int kHeight = 1600; |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 99 | |
| 100 | typedef GM INHERITED; |
| 101 | }; |
| 102 | |
| 103 | ////////////////////////////////////////////////////////////////////////////// |
| 104 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 105 | DEF_GM(return new TextBlobColorTrans;) |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 106 | } |