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