blob: e4001fce32efe452cff81022c3689c4aa5ee3d6e [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"
Ben Wagnerd1701ba2019-04-30 13:44:26 -0400162
Mike Reed5df49342016-11-12 08:06:55 -0600163static std::unique_ptr<SkCanvas> make_canvas(const SkBitmap& bm) {
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000164 const SkImageInfo& info = bm.info();
165 if (info.bytesPerPixel() == 4) {
Mike Reed5df49342016-11-12 08:06:55 -0600166 return SkCanvas::MakeRasterDirectN32(info.width(), info.height(),
167 (SkPMColor*)bm.getPixels(),
168 bm.rowBytes());
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000169 } else {
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500170 return std::make_unique<SkCanvas>(bm);
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000171 }
172}
173
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000174static void test_image(SkCanvas* canvas, const SkImageInfo& info) {
175 SkBitmap bm;
176 bm.allocPixels(info);
177
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000178 if (info.isOpaque()) {
179 bm.eraseColor(SK_ColorGREEN);
180 } else {
181 bm.eraseColor(0);
182 }
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000183
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000184 SkPaint paint;
185 paint.setAntiAlias(true);
186 paint.setColor(SK_ColorBLUE);
Mike Reed5df49342016-11-12 08:06:55 -0600187 make_canvas(bm)->drawCircle(50, 50, 49, paint);
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000188 canvas->drawBitmap(bm, 10, 10);
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000189
halcanary96fcdcc2015-08-27 07:41:13 -0700190 CGImageRef image = SkCreateCGImageRefWithColorspace(bm, nullptr);
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000191
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000192 SkBitmap bm2;
193 SkCreateBitmapFromCGImage(&bm2, image);
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000194 canvas->drawBitmap(bm2, 10, 120);
Mike Reed463c8482016-12-21 12:01:12 -0500195 canvas->drawImage(SkMakeImageFromCGImage(image), 10, 120 + bm2.height() + 10);
196
197 CGImageRelease(image);
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000198}
199
Hal Canary607c44f2019-01-23 10:40:02 -0500200DEF_SIMPLE_GM(cgimage, canvas, 800, 250) {
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000201 const struct {
202 SkColorType fCT;
203 SkAlphaType fAT;
204 } rec[] = {
205 { kRGB_565_SkColorType, kOpaque_SkAlphaType },
206
207 { kRGBA_8888_SkColorType, kPremul_SkAlphaType },
208 { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType },
209 { kRGBA_8888_SkColorType, kOpaque_SkAlphaType },
210
211 { kBGRA_8888_SkColorType, kPremul_SkAlphaType },
212 { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType },
213 { kBGRA_8888_SkColorType, kOpaque_SkAlphaType },
214 };
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000215
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000216 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
217 SkImageInfo info = SkImageInfo::Make(100, 100, rec[i].fCT, rec[i].fAT);
218 test_image(canvas, info);
219 canvas->translate(info.width() + 10, 0);
220 }
Hal Canary607c44f2019-01-23 10:40:02 -0500221}
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000222
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000223#endif
reed8f76cb92015-04-22 17:38:23 -0700224
225///////////////////////////////////////////////////////////////////////////////////////////////////
226
halcanary6950de62015-11-07 05:29:00 -0800227// https://bug.skia.org/3716
reed8f76cb92015-04-22 17:38:23 -0700228class ClipCubicGM : public skiagm::GM {
229 const SkScalar W = 100;
230 const SkScalar H = 240;
231
232 SkPath fVPath, fHPath;
233public:
234 ClipCubicGM() {
235 fVPath.moveTo(W, 0);
236 fVPath.cubicTo(W, H-10, 0, 10, 0, H);
halcanary9d524f22016-03-29 09:03:52 -0700237
reed8f76cb92015-04-22 17:38:23 -0700238 SkMatrix pivot;
239 pivot.setRotate(90, W/2, H/2);
240 fVPath.transform(pivot, &fHPath);
241 }
242
243protected:
244 SkString onShortName() override {
245 return SkString("clipcubic");
246 }
halcanary9d524f22016-03-29 09:03:52 -0700247
reed8f76cb92015-04-22 17:38:23 -0700248 SkISize onISize() override {
249 return SkISize::Make(400, 410);
250 }
251
252 void doDraw(SkCanvas* canvas, const SkPath& path) {
253 SkPaint paint;
254 paint.setAntiAlias(true);
halcanary9d524f22016-03-29 09:03:52 -0700255
Mike Kleind46dce32018-08-16 10:17:03 -0400256 paint.setColor(0xFFCCCCCC);
reed8f76cb92015-04-22 17:38:23 -0700257 canvas->drawPath(path, paint);
halcanary9d524f22016-03-29 09:03:52 -0700258
reed8f76cb92015-04-22 17:38:23 -0700259 paint.setColor(SK_ColorRED);
260 paint.setStyle(SkPaint::kStroke_Style);
261 canvas->drawPath(path, paint);
262 }
263
264 void drawAndClip(SkCanvas* canvas, const SkPath& path, SkScalar dx, SkScalar dy) {
265 SkAutoCanvasRestore acr(canvas, true);
266
267 SkRect r = SkRect::MakeXYWH(0, H/4, W, H/2);
268 SkPaint paint;
Mike Kleinea3f0142019-03-20 11:12:10 -0500269 paint.setColor(ToolUtils::color_to_565(0xFF8888FF));
reed8f76cb92015-04-22 17:38:23 -0700270
271 canvas->drawRect(r, paint);
272 this->doDraw(canvas, path);
halcanary9d524f22016-03-29 09:03:52 -0700273
reed8f76cb92015-04-22 17:38:23 -0700274 canvas->translate(dx, dy);
275
276 canvas->drawRect(r, paint);
277 canvas->clipRect(r);
278 this->doDraw(canvas, path);
279 }
280
281 void onDraw(SkCanvas* canvas) override {
282 canvas->translate(80, 10);
283 this->drawAndClip(canvas, fVPath, 200, 0);
284 canvas->translate(0, 200);
285 this->drawAndClip(canvas, fHPath, 200, 0);
286 }
halcanary9d524f22016-03-29 09:03:52 -0700287
reed8f76cb92015-04-22 17:38:23 -0700288private:
289 typedef skiagm::GM INHERITED;
290};
halcanary385fe4d2015-08-26 13:07:48 -0700291DEF_GM(return new ClipCubicGM;)