joshualitt | cbbc9df | 2016-02-24 05:49:55 -0800 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #include "include/core/SkBlendMode.h" |
| 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkColor.h" |
| 12 | #include "include/core/SkFont.h" |
| 13 | #include "include/core/SkFontTypes.h" |
| 14 | #include "include/core/SkPaint.h" |
| 15 | #include "include/core/SkRect.h" |
| 16 | #include "include/core/SkRefCnt.h" |
| 17 | #include "include/core/SkScalar.h" |
| 18 | #include "include/core/SkSize.h" |
| 19 | #include "include/core/SkString.h" |
| 20 | #include "include/core/SkTextBlob.h" |
| 21 | #include "include/core/SkTypeface.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "tools/ToolUtils.h" |
joshualitt | cbbc9df | 2016-02-24 05:49:55 -0800 | [diff] [blame] | 23 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 24 | #include <string.h> |
joshualitt | cbbc9df | 2016-02-24 05:49:55 -0800 | [diff] [blame] | 25 | |
| 26 | namespace skiagm { |
| 27 | class TextBlobBlockReordering : public GM { |
| 28 | public: |
| 29 | // This gm tests that textblobs translate properly when their draw order is different from their |
| 30 | // flush order |
| 31 | TextBlobBlockReordering() { } |
| 32 | |
| 33 | protected: |
| 34 | void onOnceBeforeDraw() override { |
| 35 | SkTextBlobBuilder builder; |
| 36 | |
| 37 | // make textblob |
| 38 | // Large text is used to trigger atlas eviction |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 39 | SkFont font(ToolUtils::create_portable_typeface(), 56); |
Mike Reed | 28bd882 | 2018-12-22 22:29:45 -0500 | [diff] [blame] | 40 | font.setEdging(SkFont::Edging::kAlias); |
joshualitt | cbbc9df | 2016-02-24 05:49:55 -0800 | [diff] [blame] | 41 | const char* text = "AB"; |
joshualitt | cbbc9df | 2016-02-24 05:49:55 -0800 | [diff] [blame] | 42 | |
| 43 | SkRect bounds; |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 44 | font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds); |
joshualitt | cbbc9df | 2016-02-24 05:49:55 -0800 | [diff] [blame] | 45 | |
| 46 | SkScalar yOffset = bounds.height(); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 47 | ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset - 30); |
joshualitt | cbbc9df | 2016-02-24 05:49:55 -0800 | [diff] [blame] | 48 | |
| 49 | // build |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 50 | fBlob = builder.make(); |
joshualitt | cbbc9df | 2016-02-24 05:49:55 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | SkString onShortName() override { |
| 54 | return SkString("textblobblockreordering"); |
| 55 | } |
| 56 | |
| 57 | SkISize onISize() override { |
| 58 | return SkISize::Make(kWidth, kHeight); |
| 59 | } |
| 60 | |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 61 | // This draws the same text blob 3 times. The second draw used a different xfer mode so its |
| 62 | // GrDrawOp doesn't get combined with the first and third. Ultimately, they will be flushed in |
| 63 | // the order first, third, and then second. |
joshualitt | cbbc9df | 2016-02-24 05:49:55 -0800 | [diff] [blame] | 64 | void onDraw(SkCanvas* canvas) override { |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 65 | canvas->drawColor(SK_ColorGRAY); |
joshualitt | cbbc9df | 2016-02-24 05:49:55 -0800 | [diff] [blame] | 66 | |
| 67 | SkPaint paint; |
| 68 | canvas->translate(10, 40); |
| 69 | |
| 70 | SkRect bounds = fBlob->bounds(); |
| 71 | const int yDelta = SkScalarFloorToInt(bounds.height()) + 20; |
| 72 | const int xDelta = SkScalarFloorToInt(bounds.width()); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 73 | |
joshualitt | cbbc9df | 2016-02-24 05:49:55 -0800 | [diff] [blame] | 74 | canvas->drawTextBlob(fBlob, 0, 0, paint); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 75 | |
joshualitt | cbbc9df | 2016-02-24 05:49:55 -0800 | [diff] [blame] | 76 | canvas->translate(SkIntToScalar(xDelta), SkIntToScalar(yDelta)); |
| 77 | |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 78 | // Draw a rect where the text should be, and then twiddle the xfermode so we don't combine. |
joshualitt | cbbc9df | 2016-02-24 05:49:55 -0800 | [diff] [blame] | 79 | SkPaint redPaint; |
| 80 | redPaint.setColor(SK_ColorRED); |
| 81 | canvas->drawRect(bounds, redPaint); |
| 82 | SkPaint srcInPaint(paint); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 83 | srcInPaint.setBlendMode(SkBlendMode::kSrcIn); |
joshualitt | cbbc9df | 2016-02-24 05:49:55 -0800 | [diff] [blame] | 84 | canvas->drawTextBlob(fBlob, 0, 0, srcInPaint); |
| 85 | |
| 86 | canvas->translate(SkIntToScalar(xDelta), SkIntToScalar(yDelta)); |
| 87 | canvas->drawTextBlob(fBlob, 0, 0, paint); |
| 88 | } |
| 89 | |
| 90 | private: |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 91 | sk_sp<SkTextBlob> fBlob; |
joshualitt | cbbc9df | 2016-02-24 05:49:55 -0800 | [diff] [blame] | 92 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 93 | static constexpr int kWidth = 275; |
| 94 | static constexpr int kHeight = 200; |
joshualitt | cbbc9df | 2016-02-24 05:49:55 -0800 | [diff] [blame] | 95 | |
| 96 | typedef GM INHERITED; |
| 97 | }; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 98 | |
joshualitt | cbbc9df | 2016-02-24 05:49:55 -0800 | [diff] [blame] | 99 | ////////////////////////////////////////////////////////////////////////////// |
| 100 | |
| 101 | DEF_GM(return new TextBlobBlockReordering;) |
| 102 | } |