Mike Reed | 113fd34 | 2017-01-14 13:45:02 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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" |
Mike Reed | 113fd34 | 2017-01-14 13:45:02 -0500 | [diff] [blame] | 10 | |
Yuqian Li | d196cbe | 2017-02-23 10:28:33 -0500 | [diff] [blame] | 11 | |
| 12 | static const uint32_t SkCanvas_kDontClipToLayer_PrivateSaveLayerFlag = 1U << 31; |
| 13 | |
Robert Phillips | 42f3094 | 2017-01-17 10:48:52 -0500 | [diff] [blame] | 14 | // This GM tests out the deprecated Android-specific unclipped saveLayer "feature". |
| 15 | // In particular, it attempts to compare the performance of unclipped saveLayers with alternatives. |
| 16 | |
| 17 | static void save_layer_unclipped(SkCanvas* canvas, |
| 18 | SkScalar l, SkScalar t, SkScalar r, SkScalar b) { |
Mike Reed | 113fd34 | 2017-01-14 13:45:02 -0500 | [diff] [blame] | 19 | SkRect rect = SkRect::MakeLTRB(l, t, r, b); |
Yuqian Li | d196cbe | 2017-02-23 10:28:33 -0500 | [diff] [blame] | 20 | canvas->saveLayer({ &rect, nullptr, nullptr, SkCanvas_kDontClipToLayer_PrivateSaveLayerFlag }); |
Mike Reed | 113fd34 | 2017-01-14 13:45:02 -0500 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | static void do_draw(SkCanvas* canvas) { |
| 24 | SkPaint paint; |
| 25 | SkRandom rand; |
| 26 | |
Mike Reed | 113fd34 | 2017-01-14 13:45:02 -0500 | [diff] [blame] | 27 | for (int i = 0; i < 20; ++i) { |
Robert Phillips | 52b346e | 2017-01-19 08:37:31 -0500 | [diff] [blame] | 28 | paint.setColor(sk_tool_utils::color_to_565(rand.nextU() | (0xFF << 24))); |
Mike Reed | 113fd34 | 2017-01-14 13:45:02 -0500 | [diff] [blame] | 29 | canvas->drawRect({ 15, 15, 290, 40 }, paint); |
| 30 | canvas->translate(0, 30); |
| 31 | } |
| 32 | } |
| 33 | |
Robert Phillips | 42f3094 | 2017-01-17 10:48:52 -0500 | [diff] [blame] | 34 | class UnclippedSaveLayerGM : public skiagm::GM { |
| 35 | public: |
| 36 | enum class Mode { |
| 37 | kClipped, |
| 38 | kUnclipped |
| 39 | }; |
Mike Reed | 113fd34 | 2017-01-14 13:45:02 -0500 | [diff] [blame] | 40 | |
Robert Phillips | 42f3094 | 2017-01-17 10:48:52 -0500 | [diff] [blame] | 41 | UnclippedSaveLayerGM(Mode mode) : fMode(mode) { this->setBGColor(SK_ColorWHITE); } |
Mike Reed | 113fd34 | 2017-01-14 13:45:02 -0500 | [diff] [blame] | 42 | |
Robert Phillips | 42f3094 | 2017-01-17 10:48:52 -0500 | [diff] [blame] | 43 | protected: |
| 44 | bool runAsBench() const override { return true; } |
| 45 | |
| 46 | SkString onShortName() override { |
| 47 | if (Mode::kClipped == fMode) { |
| 48 | return SkString("savelayer_unclipped"); |
| 49 | } else { |
| 50 | SkASSERT(Mode::kUnclipped == fMode); |
| 51 | return SkString("savelayer_clipped"); |
| 52 | } |
Mike Reed | 113fd34 | 2017-01-14 13:45:02 -0500 | [diff] [blame] | 53 | } |
Robert Phillips | 42f3094 | 2017-01-17 10:48:52 -0500 | [diff] [blame] | 54 | |
| 55 | SkISize onISize() override { return SkISize::Make(320, 640); } |
| 56 | |
| 57 | void onDraw(SkCanvas* canvas) override { |
| 58 | const SkScalar L = 10; |
| 59 | const SkScalar T = 10; |
| 60 | const SkScalar R = 310; |
| 61 | const SkScalar B = 630; |
| 62 | |
| 63 | canvas->clipRect({ L, T, R, B }); |
| 64 | |
| 65 | for (int i = 0; i < 100; ++i) { |
| 66 | SkAutoCanvasRestore acr(canvas, true); |
| 67 | if (Mode::kClipped == fMode) { |
| 68 | save_layer_unclipped(canvas, L, T, R, T + 20); |
| 69 | save_layer_unclipped(canvas, L, B - 20, R, B); |
| 70 | } else { |
| 71 | SkASSERT(Mode::kUnclipped == fMode); |
| 72 | canvas->saveLayer({ L, T, R, B }, nullptr); |
Yuqian Li | d196cbe | 2017-02-23 10:28:33 -0500 | [diff] [blame] | 73 | } |
Robert Phillips | 42f3094 | 2017-01-17 10:48:52 -0500 | [diff] [blame] | 74 | |
| 75 | do_draw(canvas); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | private: |
| 80 | Mode fMode; |
| 81 | |
| 82 | typedef skiagm::GM INHERITED; |
| 83 | }; |
Mike Reed | c61abee | 2017-02-28 17:45:27 -0500 | [diff] [blame] | 84 | DEF_GM(return new UnclippedSaveLayerGM(UnclippedSaveLayerGM::Mode::kClipped);) |
| 85 | DEF_GM(return new UnclippedSaveLayerGM(UnclippedSaveLayerGM::Mode::kUnclipped);) |
Robert Phillips | 42f3094 | 2017-01-17 10:48:52 -0500 | [diff] [blame] | 86 | |
Yuqian Li | d196cbe | 2017-02-23 10:28:33 -0500 | [diff] [blame] | 87 | DEF_SIMPLE_GM(picture_savelayer, canvas, 320, 640) { |
| 88 | SkPaint paint1, paint2, paint3; |
| 89 | paint1.setAlpha(0x7f); |
| 90 | paint2.setAlpha(0x3f); |
| 91 | paint3.setColor(0xFFFF0000); |
| 92 | SkRect rect1{40, 5, 80, 70}, rect2{5, 40, 70, 80}, rect3{10, 10, 70, 70}; |
| 93 | // In the future, we might also test the clipped case by allowing i = 0 |
| 94 | for(int i = 1; i < 2; ++i) { |
| 95 | canvas->translate(100 * i, 0); |
| 96 | auto flag = i ? SkCanvas_kDontClipToLayer_PrivateSaveLayerFlag : 0; |
| 97 | canvas->saveLayer({ &rect1, &paint1, nullptr, flag}); |
| 98 | canvas->saveLayer({ &rect2, &paint2, nullptr, flag}); |
| 99 | canvas->drawRect(rect3, paint3); |
| 100 | canvas->restore(); |
| 101 | canvas->restore(); |
| 102 | } |
| 103 | }; |
| 104 | |
Mike Reed | c61abee | 2017-02-28 17:45:27 -0500 | [diff] [blame] | 105 | #include "Resources.h" |
Robert Phillips | 42f3094 | 2017-01-17 10:48:52 -0500 | [diff] [blame] | 106 | |
Mike Reed | c61abee | 2017-02-28 17:45:27 -0500 | [diff] [blame] | 107 | // Test kInitWithPrevious_SaveLayerFlag by drawing an image, save a layer with the flag, which |
| 108 | // should seed the layer with the image (from below). Then we punch a hole in the layer and |
| 109 | // restore with kPlus mode, which should show the mandrill super-bright on the outside, but |
| 110 | // normal where we punched the hole. |
| 111 | DEF_SIMPLE_GM(savelayer_initfromprev, canvas, 256, 256) { |
| 112 | canvas->drawImage(GetResourceAsImage("mandrill_256.png"), 0, 0, nullptr); |
| 113 | |
| 114 | SkCanvas::SaveLayerRec rec; |
| 115 | SkPaint paint; |
| 116 | paint.setBlendMode(SkBlendMode::kPlus); |
| 117 | rec.fSaveLayerFlags = SkCanvas::kInitWithPrevious_SaveLayerFlag; |
| 118 | rec.fPaint = &paint; |
| 119 | canvas->saveLayer(rec); |
| 120 | paint.setBlendMode(SkBlendMode::kClear); |
| 121 | canvas->drawCircle(128, 128, 96, paint); |
| 122 | canvas->restore(); |
| 123 | }; |
Robert Phillips | 42f3094 | 2017-01-17 10:48:52 -0500 | [diff] [blame] | 124 | |