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 | |
| 8 | #include "gm.h" |
| 9 | |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkTextBlob.h" |
| 12 | |
| 13 | namespace skiagm { |
| 14 | class TextBlobBlockReordering : public GM { |
| 15 | public: |
| 16 | // This gm tests that textblobs translate properly when their draw order is different from their |
| 17 | // flush order |
| 18 | TextBlobBlockReordering() { } |
| 19 | |
| 20 | protected: |
| 21 | void onOnceBeforeDraw() override { |
| 22 | SkTextBlobBuilder builder; |
| 23 | |
| 24 | // make textblob |
| 25 | // Large text is used to trigger atlas eviction |
| 26 | SkPaint paint; |
| 27 | paint.setTextSize(56); |
| 28 | const char* text = "AB"; |
| 29 | sk_tool_utils::set_portable_typeface(&paint); |
| 30 | |
| 31 | SkRect bounds; |
| 32 | paint.measureText(text, strlen(text), &bounds); |
| 33 | |
| 34 | SkScalar yOffset = bounds.height(); |
| 35 | sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, yOffset - 30); |
| 36 | |
| 37 | // build |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 38 | fBlob = builder.make(); |
joshualitt | cbbc9df | 2016-02-24 05:49:55 -0800 | [diff] [blame] | 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 |
| 50 | // xfer mode so it doens't get batched with the first and third. |
| 51 | // ultimately thye iwll be flushed in the order first, third, and then second |
| 52 | void onDraw(SkCanvas* canvas) override { |
| 53 | canvas->drawColor(sk_tool_utils::color_to_565(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()); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 61 | |
joshualitt | cbbc9df | 2016-02-24 05:49:55 -0800 | [diff] [blame] | 62 | canvas->drawTextBlob(fBlob, 0, 0, paint); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 63 | |
joshualitt | cbbc9df | 2016-02-24 05:49:55 -0800 | [diff] [blame] | 64 | canvas->translate(SkIntToScalar(xDelta), SkIntToScalar(yDelta)); |
| 65 | |
| 66 | // draw a rect where the text should be, and then twiddle the xfermode |
| 67 | // so we don't batch |
| 68 | SkPaint redPaint; |
| 69 | redPaint.setColor(SK_ColorRED); |
| 70 | canvas->drawRect(bounds, redPaint); |
| 71 | SkPaint srcInPaint(paint); |
Mike Reed | 2cbcd12 | 2016-10-03 21:34:16 +0000 | [diff] [blame] | 72 | srcInPaint.setXfermodeMode(SkXfermode::kSrcIn_Mode); |
joshualitt | cbbc9df | 2016-02-24 05:49:55 -0800 | [diff] [blame] | 73 | canvas->drawTextBlob(fBlob, 0, 0, srcInPaint); |
| 74 | |
| 75 | canvas->translate(SkIntToScalar(xDelta), SkIntToScalar(yDelta)); |
| 76 | canvas->drawTextBlob(fBlob, 0, 0, paint); |
| 77 | } |
| 78 | |
| 79 | private: |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 80 | sk_sp<SkTextBlob> fBlob; |
joshualitt | cbbc9df | 2016-02-24 05:49:55 -0800 | [diff] [blame] | 81 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 82 | static constexpr int kWidth = 275; |
| 83 | static constexpr int kHeight = 200; |
joshualitt | cbbc9df | 2016-02-24 05:49:55 -0800 | [diff] [blame] | 84 | |
| 85 | typedef GM INHERITED; |
| 86 | }; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 87 | |
joshualitt | cbbc9df | 2016-02-24 05:49:55 -0800 | [diff] [blame] | 88 | ////////////////////////////////////////////////////////////////////////////// |
| 89 | |
| 90 | DEF_GM(return new TextBlobBlockReordering;) |
| 91 | } |