1 /*
2 * Copyright 2011 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 #include "SkCanvas.h"
11 #include "SkPath.h"
12 #include "SkMakeUnique.h"
13
14
15 #include "SkCubicMap.h"
16
test_cubic(SkCanvas * canvas)17 static void test_cubic(SkCanvas* canvas) {
18 const SkPoint pts[] = {
19 { 0.333333f, 0.333333f }, { 0.666666f, 0.666666f },
20 { 1, 0 }, { 0, 1 },
21 { 0, 1 }, { 1, 0 },
22 { 0, 0 }, { 1, 1 },
23 { 1, 1 }, { 0, 0 },
24 { 0, 1 }, { 0, 1 },
25 { 1, 0 }, { 1, 0 },
26 };
27
28 SkPaint paint0, paint1;
29 paint0.setAntiAlias(true); paint0.setStrokeWidth(3/256.0f); paint0.setColor(SK_ColorRED);
30 paint1.setAntiAlias(true);
31
32 SkCubicMap cmap;
33
34 canvas->translate(10, 266);
35 canvas->scale(256, -256);
36 for (size_t i = 0; i < SK_ARRAY_COUNT(pts); i += 2) {
37 cmap.setPts(pts[i], pts[i+1]);
38
39 const int N = 128;
40 SkPoint tmp0[N+1], tmp1[N+1], tmp2[N+1];
41 for (int j = 0; j <= N; ++j) {
42 float p = j * 1.0f / N;
43 tmp0[j] = cmap.computeFromT(p);
44 tmp1[j].set(p, cmap.computeYFromX(p));
45 tmp2[j].set(p, cmap.hackYFromX(p));
46 }
47
48 canvas->save();
49 canvas->drawPoints(SkCanvas::kPolygon_PointMode, N+1, tmp0, paint0);
50 canvas->drawPoints(SkCanvas::kPolygon_PointMode, N+1, tmp1, paint1);
51 canvas->translate(0, -1.2f);
52 canvas->drawPoints(SkCanvas::kPolygon_PointMode, N+1, tmp0, paint0);
53 canvas->drawPoints(SkCanvas::kPolygon_PointMode, N+1, tmp2, paint1);
54 canvas->restore();
55
56 canvas->translate(1.1f, 0);
57 }
58 }
59
do_draw(SkCanvas * canvas,const SkRect & r)60 static void do_draw(SkCanvas* canvas, const SkRect& r) {
61 SkPaint paint;
62 paint.setBlendMode(SkBlendMode::kSrc);
63 paint.setColor(0x800000FF);
64 canvas->drawRect(r, paint);
65 }
66
67 /**
68 * Exercise kDontClipToLayer_Legacy_SaveLayerFlag flag, which does not limit the clip to the
69 * layer's bounds. Thus when a draw occurs, it can (depending on "where" it is) draw into the layer
70 * and/or draw onto the surrounding portions of the canvas, or both.
71 *
72 * This GM has a 100x100 rectangle (r), which its going to draw. However first it creates a layer
73 * with this flag covering 1/2 of the rectangle (upper half). Then it draws the rect in SRC mode.
74 *
75 * The portion of the draw that intersects the layer should see the SRC draw, apply it to the layer
76 * and then during restore, it will SRC_OVER that layer onto the canvas (SRC_OVER since the layer
77 * has no paint, so it gets the default xfermode during restore).
78 *
79 * Note: when we talk about drawing directly into the "canvas", in fact we are drawing into an
80 * "outer" layer we created (filled with red). This is a testing detail, so that our final
81 * output image is itself opaque, otherwise we make it harder to view the GM as a PNG.
82 *
83 * The portion of the draw below the layer draws directly into the canvas. Since it is in SRC mode,
84 * it will write 0x80 to the canvas' alpha, overwriting the "red", which then gets blended with
85 * the GM's white background.
86 *
87 * The portion in the layer, will end up SRC_OVERing the 0x80 layer pixels onto the canvas' red
88 * pixels, making magenta.
89 *
90 * Thus the expected result is the upper half to be magenta 0xFF7F0080, and the lower half to be
91 * light blue 0xFF7F7FFF.
92 */
93 DEF_SIMPLE_GM(dont_clip_to_layer, canvas, 120, 120) {
94 const SkRect r { 10, 10, 110, 110 };
95
96 // Wrap the entire test inside a red layer, so we don't punch the actual gm's alpha with
97 // kSrc_Mode, which makes it hard to view (we like our GMs to have opaque pixels).
98 canvas->saveLayer(&r, nullptr);
99 canvas->drawColor(SK_ColorRED);
100
101 SkRect r0 = { 20, 20, 100, 55 };
102 SkRect r1 = { 20, 65, 100, 100 };
103
104 SkCanvas::SaveLayerRec rec;
105 rec.fPaint = nullptr;
106 rec.fBounds = &r0;
107 rec.fBackdrop = nullptr;
108 rec.fSaveLayerFlags = 1 << 31;//SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag;
109 canvas->saveLayer(rec);
110 rec.fBounds = &r1;
111 canvas->saveLayer(rec);
112 do_draw(canvas, r);
113 canvas->restore();
114 canvas->restore();
115
116 canvas->restore(); // red-layer
117 }
118
119 /** Draw a 2px border around the target, then red behind the target;
120 set the clip to match the target, then draw >> the target in blue.
121 */
122
draw(SkCanvas * canvas,SkRect & target,int x,int y)123 static void draw(SkCanvas* canvas, SkRect& target, int x, int y) {
124 SkPaint borderPaint;
125 borderPaint.setColor(SkColorSetRGB(0x0, 0xDD, 0x0));
126 borderPaint.setAntiAlias(true);
127 SkPaint backgroundPaint;
128 backgroundPaint.setColor(SkColorSetRGB(0xDD, 0x0, 0x0));
129 backgroundPaint.setAntiAlias(true);
130 SkPaint foregroundPaint;
131 foregroundPaint.setColor(SkColorSetRGB(0x0, 0x0, 0xDD));
132 foregroundPaint.setAntiAlias(true);
133
134 canvas->save();
135 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
136 target.inset(SkIntToScalar(-2), SkIntToScalar(-2));
137 canvas->drawRect(target, borderPaint);
138 target.inset(SkIntToScalar(2), SkIntToScalar(2));
139 canvas->drawRect(target, backgroundPaint);
140 canvas->clipRect(target, true);
141 target.inset(SkIntToScalar(-4), SkIntToScalar(-4));
142 canvas->drawRect(target, foregroundPaint);
143 canvas->restore();
144 }
145
draw_square(SkCanvas * canvas,int x,int y)146 static void draw_square(SkCanvas* canvas, int x, int y) {
147 SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 10 * SK_Scalar1));
148 draw(canvas, target, x, y);
149 }
150
draw_column(SkCanvas * canvas,int x,int y)151 static void draw_column(SkCanvas* canvas, int x, int y) {
152 SkRect target (SkRect::MakeWH(1 * SK_Scalar1, 10 * SK_Scalar1));
153 draw(canvas, target, x, y);
154 }
155
draw_bar(SkCanvas * canvas,int x,int y)156 static void draw_bar(SkCanvas* canvas, int x, int y) {
157 SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 1 * SK_Scalar1));
158 draw(canvas, target, x, y);
159 }
160
draw_rect_tests(SkCanvas * canvas)161 static void draw_rect_tests(SkCanvas* canvas) {
162 draw_square(canvas, 10, 10);
163 draw_column(canvas, 30, 10);
164 draw_bar(canvas, 10, 30);
165 }
166
167 /**
168 Test a set of clipping problems discovered while writing blitAntiRect,
169 and test all the code paths through the clipping blitters.
170 Each region should show as a blue center surrounded by a 2px green
171 border, with no red.
172 */
173
174 class AAClipGM : public skiagm::GM {
175 public:
AAClipGM()176 AAClipGM() {
177
178 }
179
180 protected:
onShortName()181 SkString onShortName() override {
182 return SkString("aaclip");
183 }
184
onISize()185 SkISize onISize() override {
186 return SkISize::Make(240, 120);
187 }
188
onDraw(SkCanvas * canvas)189 void onDraw(SkCanvas* canvas) override {
190 if (0) { test_cubic(canvas); return; }
191
192 // Initial pixel-boundary-aligned draw
193 draw_rect_tests(canvas);
194
195 // Repeat 4x with .2, .4, .6, .8 px offsets
196 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
197 canvas->translate(SkIntToScalar(50), 0);
198 draw_rect_tests(canvas);
199
200 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
201 canvas->translate(SkIntToScalar(50), 0);
202 draw_rect_tests(canvas);
203
204 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
205 canvas->translate(SkIntToScalar(50), 0);
206 draw_rect_tests(canvas);
207
208 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
209 canvas->translate(SkIntToScalar(50), 0);
210 draw_rect_tests(canvas);
211 }
212
213 private:
214 typedef skiagm::GM INHERITED;
215 };
216
DEF_GM(return new AAClipGM;)217 DEF_GM(return new AAClipGM;)
218
219 /////////////////////////////////////////////////////////////////////////
220
221 #ifdef SK_BUILD_FOR_MAC
222
223 static std::unique_ptr<SkCanvas> make_canvas(const SkBitmap& bm) {
224 const SkImageInfo& info = bm.info();
225 if (info.bytesPerPixel() == 4) {
226 return SkCanvas::MakeRasterDirectN32(info.width(), info.height(),
227 (SkPMColor*)bm.getPixels(),
228 bm.rowBytes());
229 } else {
230 return skstd::make_unique<SkCanvas>(bm);
231 }
232 }
233
234 #include "SkCGUtils.h"
test_image(SkCanvas * canvas,const SkImageInfo & info)235 static void test_image(SkCanvas* canvas, const SkImageInfo& info) {
236 SkBitmap bm;
237 bm.allocPixels(info);
238
239 if (info.isOpaque()) {
240 bm.eraseColor(SK_ColorGREEN);
241 } else {
242 bm.eraseColor(0);
243 }
244
245 SkPaint paint;
246 paint.setAntiAlias(true);
247 paint.setColor(SK_ColorBLUE);
248 make_canvas(bm)->drawCircle(50, 50, 49, paint);
249 canvas->drawBitmap(bm, 10, 10);
250
251 CGImageRef image = SkCreateCGImageRefWithColorspace(bm, nullptr);
252
253 SkBitmap bm2;
254 SkCreateBitmapFromCGImage(&bm2, image);
255 canvas->drawBitmap(bm2, 10, 120);
256 canvas->drawImage(SkMakeImageFromCGImage(image), 10, 120 + bm2.height() + 10);
257
258 CGImageRelease(image);
259 }
260
261 class CGImageGM : public skiagm::GM {
262 public:
CGImageGM()263 CGImageGM() {}
264
265 protected:
onShortName()266 SkString onShortName() override {
267 return SkString("cgimage");
268 }
269
onISize()270 SkISize onISize() override {
271 return SkISize::Make(800, 250);
272 }
273
onDraw(SkCanvas * canvas)274 void onDraw(SkCanvas* canvas) override {
275 const struct {
276 SkColorType fCT;
277 SkAlphaType fAT;
278 } rec[] = {
279 { kRGB_565_SkColorType, kOpaque_SkAlphaType },
280
281 { kRGBA_8888_SkColorType, kPremul_SkAlphaType },
282 { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType },
283 { kRGBA_8888_SkColorType, kOpaque_SkAlphaType },
284
285 { kBGRA_8888_SkColorType, kPremul_SkAlphaType },
286 { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType },
287 { kBGRA_8888_SkColorType, kOpaque_SkAlphaType },
288 };
289
290 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
291 SkImageInfo info = SkImageInfo::Make(100, 100, rec[i].fCT, rec[i].fAT);
292 test_image(canvas, info);
293 canvas->translate(info.width() + 10, 0);
294 }
295 }
296
297 private:
298 typedef skiagm::GM INHERITED;
299 };
300 //DEF_GM( return new CGImageGM; )
301 #endif
302
303 ///////////////////////////////////////////////////////////////////////////////////////////////////
304
305 // https://bug.skia.org/3716
306 class ClipCubicGM : public skiagm::GM {
307 const SkScalar W = 100;
308 const SkScalar H = 240;
309
310 SkPath fVPath, fHPath;
311 public:
ClipCubicGM()312 ClipCubicGM() {
313 fVPath.moveTo(W, 0);
314 fVPath.cubicTo(W, H-10, 0, 10, 0, H);
315
316 SkMatrix pivot;
317 pivot.setRotate(90, W/2, H/2);
318 fVPath.transform(pivot, &fHPath);
319 }
320
321 protected:
onShortName()322 SkString onShortName() override {
323 return SkString("clipcubic");
324 }
325
onISize()326 SkISize onISize() override {
327 return SkISize::Make(400, 410);
328 }
329
doDraw(SkCanvas * canvas,const SkPath & path)330 void doDraw(SkCanvas* canvas, const SkPath& path) {
331 SkPaint paint;
332 paint.setAntiAlias(true);
333
334 paint.setColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
335 canvas->drawPath(path, paint);
336
337 paint.setColor(SK_ColorRED);
338 paint.setStyle(SkPaint::kStroke_Style);
339 canvas->drawPath(path, paint);
340 }
341
drawAndClip(SkCanvas * canvas,const SkPath & path,SkScalar dx,SkScalar dy)342 void drawAndClip(SkCanvas* canvas, const SkPath& path, SkScalar dx, SkScalar dy) {
343 SkAutoCanvasRestore acr(canvas, true);
344
345 SkRect r = SkRect::MakeXYWH(0, H/4, W, H/2);
346 SkPaint paint;
347 paint.setColor(sk_tool_utils::color_to_565(0xFF8888FF));
348
349 canvas->drawRect(r, paint);
350 this->doDraw(canvas, path);
351
352 canvas->translate(dx, dy);
353
354 canvas->drawRect(r, paint);
355 canvas->clipRect(r);
356 this->doDraw(canvas, path);
357 }
358
onDraw(SkCanvas * canvas)359 void onDraw(SkCanvas* canvas) override {
360 canvas->translate(80, 10);
361 this->drawAndClip(canvas, fVPath, 200, 0);
362 canvas->translate(0, 200);
363 this->drawAndClip(canvas, fHPath, 200, 0);
364 }
365
366 private:
367 typedef skiagm::GM INHERITED;
368 };
369 DEF_GM(return new ClipCubicGM;)
370