1 /* 2 * Copyright 2016 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 #include "sk_tool_utils.h" 10 11 #include "SkCanvas.h" 12 #include "SkTextBlob.h" 13 14 namespace skiagm { 15 class TextBlobBlockReordering : public GM { 16 public: 17 // This gm tests that textblobs translate properly when their draw order is different from their 18 // flush order 19 TextBlobBlockReordering() { } 20 21 protected: 22 void onOnceBeforeDraw() override { 23 SkTextBlobBuilder builder; 24 25 // make textblob 26 // Large text is used to trigger atlas eviction 27 SkFont font(sk_tool_utils::create_portable_typeface(), 56); 28 font.setEdging(SkFont::Edging::kAlias); 29 const char* text = "AB"; 30 31 SkRect bounds; 32 font.measureText(text, strlen(text), kUTF8_SkTextEncoding, &bounds); 33 34 SkScalar yOffset = bounds.height(); 35 sk_tool_utils::add_to_text_blob(&builder, text, font, 0, yOffset - 30); 36 37 // build 38 fBlob = builder.make(); 39 } 40 41 SkString onShortName() override { 42 return SkString("textblobblockreordering"); 43 } 44 45 SkISize onISize() override { 46 return SkISize::Make(kWidth, kHeight); 47 } 48 49 // This draws the same text blob 3 times. The second draw used a different xfer mode so its 50 // GrDrawOp doesn't get combined with the first and third. Ultimately, they will be flushed in 51 // the order first, third, and then second. 52 void onDraw(SkCanvas* canvas) override { 53 canvas->drawColor(SK_ColorGRAY); 54 55 SkPaint paint; 56 canvas->translate(10, 40); 57 58 SkRect bounds = fBlob->bounds(); 59 const int yDelta = SkScalarFloorToInt(bounds.height()) + 20; 60 const int xDelta = SkScalarFloorToInt(bounds.width()); 61 62 canvas->drawTextBlob(fBlob, 0, 0, paint); 63 64 canvas->translate(SkIntToScalar(xDelta), SkIntToScalar(yDelta)); 65 66 // Draw a rect where the text should be, and then twiddle the xfermode so we don't combine. 67 SkPaint redPaint; 68 redPaint.setColor(SK_ColorRED); 69 canvas->drawRect(bounds, redPaint); 70 SkPaint srcInPaint(paint); 71 srcInPaint.setBlendMode(SkBlendMode::kSrcIn); 72 canvas->drawTextBlob(fBlob, 0, 0, srcInPaint); 73 74 canvas->translate(SkIntToScalar(xDelta), SkIntToScalar(yDelta)); 75 canvas->drawTextBlob(fBlob, 0, 0, paint); 76 } 77 78 private: 79 sk_sp<SkTextBlob> fBlob; 80 81 static constexpr int kWidth = 275; 82 static constexpr int kHeight = 200; 83 84 typedef GM INHERITED; 85 }; 86 87 ////////////////////////////////////////////////////////////////////////////// 88 89 DEF_GM(return new TextBlobBlockReordering;) 90 } 91