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" 9 #include "sk_tool_utils.h" 10 11 12 static const uint32_t SkCanvas_kDontClipToLayer_PrivateSaveLayerFlag = 1U << 31; 13 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) { 19 SkRect rect = SkRect::MakeLTRB(l, t, r, b); 20 canvas->saveLayer({ &rect, nullptr, nullptr, SkCanvas_kDontClipToLayer_PrivateSaveLayerFlag }); 21 } 22 23 static void do_draw(SkCanvas* canvas) { 24 SkPaint paint; 25 SkRandom rand; 26 27 for (int i = 0; i < 20; ++i) { 28 paint.setColor(sk_tool_utils::color_to_565(rand.nextU() | (0xFF << 24))); 29 canvas->drawRect({ 15, 15, 290, 40 }, paint); 30 canvas->translate(0, 30); 31 } 32 } 33 34 class UnclippedSaveLayerGM : public skiagm::GM { 35 public: 36 enum class Mode { 37 kClipped, 38 kUnclipped 39 }; 40 41 UnclippedSaveLayerGM(Mode mode) : fMode(mode) { this->setBGColor(SK_ColorWHITE); } 42 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 } 53 } 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); 73 } 74 75 do_draw(canvas); 76 } 77 } 78 79 private: 80 Mode fMode; 81 82 typedef skiagm::GM INHERITED; 83 }; 84 DEF_GM(return new UnclippedSaveLayerGM(UnclippedSaveLayerGM::Mode::kClipped);) 85 DEF_GM(return new UnclippedSaveLayerGM(UnclippedSaveLayerGM::Mode::kUnclipped);) 86 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 105 #include "Resources.h" 106 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 }; 124 125