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" 9 #include "include/core/SkCanvas.h" 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" 19 #include "include/core/SkTextBlob.h" 20 #include "include/core/SkTypeface.h" 21 #include "include/core/SkTypes.h" 22 #include "tools/ToolUtils.h" 23 24 #include <string.h> 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 TextBlobColorTrans()32 TextBlobColorTrans() { } 33 34 protected: onOnceBeforeDraw()35 void onOnceBeforeDraw() override { 36 SkTextBlobBuilder builder; 37 38 // make textblob 39 // Large text is used to trigger atlas eviction 40 SkFont font(ToolUtils::create_portable_typeface(), 256); 41 font.setEdging(SkFont::Edging::kAlias); 42 const char* text = "AB"; 43 44 SkRect bounds; 45 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds); 46 47 SkScalar yOffset = bounds.height(); 48 ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset - 30); 49 50 // A8 51 font.setSize(28); 52 text = "The quick brown fox jumps over the lazy dog."; 53 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds); 54 ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset - 8); 55 56 // build 57 fBlob = builder.make(); 58 } 59 onShortName()60 SkString onShortName() override { 61 return SkString("textblobcolortrans"); 62 } 63 onISize()64 SkISize onISize() override { 65 return SkISize::Make(kWidth, kHeight); 66 } 67 onDraw(SkCanvas * canvas)68 void onDraw(SkCanvas* canvas) override { 69 70 canvas->drawColor(SK_ColorGRAY); 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. 80 SkColor colors[] = {SK_ColorCYAN, SK_ColorLTGRAY, SK_ColorYELLOW, SK_ColorWHITE}; 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: 95 sk_sp<SkTextBlob> fBlob; 96 97 inline static constexpr int kWidth = 675; 98 inline static constexpr int kHeight = 1600; 99 100 using INHERITED = GM; 101 }; 102 103 ////////////////////////////////////////////////////////////////////////////// 104 105 DEF_GM(return new TextBlobColorTrans;) 106 } // namespace skiagm 107