blob: cceb11d38cea51007d50ec2fbcd30d59ed755c64 [file] [log] [blame]
tomhudson@google.comef279d32011-12-21 14:27:14 +00001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -04009#include "include/core/SkBlendMode.h"
10#include "include/core/SkCanvas.h"
11#include "include/core/SkColor.h"
12#include "include/core/SkMatrix.h"
13#include "include/core/SkPaint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/core/SkPath.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -040015#include "include/core/SkRect.h"
16#include "include/core/SkScalar.h"
17#include "include/core/SkSize.h"
18#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/core/SkCanvasPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "tools/ToolUtils.h"
tomhudson@google.comef279d32011-12-21 14:27:14 +000021
reed02f9ed72016-09-06 09:06:18 -070022static void do_draw(SkCanvas* canvas, const SkRect& r) {
23 SkPaint paint;
reed374772b2016-10-05 17:33:02 -070024 paint.setBlendMode(SkBlendMode::kSrc);
reed02f9ed72016-09-06 09:06:18 -070025 paint.setColor(0x800000FF);
26 canvas->drawRect(r, paint);
27}
28
29/**
Cary Clark7eddfb82018-03-13 14:41:10 -040030 * Exercise SkCanvasPriv::kDontClipToLayer_SaveLayerFlag flag, which does not limit the clip to the
reed02f9ed72016-09-06 09:06:18 -070031 * layer's bounds. Thus when a draw occurs, it can (depending on "where" it is) draw into the layer
32 * and/or draw onto the surrounding portions of the canvas, or both.
33 *
34 * This GM has a 100x100 rectangle (r), which its going to draw. However first it creates a layer
35 * with this flag covering 1/2 of the rectangle (upper half). Then it draws the rect in SRC mode.
36 *
37 * The portion of the draw that intersects the layer should see the SRC draw, apply it to the layer
38 * and then during restore, it will SRC_OVER that layer onto the canvas (SRC_OVER since the layer
39 * has no paint, so it gets the default xfermode during restore).
40 *
reed28d1ce52016-09-06 14:57:00 -070041 * Note: when we talk about drawing directly into the "canvas", in fact we are drawing into an
42 * "outer" layer we created (filled with red). This is a testing detail, so that our final
43 * output image is itself opaque, otherwise we make it harder to view the GM as a PNG.
reed02f9ed72016-09-06 09:06:18 -070044 *
reed28d1ce52016-09-06 14:57:00 -070045 * The portion of the draw below the layer draws directly into the canvas. Since it is in SRC mode,
46 * it will write 0x80 to the canvas' alpha, overwriting the "red", which then gets blended with
47 * the GM's white background.
48 *
49 * The portion in the layer, will end up SRC_OVERing the 0x80 layer pixels onto the canvas' red
50 * pixels, making magenta.
51 *
52 * Thus the expected result is the upper half to be magenta 0xFF7F0080, and the lower half to be
53 * light blue 0xFF7F7FFF.
reed02f9ed72016-09-06 09:06:18 -070054 */
55DEF_SIMPLE_GM(dont_clip_to_layer, canvas, 120, 120) {
reed28d1ce52016-09-06 14:57:00 -070056 const SkRect r { 10, 10, 110, 110 };
57
58 // Wrap the entire test inside a red layer, so we don't punch the actual gm's alpha with
59 // kSrc_Mode, which makes it hard to view (we like our GMs to have opaque pixels).
60 canvas->saveLayer(&r, nullptr);
61 canvas->drawColor(SK_ColorRED);
62
Mike Reed8a8937c2017-02-14 10:12:34 -050063 SkRect r0 = { 20, 20, 100, 55 };
64 SkRect r1 = { 20, 65, 100, 100 };
reed02f9ed72016-09-06 09:06:18 -070065
66 SkCanvas::SaveLayerRec rec;
67 rec.fPaint = nullptr;
68 rec.fBounds = &r0;
69 rec.fBackdrop = nullptr;
Cary Clark7eddfb82018-03-13 14:41:10 -040070 rec.fSaveLayerFlags = SkCanvasPriv::kDontClipToLayer_SaveLayerFlag;
reed02f9ed72016-09-06 09:06:18 -070071 canvas->saveLayer(rec);
Mike Reed8a8937c2017-02-14 10:12:34 -050072 rec.fBounds = &r1;
73 canvas->saveLayer(rec);
reed02f9ed72016-09-06 09:06:18 -070074 do_draw(canvas, r);
75 canvas->restore();
Matt Sarett25d82962017-04-24 13:59:54 -040076 canvas->restore();
reed28d1ce52016-09-06 14:57:00 -070077
78 canvas->restore(); // red-layer
reed02f9ed72016-09-06 09:06:18 -070079}
80
tomhudson@google.comef279d32011-12-21 14:27:14 +000081/** Draw a 2px border around the target, then red behind the target;
82 set the clip to match the target, then draw >> the target in blue.
83*/
84
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000085static void draw(SkCanvas* canvas, SkRect& target, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +000086 SkPaint borderPaint;
87 borderPaint.setColor(SkColorSetRGB(0x0, 0xDD, 0x0));
88 borderPaint.setAntiAlias(true);
89 SkPaint backgroundPaint;
90 backgroundPaint.setColor(SkColorSetRGB(0xDD, 0x0, 0x0));
91 backgroundPaint.setAntiAlias(true);
92 SkPaint foregroundPaint;
93 foregroundPaint.setColor(SkColorSetRGB(0x0, 0x0, 0xDD));
94 foregroundPaint.setAntiAlias(true);
95
96 canvas->save();
tomhudson@google.comabfa8d12011-12-28 19:40:48 +000097 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
98 target.inset(SkIntToScalar(-2), SkIntToScalar(-2));
tomhudson@google.comef279d32011-12-21 14:27:14 +000099 canvas->drawRect(target, borderPaint);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000100 target.inset(SkIntToScalar(2), SkIntToScalar(2));
tomhudson@google.comef279d32011-12-21 14:27:14 +0000101 canvas->drawRect(target, backgroundPaint);
reed66998382016-09-21 11:15:07 -0700102 canvas->clipRect(target, true);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000103 target.inset(SkIntToScalar(-4), SkIntToScalar(-4));
tomhudson@google.comef279d32011-12-21 14:27:14 +0000104 canvas->drawRect(target, foregroundPaint);
105 canvas->restore();
106}
107
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000108static void draw_square(SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000109 SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 10 * SK_Scalar1));
110 draw(canvas, target, x, y);
111}
112
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000113static void draw_column(SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000114 SkRect target (SkRect::MakeWH(1 * SK_Scalar1, 10 * SK_Scalar1));
115 draw(canvas, target, x, y);
116}
117
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000118static void draw_bar(SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000119 SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 1 * SK_Scalar1));
120 draw(canvas, target, x, y);
121}
122
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000123static void draw_rect_tests(SkCanvas* canvas) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000124 draw_square(canvas, 10, 10);
125 draw_column(canvas, 30, 10);
126 draw_bar(canvas, 10, 30);
127}
128
129/**
130 Test a set of clipping problems discovered while writing blitAntiRect,
131 and test all the code paths through the clipping blitters.
132 Each region should show as a blue center surrounded by a 2px green
133 border, with no red.
134*/
Hal Canary607c44f2019-01-23 10:40:02 -0500135DEF_SIMPLE_GM(aaclip, canvas, 240, 120) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000136 // Initial pixel-boundary-aligned draw
137 draw_rect_tests(canvas);
138
139 // Repeat 4x with .2, .4, .6, .8 px offsets
140 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000141 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000142 draw_rect_tests(canvas);
143
144 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000145 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000146 draw_rect_tests(canvas);
147
148 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000149 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000150 draw_rect_tests(canvas);
151
152 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000153 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000154 draw_rect_tests(canvas);
Hal Canary607c44f2019-01-23 10:40:02 -0500155}
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000156
157/////////////////////////////////////////////////////////////////////////
158
159#ifdef SK_BUILD_FOR_MAC
160
Ben Wagnerd1701ba2019-04-30 13:44:26 -0400161#include "include/utils/mac/SkCGUtils.h"
162#include "src/core/SkMakeUnique.h"
163
Mike Reed5df49342016-11-12 08:06:55 -0600164static std::unique_ptr<SkCanvas> make_canvas(const SkBitmap& bm) {
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000165 const SkImageInfo& info = bm.info();
166 if (info.bytesPerPixel() == 4) {
Mike Reed5df49342016-11-12 08:06:55 -0600167 return SkCanvas::MakeRasterDirectN32(info.width(), info.height(),
168 (SkPMColor*)bm.getPixels(),
169 bm.rowBytes());
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000170 } else {
Mike Reed5df49342016-11-12 08:06:55 -0600171 return skstd::make_unique<SkCanvas>(bm);
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000172 }
173}
174
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000175static void test_image(SkCanvas* canvas, const SkImageInfo& info) {
176 SkBitmap bm;
177 bm.allocPixels(info);
178
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000179 if (info.isOpaque()) {
180 bm.eraseColor(SK_ColorGREEN);
181 } else {
182 bm.eraseColor(0);
183 }
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000184
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000185 SkPaint paint;
186 paint.setAntiAlias(true);
187 paint.setColor(SK_ColorBLUE);
Mike Reed5df49342016-11-12 08:06:55 -0600188 make_canvas(bm)->drawCircle(50, 50, 49, paint);
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000189 canvas->drawBitmap(bm, 10, 10);
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000190
halcanary96fcdcc2015-08-27 07:41:13 -0700191 CGImageRef image = SkCreateCGImageRefWithColorspace(bm, nullptr);
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000192
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000193 SkBitmap bm2;
194 SkCreateBitmapFromCGImage(&bm2, image);
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000195 canvas->drawBitmap(bm2, 10, 120);
Mike Reed463c8482016-12-21 12:01:12 -0500196 canvas->drawImage(SkMakeImageFromCGImage(image), 10, 120 + bm2.height() + 10);
197
198 CGImageRelease(image);
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000199}
200
Hal Canary607c44f2019-01-23 10:40:02 -0500201DEF_SIMPLE_GM(cgimage, canvas, 800, 250) {
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000202 const struct {
203 SkColorType fCT;
204 SkAlphaType fAT;
205 } rec[] = {
206 { kRGB_565_SkColorType, kOpaque_SkAlphaType },
207
208 { kRGBA_8888_SkColorType, kPremul_SkAlphaType },
209 { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType },
210 { kRGBA_8888_SkColorType, kOpaque_SkAlphaType },
211
212 { kBGRA_8888_SkColorType, kPremul_SkAlphaType },
213 { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType },
214 { kBGRA_8888_SkColorType, kOpaque_SkAlphaType },
215 };
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000216
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000217 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
218 SkImageInfo info = SkImageInfo::Make(100, 100, rec[i].fCT, rec[i].fAT);
219 test_image(canvas, info);
220 canvas->translate(info.width() + 10, 0);
221 }
Hal Canary607c44f2019-01-23 10:40:02 -0500222}
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000223
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000224#endif
reed8f76cb92015-04-22 17:38:23 -0700225
226///////////////////////////////////////////////////////////////////////////////////////////////////
227
halcanary6950de62015-11-07 05:29:00 -0800228// https://bug.skia.org/3716
reed8f76cb92015-04-22 17:38:23 -0700229class ClipCubicGM : public skiagm::GM {
230 const SkScalar W = 100;
231 const SkScalar H = 240;
232
233 SkPath fVPath, fHPath;
234public:
235 ClipCubicGM() {
236 fVPath.moveTo(W, 0);
237 fVPath.cubicTo(W, H-10, 0, 10, 0, H);
halcanary9d524f22016-03-29 09:03:52 -0700238
reed8f76cb92015-04-22 17:38:23 -0700239 SkMatrix pivot;
240 pivot.setRotate(90, W/2, H/2);
241 fVPath.transform(pivot, &fHPath);
242 }
243
244protected:
245 SkString onShortName() override {
246 return SkString("clipcubic");
247 }
halcanary9d524f22016-03-29 09:03:52 -0700248
reed8f76cb92015-04-22 17:38:23 -0700249 SkISize onISize() override {
250 return SkISize::Make(400, 410);
251 }
252
253 void doDraw(SkCanvas* canvas, const SkPath& path) {
254 SkPaint paint;
255 paint.setAntiAlias(true);
halcanary9d524f22016-03-29 09:03:52 -0700256
Mike Kleind46dce32018-08-16 10:17:03 -0400257 paint.setColor(0xFFCCCCCC);
reed8f76cb92015-04-22 17:38:23 -0700258 canvas->drawPath(path, paint);
halcanary9d524f22016-03-29 09:03:52 -0700259
reed8f76cb92015-04-22 17:38:23 -0700260 paint.setColor(SK_ColorRED);
261 paint.setStyle(SkPaint::kStroke_Style);
262 canvas->drawPath(path, paint);
263 }
264
265 void drawAndClip(SkCanvas* canvas, const SkPath& path, SkScalar dx, SkScalar dy) {
266 SkAutoCanvasRestore acr(canvas, true);
267
268 SkRect r = SkRect::MakeXYWH(0, H/4, W, H/2);
269 SkPaint paint;
Mike Kleinea3f0142019-03-20 11:12:10 -0500270 paint.setColor(ToolUtils::color_to_565(0xFF8888FF));
reed8f76cb92015-04-22 17:38:23 -0700271
272 canvas->drawRect(r, paint);
273 this->doDraw(canvas, path);
halcanary9d524f22016-03-29 09:03:52 -0700274
reed8f76cb92015-04-22 17:38:23 -0700275 canvas->translate(dx, dy);
276
277 canvas->drawRect(r, paint);
278 canvas->clipRect(r);
279 this->doDraw(canvas, path);
280 }
281
282 void onDraw(SkCanvas* canvas) override {
283 canvas->translate(80, 10);
284 this->drawAndClip(canvas, fVPath, 200, 0);
285 canvas->translate(0, 200);
286 this->drawAndClip(canvas, fHPath, 200, 0);
287 }
halcanary9d524f22016-03-29 09:03:52 -0700288
reed8f76cb92015-04-22 17:38:23 -0700289private:
290 typedef skiagm::GM INHERITED;
291};
halcanary385fe4d2015-08-26 13:07:48 -0700292DEF_GM(return new ClipCubicGM;)