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 | |
| 8 | #include "gm.h" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 10 | |
| 11 | #include "Resources.h" |
| 12 | #include "SkCanvas.h" |
| 13 | #include "SkGradientShader.h" |
| 14 | #include "SkStream.h" |
| 15 | #include "SkTextBlob.h" |
| 16 | #include "SkTypeface.h" |
| 17 | |
| 18 | namespace skiagm { |
| 19 | class TextBlobColorTrans : public GM { |
| 20 | public: |
| 21 | // This gm tests that textblobs can be translated and have their colors regenerated |
| 22 | // correctly. With smaller atlas sizes, it can also trigger regeneration of texture coords on |
| 23 | // the GPU backend |
| 24 | TextBlobColorTrans() { } |
| 25 | |
| 26 | protected: |
| 27 | void onOnceBeforeDraw() override { |
| 28 | SkTextBlobBuilder builder; |
| 29 | |
| 30 | // make textblob |
| 31 | // Large text is used to trigger atlas eviction |
| 32 | SkPaint paint; |
| 33 | paint.setTextSize(256); |
| 34 | const char* text = "AB"; |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 35 | sk_tool_utils::set_portable_typeface(&paint); |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 36 | |
| 37 | SkRect bounds; |
| 38 | paint.measureText(text, strlen(text), &bounds); |
| 39 | |
| 40 | SkScalar yOffset = bounds.height(); |
| 41 | sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, yOffset - 30); |
| 42 | |
| 43 | // A8 |
| 44 | paint.setTextSize(28); |
| 45 | text = "The quick brown fox jumps over the lazy dog."; |
| 46 | paint.setSubpixelText(false); |
| 47 | paint.setLCDRenderText(false); |
| 48 | paint.measureText(text, strlen(text), &bounds); |
| 49 | sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, yOffset - 8); |
| 50 | |
| 51 | // build |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 52 | fBlob = builder.make(); |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | SkString onShortName() override { |
| 56 | return SkString("textblobcolortrans"); |
| 57 | } |
| 58 | |
| 59 | SkISize onISize() override { |
| 60 | return SkISize::Make(kWidth, kHeight); |
| 61 | } |
| 62 | |
| 63 | void onDraw(SkCanvas* canvas) override { |
| 64 | |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame^] | 65 | canvas->drawColor(SK_ColorGRAY); |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 66 | |
| 67 | SkPaint paint; |
| 68 | canvas->translate(10, 40); |
| 69 | |
| 70 | SkRect bounds = fBlob->bounds(); |
| 71 | |
| 72 | // Colors were chosen to map to pairs of canonical colors. The GPU Backend will cache A8 |
| 73 | // Texture Blobs based on the canonical color they map to. Canonical colors are used to |
| 74 | // create masks. For A8 there are 8 of them. |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame^] | 75 | SkColor colors[] = {SK_ColorCYAN, SK_ColorLTGRAY, SK_ColorYELLOW, SK_ColorWHITE}; |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 76 | |
| 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 | |
| 89 | private: |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 90 | sk_sp<SkTextBlob> fBlob; |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 91 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 92 | static constexpr int kWidth = 675; |
| 93 | static constexpr int kHeight = 1600; |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 94 | |
| 95 | typedef GM INHERITED; |
| 96 | }; |
| 97 | |
| 98 | ////////////////////////////////////////////////////////////////////////////// |
| 99 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 100 | DEF_GM(return new TextBlobColorTrans;) |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 101 | } |