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 | |
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/SkColorSpace.h" |
| 13 | #include "include/core/SkFont.h" |
| 14 | #include "include/core/SkImage.h" |
| 15 | #include "include/core/SkImageFilter.h" |
| 16 | #include "include/core/SkImageInfo.h" |
| 17 | #include "include/core/SkMaskFilter.h" |
| 18 | #include "include/core/SkMatrix.h" |
| 19 | #include "include/core/SkPaint.h" |
| 20 | #include "include/core/SkPicture.h" |
| 21 | #include "include/core/SkPictureRecorder.h" |
| 22 | #include "include/core/SkPoint.h" |
| 23 | #include "include/core/SkRect.h" |
| 24 | #include "include/core/SkRefCnt.h" |
| 25 | #include "include/core/SkScalar.h" |
| 26 | #include "include/core/SkShader.h" |
| 27 | #include "include/core/SkSize.h" |
| 28 | #include "include/core/SkString.h" |
| 29 | #include "include/core/SkSurface.h" |
| 30 | #include "include/core/SkTextBlob.h" |
| 31 | #include "include/core/SkTileMode.h" |
| 32 | #include "include/core/SkTypeface.h" |
| 33 | #include "include/core/SkTypes.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 34 | #include "include/effects/SkGradientShader.h" |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 35 | #include "include/effects/SkImageFilters.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 36 | #include "include/effects/SkShaderMaskFilter.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 37 | #include "include/utils/SkRandom.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 38 | #include "src/core/SkCanvasPriv.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 39 | #include "tools/Resources.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 40 | #include "tools/ToolUtils.h" |
Yuqian Li | d196cbe | 2017-02-23 10:28:33 -0500 | [diff] [blame] | 41 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 42 | #include <string.h> |
| 43 | #include <initializer_list> |
| 44 | |
Mike Reed | c61abee | 2017-02-28 17:45:27 -0500 | [diff] [blame] | 45 | // Test kInitWithPrevious_SaveLayerFlag by drawing an image, save a layer with the flag, which |
| 46 | // should seed the layer with the image (from below). Then we punch a hole in the layer and |
| 47 | // restore with kPlus mode, which should show the mandrill super-bright on the outside, but |
| 48 | // normal where we punched the hole. |
| 49 | DEF_SIMPLE_GM(savelayer_initfromprev, canvas, 256, 256) { |
Mike Reed | 8d29ab6 | 2021-01-23 18:10:39 -0500 | [diff] [blame] | 50 | canvas->drawImage(GetResourceAsImage("images/mandrill_256.png"), 0, 0); |
Mike Reed | c61abee | 2017-02-28 17:45:27 -0500 | [diff] [blame] | 51 | |
| 52 | SkCanvas::SaveLayerRec rec; |
| 53 | SkPaint paint; |
| 54 | paint.setBlendMode(SkBlendMode::kPlus); |
| 55 | rec.fSaveLayerFlags = SkCanvas::kInitWithPrevious_SaveLayerFlag; |
| 56 | rec.fPaint = &paint; |
| 57 | canvas->saveLayer(rec); |
| 58 | paint.setBlendMode(SkBlendMode::kClear); |
| 59 | canvas->drawCircle(128, 128, 96, paint); |
| 60 | canvas->restore(); |
| 61 | }; |
Robert Phillips | 42f3094 | 2017-01-17 10:48:52 -0500 | [diff] [blame] | 62 | |
Mike Reed | 910ca0f | 2018-04-25 13:04:05 -0400 | [diff] [blame] | 63 | DEF_SIMPLE_GM(savelayer_coverage, canvas, 500, 500) { |
| 64 | canvas->saveLayer(nullptr, nullptr); |
| 65 | |
| 66 | SkRect r = { 0, 0, 200, 200 }; |
| 67 | SkPaint layerPaint; |
| 68 | layerPaint.setBlendMode(SkBlendMode::kModulate); |
| 69 | |
| 70 | auto image = GetResourceAsImage("images/mandrill_128.png"); |
| 71 | |
| 72 | auto proc = [layerPaint](SkCanvas* canvas, SkCanvas::SaveLayerRec& rec) { |
| 73 | SkPaint paint; |
| 74 | paint.setColor(SK_ColorRED); |
| 75 | |
| 76 | canvas->saveLayer(rec); |
| 77 | canvas->drawCircle(100, 100, 50, paint); |
| 78 | paint.setColor(0x8800FF00); |
| 79 | canvas->drawRect({10, 90, 190, 110}, paint); |
| 80 | canvas->restore(); |
| 81 | }; |
| 82 | |
| 83 | const int yflags[] = { 0, SkCanvas::kInitWithPrevious_SaveLayerFlag }; |
| 84 | for (int y = 0; y <= 1; ++y) { |
| 85 | const int xflags[] = { 0, SkCanvas::kMaskAgainstCoverage_EXPERIMENTAL_DONT_USE_SaveLayerFlag }; |
| 86 | for (int x = 0; x <= 1; ++x) { |
| 87 | canvas->save(); |
| 88 | canvas->translate(x * 200.f, y * 200.f); |
| 89 | |
| 90 | SkCanvas::SaveLayerRec rec(&r, &layerPaint, yflags[y] | xflags[x]); |
Mike Reed | 8d29ab6 | 2021-01-23 18:10:39 -0500 | [diff] [blame] | 91 | canvas->drawImageRect(image, r, SkSamplingOptions(), nullptr); |
Mike Reed | 910ca0f | 2018-04-25 13:04:05 -0400 | [diff] [blame] | 92 | proc(canvas, rec); |
| 93 | |
| 94 | canvas->restore(); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | canvas->restore(); |
| 99 | } |
| 100 | |
Mike Reed | d567408 | 2019-04-19 15:00:47 -0400 | [diff] [blame] | 101 | static void draw_cell(SkCanvas* canvas, sk_sp<SkTextBlob> blob, SkColor c, SkScalar w, SkScalar h, |
| 102 | bool useDrawBehind) { |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 103 | SkRect r = SkRect::MakeWH(w, h); |
| 104 | SkPaint p; |
| 105 | p.setColor(c); |
| 106 | p.setBlendMode(SkBlendMode::kSrc); |
| 107 | canvas->drawRect(r, p); |
| 108 | p.setBlendMode(SkBlendMode::kSrcOver); |
| 109 | |
| 110 | const SkScalar margin = 80; |
| 111 | r.fLeft = w - margin; |
| 112 | |
| 113 | // save the behind image |
| 114 | SkDEBUGCODE(int sc0 =) canvas->getSaveCount(); |
| 115 | SkDEBUGCODE(int sc1 =) SkCanvasPriv::SaveBehind(canvas, &r); |
| 116 | SkDEBUGCODE(int sc2 =) canvas->getSaveCount(); |
| 117 | SkASSERT(sc0 == sc1); |
| 118 | SkASSERT(sc0 + 1 == sc2); |
| 119 | |
| 120 | // draw the foreground (including over the 'behind' section) |
| 121 | p.setColor(SK_ColorBLACK); |
| 122 | canvas->drawTextBlob(blob, 10, 30, p); |
| 123 | |
| 124 | // draw the treatment |
| 125 | const SkPoint pts[] = { {r.fLeft,0}, {r.fRight, 0} }; |
| 126 | const SkColor colors[] = { 0x88000000, 0x0 }; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 127 | auto sh = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp); |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 128 | p.setShader(sh); |
| 129 | p.setBlendMode(SkBlendMode::kDstIn); |
Mike Reed | d567408 | 2019-04-19 15:00:47 -0400 | [diff] [blame] | 130 | |
| 131 | if (useDrawBehind) { |
| 132 | SkCanvasPriv::DrawBehind(canvas, p); |
| 133 | } else { |
| 134 | canvas->drawRect(r, p); |
| 135 | } |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 136 | |
| 137 | // this should restore the behind image |
| 138 | canvas->restore(); |
| 139 | SkDEBUGCODE(int sc3 =) canvas->getSaveCount(); |
| 140 | SkASSERT(sc3 == sc0); |
| 141 | |
| 142 | // just outline where we expect the treatment to appear |
| 143 | p.reset(); |
| 144 | p.setStyle(SkPaint::kStroke_Style); |
Mike Reed | 9407e24 | 2019-02-15 16:13:57 -0500 | [diff] [blame] | 145 | p.setAlphaf(0.25f); |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 146 | } |
| 147 | |
Mike Reed | d567408 | 2019-04-19 15:00:47 -0400 | [diff] [blame] | 148 | static void draw_list(SkCanvas* canvas, sk_sp<SkTextBlob> blob, bool useDrawBehind) { |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 149 | SkAutoCanvasRestore acr(canvas, true); |
| 150 | |
| 151 | SkRandom rand; |
| 152 | SkScalar w = 400; |
| 153 | SkScalar h = 40; |
| 154 | for (int i = 0; i < 8; ++i) { |
| 155 | SkColor c = rand.nextU(); // ensure we're opaque |
| 156 | c = (c & 0xFFFFFF) | 0x80000000; |
Mike Reed | d567408 | 2019-04-19 15:00:47 -0400 | [diff] [blame] | 157 | draw_cell(canvas, blob, c, w, h, useDrawBehind); |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 158 | canvas->translate(0, h); |
| 159 | } |
| 160 | } |
| 161 | |
Mike Reed | d567408 | 2019-04-19 15:00:47 -0400 | [diff] [blame] | 162 | DEF_SIMPLE_GM(save_behind, canvas, 830, 670) { |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 163 | SkFont font; |
Mike Reed | 2ce3640 | 2019-04-21 18:15:23 -0400 | [diff] [blame] | 164 | font.setTypeface(ToolUtils::create_portable_typeface()); |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 165 | font.setSize(30); |
Mike Reed | 2ce3640 | 2019-04-21 18:15:23 -0400 | [diff] [blame] | 166 | |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 167 | const char text[] = "This is a very long line of text"; |
| 168 | auto blob = SkTextBlob::MakeFromText(text, strlen(text), font); |
| 169 | |
Mike Reed | d567408 | 2019-04-19 15:00:47 -0400 | [diff] [blame] | 170 | for (bool useDrawBehind : {false, true}) { |
| 171 | canvas->save(); |
| 172 | |
| 173 | draw_list(canvas, blob, useDrawBehind); |
| 174 | canvas->translate(0, 350); |
| 175 | canvas->saveLayer({0, 0, 400, 320}, nullptr); |
| 176 | draw_list(canvas, blob, useDrawBehind); |
| 177 | canvas->restore(); |
| 178 | |
| 179 | canvas->restore(); |
| 180 | canvas->translate(430, 0); |
| 181 | } |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 182 | } |
Mike Reed | 1f3548c | 2019-07-12 12:53:11 -0400 | [diff] [blame] | 183 | |
| 184 | #include "include/effects/SkGradientShader.h" |
| 185 | |
| 186 | DEF_SIMPLE_GM(savelayer_f16, canvas, 900, 300) { |
| 187 | int n = 15; |
| 188 | SkRect r{0, 0, 300, 300}; |
| 189 | SkPaint paint; |
| 190 | |
| 191 | const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorRED }; |
| 192 | paint.setShader(SkGradientShader::MakeSweep(r.centerX(), r.centerY(), |
| 193 | colors, nullptr, SK_ARRAY_COUNT(colors))); |
| 194 | |
| 195 | canvas->drawOval(r, paint); |
| 196 | |
| 197 | paint.setAlphaf(1.0f/n); |
| 198 | paint.setBlendMode(SkBlendMode::kPlus); |
| 199 | |
| 200 | for (auto flags : {0, (int)SkCanvas::kF16ColorType}) { |
| 201 | canvas->translate(r.width(), 0); |
| 202 | |
| 203 | SkCanvas::SaveLayerRec rec; |
| 204 | rec.fSaveLayerFlags = flags; |
| 205 | canvas->saveLayer(rec); |
| 206 | for (int i = 0; i < n; ++i) { |
| 207 | canvas->drawOval(r, paint); |
| 208 | } |
| 209 | canvas->restore(); |
| 210 | } |
| 211 | } |