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/SkColorSpace.h" 12 #include "include/core/SkFont.h" 13 #include "include/core/SkFontStyle.h" 14 #include "include/core/SkFontTypes.h" 15 #include "include/core/SkImageInfo.h" 16 #include "include/core/SkPaint.h" 17 #include "include/core/SkRect.h" 18 #include "include/core/SkRefCnt.h" 19 #include "include/core/SkScalar.h" 20 #include "include/core/SkSize.h" 21 #include "include/core/SkString.h" 22 #include "include/core/SkSurface.h" 23 #include "include/core/SkSurfaceProps.h" 24 #include "include/core/SkTextBlob.h" 25 #include "include/core/SkTypeface.h" 26 #include "include/gpu/GrDirectContext.h" 27 #include "include/gpu/GrRecordingContext.h" 28 #include "tools/ToolUtils.h" 29 #include "tools/fonts/RandomScalerContext.h" 30 31 #include <string.h> 32 #include <utility> 33 34 namespace skiagm { 35 class TextBlobRandomFont : public GM { 36 public: 37 // This gm tests that textblobs can be translated and scaled with a font that returns random 38 // but deterministic masks TextBlobRandomFont()39 TextBlobRandomFont() { } 40 41 protected: onOnceBeforeDraw()42 void onOnceBeforeDraw() override { 43 SkTextBlobBuilder builder; 44 45 const char* text = "The quick brown fox jumps over the lazy dog."; 46 47 SkPaint paint; 48 paint.setAntiAlias(true); 49 paint.setColor(SK_ColorMAGENTA); 50 51 // make textbloben 52 SkFont font; 53 font.setSize(32); 54 font.setEdging(SkFont::Edging::kSubpixelAntiAlias); 55 56 // Setup our random scaler context 57 auto typeface = ToolUtils::create_portable_typeface("sans-serif", SkFontStyle::Bold()); 58 if (!typeface) { 59 typeface = SkTypeface::MakeDefault(); 60 } 61 font.setTypeface(sk_make_sp<SkRandomTypeface>(std::move(typeface), paint, false)); 62 63 SkScalar y = 0; 64 SkRect bounds; 65 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds); 66 y -= bounds.fTop; 67 ToolUtils::add_to_text_blob(&builder, text, font, 0, y); 68 y += bounds.fBottom; 69 70 // A8 71 const char* bigtext1 = "The quick brown fox"; 72 const char* bigtext2 = "jumps over the lazy dog."; 73 font.setSize(160); 74 font.setSubpixel(false); 75 font.setEdging(SkFont::Edging::kAntiAlias); 76 font.measureText(bigtext1, strlen(bigtext1), SkTextEncoding::kUTF8, &bounds); 77 y -= bounds.fTop; 78 ToolUtils::add_to_text_blob(&builder, bigtext1, font, 0, y); 79 y += bounds.fBottom; 80 81 font.measureText(bigtext2, strlen(bigtext2), SkTextEncoding::kUTF8, &bounds); 82 y -= bounds.fTop; 83 ToolUtils::add_to_text_blob(&builder, bigtext2, font, 0, y); 84 y += bounds.fBottom; 85 86 // color emoji 87 if (sk_sp<SkTypeface> origEmoji = ToolUtils::emoji_typeface()) { 88 font.setTypeface(sk_make_sp<SkRandomTypeface>(origEmoji, paint, false)); 89 const char* emojiText = ToolUtils::emoji_sample_text(); 90 font.measureText(emojiText, strlen(emojiText), SkTextEncoding::kUTF8, &bounds); 91 y -= bounds.fTop; 92 ToolUtils::add_to_text_blob(&builder, emojiText, font, 0, y); 93 y += bounds.fBottom; 94 } 95 96 // build 97 fBlob = builder.make(); 98 } 99 onShortName()100 SkString onShortName() override { 101 return SkString("textblobrandomfont"); 102 } 103 onISize()104 SkISize onISize() override { 105 return SkISize::Make(kWidth, kHeight); 106 } 107 onDraw(SkCanvas * canvas,SkString * errorMsg)108 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override { 109 if (!canvas->recordingContext()) { 110 *errorMsg = "Active context required to create SkSurface"; 111 return DrawResult::kSkip; 112 } 113 114 auto dContext = GrAsDirectContext(canvas->recordingContext()); 115 116 // This GM exists to test a specific feature of the GPU backend. 117 // This GM uses ToolUtils::makeSurface which doesn't work well with vias. 118 // This GM uses SkRandomTypeface which doesn't work well with serialization. 119 canvas->drawColor(SK_ColorWHITE); 120 121 SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, canvas->imageInfo().colorType(), 122 kPremul_SkAlphaType, 123 canvas->imageInfo().refColorSpace()); 124 SkSurfaceProps props(0, kUnknown_SkPixelGeometry); 125 auto surface(ToolUtils::makeSurface(canvas, info, &props)); 126 if (!surface) { 127 *errorMsg = "This test requires a surface"; 128 return DrawResult::kFail; 129 } 130 131 SkPaint paint; 132 paint.setAntiAlias(true); 133 134 SkCanvas* surfaceCanvas = surface->getCanvas(); 135 136 SkScalar stride = SkScalarCeilToScalar(fBlob->bounds().height()); 137 SkScalar yOffset = 5; 138 139 canvas->save(); 140 // Originally we would alternate between rotating and not to force blob regeneration, 141 // but that code seems to have rotted. Keeping the rotate to match the old GM as 142 // much as possible, and it seems like a reasonable stress test for transformed 143 // color emoji. 144 canvas->rotate(-0.05f); 145 canvas->drawTextBlob(fBlob, 10, yOffset, paint); 146 yOffset += stride; 147 canvas->restore(); 148 149 // Rotate in the surface canvas, not the final canvas, to avoid aliasing 150 surfaceCanvas->rotate(-0.05f); 151 surfaceCanvas->drawTextBlob(fBlob, 10, yOffset, paint); 152 surface->draw(canvas, 0, 0); 153 yOffset += stride; 154 155 if (dContext) { 156 // free gpu resources and verify 157 dContext->freeGpuResources(); 158 } 159 160 canvas->rotate(-0.05f); 161 canvas->drawTextBlob(fBlob, 10, yOffset, paint); 162 yOffset += stride; 163 return DrawResult::kOk; 164 } 165 166 private: 167 sk_sp<SkTextBlob> fBlob; 168 169 inline static constexpr int kWidth = 2000; 170 inline static constexpr int kHeight = 1600; 171 172 using INHERITED = GM; 173 }; 174 175 ////////////////////////////////////////////////////////////////////////////// 176 177 DEF_GM(return new TextBlobRandomFont;) 178 } // namespace skiagm 179