• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/gm.h"
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"
34 #include "include/effects/SkGradientShader.h"
35 #include "include/effects/SkImageFilters.h"
36 #include "include/effects/SkShaderMaskFilter.h"
37 #include "include/utils/SkRandom.h"
38 #include "src/core/SkCanvasPriv.h"
39 #include "tools/Resources.h"
40 #include "tools/ToolUtils.h"
41 
42 #include <string.h>
43 #include <initializer_list>
44 
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) {
50     canvas->drawImage(GetResourceAsImage("images/mandrill_256.png"), 0, 0);
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 };
62 
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 
__anon572f263a0102(SkCanvas* canvas, SkCanvas::SaveLayerRec& rec) 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]);
91             canvas->drawImageRect(image, r, SkSamplingOptions(), nullptr);
92             proc(canvas, rec);
93 
94             canvas->restore();
95         }
96     }
97 
98     canvas->restore();
99 }
100 
draw_cell(SkCanvas * canvas,sk_sp<SkTextBlob> blob,SkColor c,SkScalar w,SkScalar h,bool useDrawBehind)101 static void draw_cell(SkCanvas* canvas, sk_sp<SkTextBlob> blob, SkColor c, SkScalar w, SkScalar h,
102                       bool useDrawBehind) {
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 };
127     auto sh = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
128     p.setShader(sh);
129     p.setBlendMode(SkBlendMode::kDstIn);
130 
131     if (useDrawBehind) {
132         SkCanvasPriv::DrawBehind(canvas, p);
133     } else {
134         canvas->drawRect(r, p);
135     }
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);
145     p.setAlphaf(0.25f);
146 }
147 
draw_list(SkCanvas * canvas,sk_sp<SkTextBlob> blob,bool useDrawBehind)148 static void draw_list(SkCanvas* canvas, sk_sp<SkTextBlob> blob, bool useDrawBehind) {
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;
157         draw_cell(canvas, blob, c, w, h, useDrawBehind);
158         canvas->translate(0, h);
159     }
160 }
161 
162 DEF_SIMPLE_GM(save_behind, canvas, 830, 670) {
163     SkFont font;
164     font.setTypeface(ToolUtils::create_portable_typeface());
165     font.setSize(30);
166 
167     const char text[] = "This is a very long line of text";
168     auto blob = SkTextBlob::MakeFromText(text, strlen(text), font);
169 
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     }
182 }
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 }
212