| 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" | 
| Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" | 
| joshualitt | cbbc9df | 2016-02-24 05:49:55 -0800 | [diff] [blame] | 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 |         SkPaint paint; | 
 | 28 |         paint.setTextSize(56); | 
 | 29 |         const char* text = "AB"; | 
 | 30 |         sk_tool_utils::set_portable_typeface(&paint); | 
 | 31 |  | 
 | 32 |         SkRect bounds; | 
 | 33 |         paint.measureText(text, strlen(text), &bounds); | 
 | 34 |  | 
 | 35 |         SkScalar yOffset = bounds.height(); | 
 | 36 |         sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, yOffset - 30); | 
 | 37 |  | 
 | 38 |         // build | 
| fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 39 |         fBlob = builder.make(); | 
| joshualitt | cbbc9df | 2016-02-24 05:49:55 -0800 | [diff] [blame] | 40 |     } | 
 | 41 |  | 
 | 42 |     SkString onShortName() override { | 
 | 43 |         return SkString("textblobblockreordering"); | 
 | 44 |     } | 
 | 45 |  | 
 | 46 |     SkISize onISize() override { | 
 | 47 |         return SkISize::Make(kWidth, kHeight); | 
 | 48 |     } | 
 | 49 |  | 
| Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 50 |     // This draws the same text blob 3 times.  The second draw used a different xfer mode so its | 
 | 51 |     // GrDrawOp doesn't get combined with the first and third. Ultimately, they will be flushed in | 
 | 52 |     // the order first, third, and then second. | 
| joshualitt | cbbc9df | 2016-02-24 05:49:55 -0800 | [diff] [blame] | 53 |     void onDraw(SkCanvas* canvas) override { | 
 | 54 |         canvas->drawColor(sk_tool_utils::color_to_565(SK_ColorGRAY)); | 
 | 55 |  | 
 | 56 |         SkPaint paint; | 
 | 57 |         canvas->translate(10, 40); | 
 | 58 |  | 
 | 59 |         SkRect bounds = fBlob->bounds(); | 
 | 60 |         const int yDelta = SkScalarFloorToInt(bounds.height()) + 20; | 
 | 61 |         const int xDelta = SkScalarFloorToInt(bounds.width()); | 
| halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 62 |  | 
| joshualitt | cbbc9df | 2016-02-24 05:49:55 -0800 | [diff] [blame] | 63 |         canvas->drawTextBlob(fBlob, 0, 0, paint); | 
| halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 64 |  | 
| joshualitt | cbbc9df | 2016-02-24 05:49:55 -0800 | [diff] [blame] | 65 |         canvas->translate(SkIntToScalar(xDelta), SkIntToScalar(yDelta)); | 
 | 66 |  | 
| Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 67 |         // 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] | 68 |         SkPaint redPaint; | 
 | 69 |         redPaint.setColor(SK_ColorRED); | 
 | 70 |         canvas->drawRect(bounds, redPaint); | 
 | 71 |         SkPaint srcInPaint(paint); | 
| reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 72 |         srcInPaint.setBlendMode(SkBlendMode::kSrcIn); | 
| 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 | } |