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
draw_cell(SkCanvas * canvas,sk_sp<SkTextBlob> blob,SkColor c,SkScalar w,SkScalar h,bool useDrawBehind)63 static void draw_cell(SkCanvas* canvas, sk_sp<SkTextBlob> blob, SkColor c, SkScalar w, SkScalar h,
64 bool useDrawBehind) {
65 SkRect r = SkRect::MakeWH(w, h);
66 SkPaint p;
67 p.setColor(c);
68 p.setBlendMode(SkBlendMode::kSrc);
69 canvas->drawRect(r, p);
70 p.setBlendMode(SkBlendMode::kSrcOver);
71
72 const SkScalar margin = 80;
73 r.fLeft = w - margin;
74
75 // save the behind image
76 SkDEBUGCODE(int sc0 =) canvas->getSaveCount();
77 SkDEBUGCODE(int sc1 =) SkCanvasPriv::SaveBehind(canvas, &r);
78 SkDEBUGCODE(int sc2 =) canvas->getSaveCount();
79 SkASSERT(sc0 == sc1);
80 SkASSERT(sc0 + 1 == sc2);
81
82 // draw the foreground (including over the 'behind' section)
83 p.setColor(SK_ColorBLACK);
84 canvas->drawTextBlob(blob, 10, 30, p);
85
86 // draw the treatment
87 const SkPoint pts[] = { {r.fLeft,0}, {r.fRight, 0} };
88 const SkColor colors[] = { 0x88000000, 0x0 };
89 auto sh = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
90 p.setShader(sh);
91 p.setBlendMode(SkBlendMode::kDstIn);
92
93 if (useDrawBehind) {
94 SkCanvasPriv::DrawBehind(canvas, p);
95 } else {
96 canvas->drawRect(r, p);
97 }
98
99 // this should restore the behind image
100 canvas->restore();
101 SkDEBUGCODE(int sc3 =) canvas->getSaveCount();
102 SkASSERT(sc3 == sc0);
103
104 // just outline where we expect the treatment to appear
105 p.reset();
106 p.setStyle(SkPaint::kStroke_Style);
107 p.setAlphaf(0.25f);
108 }
109
draw_list(SkCanvas * canvas,sk_sp<SkTextBlob> blob,bool useDrawBehind)110 static void draw_list(SkCanvas* canvas, sk_sp<SkTextBlob> blob, bool useDrawBehind) {
111 SkAutoCanvasRestore acr(canvas, true);
112
113 SkRandom rand;
114 SkScalar w = 400;
115 SkScalar h = 40;
116 for (int i = 0; i < 8; ++i) {
117 SkColor c = rand.nextU(); // ensure we're opaque
118 c = (c & 0xFFFFFF) | 0x80000000;
119 draw_cell(canvas, blob, c, w, h, useDrawBehind);
120 canvas->translate(0, h);
121 }
122 }
123
124 DEF_SIMPLE_GM(save_behind, canvas, 830, 670) {
125 SkFont font;
126 font.setTypeface(ToolUtils::create_portable_typeface());
127 font.setSize(30);
128
129 const char text[] = "This is a very long line of text";
130 auto blob = SkTextBlob::MakeFromText(text, strlen(text), font);
131
132 for (bool useDrawBehind : {false, true}) {
133 canvas->save();
134
135 draw_list(canvas, blob, useDrawBehind);
136 canvas->translate(0, 350);
137 canvas->saveLayer({0, 0, 400, 320}, nullptr);
138 draw_list(canvas, blob, useDrawBehind);
139 canvas->restore();
140
141 canvas->restore();
142 canvas->translate(430, 0);
143 }
144 }
145
146 #include "include/effects/SkGradientShader.h"
147
148 DEF_SIMPLE_GM(savelayer_f16, canvas, 900, 300) {
149 int n = 15;
150 SkRect r{0, 0, 300, 300};
151 SkPaint paint;
152
153 const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorRED };
154 paint.setShader(SkGradientShader::MakeSweep(r.centerX(), r.centerY(),
155 colors, nullptr, SK_ARRAY_COUNT(colors)));
156
157 canvas->drawOval(r, paint);
158
159 paint.setAlphaf(1.0f/n);
160 paint.setBlendMode(SkBlendMode::kPlus);
161
162 for (auto flags : {0, (int)SkCanvas::kF16ColorType}) {
163 canvas->translate(r.width(), 0);
164
165 SkCanvas::SaveLayerRec rec;
166 rec.fSaveLayerFlags = flags;
167 canvas->saveLayer(rec);
168 for (int i = 0; i < n; ++i) {
169 canvas->drawOval(r, paint);
170 }
171 canvas->restore();
172 }
173 }
174