blob: 6fb081a32bc7fd241f1c2b7e3b8d5bb5b59a2e45 [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"
Mike Reedac9f0c92020-12-23 10:11:33 -05009#include "include/core/SkBitmap.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -040010#include "include/core/SkBlendMode.h"
11#include "include/core/SkCanvas.h"
12#include "include/core/SkColor.h"
13#include "include/core/SkMatrix.h"
14#include "include/core/SkPaint.h"
Mike Reedad5494d2020-08-25 17:36:28 -040015#include "include/core/SkPathBuilder.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -040016#include "include/core/SkRect.h"
17#include "include/core/SkScalar.h"
18#include "include/core/SkSize.h"
19#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/core/SkCanvasPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "tools/ToolUtils.h"
tomhudson@google.comef279d32011-12-21 14:27:14 +000022
tomhudson@google.comef279d32011-12-21 14:27:14 +000023/** Draw a 2px border around the target, then red behind the target;
24 set the clip to match the target, then draw >> the target in blue.
25*/
26
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000027static void draw(SkCanvas* canvas, SkRect& target, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +000028 SkPaint borderPaint;
29 borderPaint.setColor(SkColorSetRGB(0x0, 0xDD, 0x0));
30 borderPaint.setAntiAlias(true);
31 SkPaint backgroundPaint;
32 backgroundPaint.setColor(SkColorSetRGB(0xDD, 0x0, 0x0));
33 backgroundPaint.setAntiAlias(true);
34 SkPaint foregroundPaint;
35 foregroundPaint.setColor(SkColorSetRGB(0x0, 0x0, 0xDD));
36 foregroundPaint.setAntiAlias(true);
37
38 canvas->save();
tomhudson@google.comabfa8d12011-12-28 19:40:48 +000039 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
40 target.inset(SkIntToScalar(-2), SkIntToScalar(-2));
tomhudson@google.comef279d32011-12-21 14:27:14 +000041 canvas->drawRect(target, borderPaint);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +000042 target.inset(SkIntToScalar(2), SkIntToScalar(2));
tomhudson@google.comef279d32011-12-21 14:27:14 +000043 canvas->drawRect(target, backgroundPaint);
reed66998382016-09-21 11:15:07 -070044 canvas->clipRect(target, true);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +000045 target.inset(SkIntToScalar(-4), SkIntToScalar(-4));
tomhudson@google.comef279d32011-12-21 14:27:14 +000046 canvas->drawRect(target, foregroundPaint);
47 canvas->restore();
48}
49
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000050static void draw_square(SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +000051 SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 10 * SK_Scalar1));
52 draw(canvas, target, x, y);
53}
54
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000055static void draw_column(SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +000056 SkRect target (SkRect::MakeWH(1 * SK_Scalar1, 10 * SK_Scalar1));
57 draw(canvas, target, x, y);
58}
59
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000060static void draw_bar(SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +000061 SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 1 * SK_Scalar1));
62 draw(canvas, target, x, y);
63}
64
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000065static void draw_rect_tests(SkCanvas* canvas) {
tomhudson@google.comef279d32011-12-21 14:27:14 +000066 draw_square(canvas, 10, 10);
67 draw_column(canvas, 30, 10);
68 draw_bar(canvas, 10, 30);
69}
70
71/**
72 Test a set of clipping problems discovered while writing blitAntiRect,
73 and test all the code paths through the clipping blitters.
74 Each region should show as a blue center surrounded by a 2px green
75 border, with no red.
76*/
Hal Canary607c44f2019-01-23 10:40:02 -050077DEF_SIMPLE_GM(aaclip, canvas, 240, 120) {
tomhudson@google.comef279d32011-12-21 14:27:14 +000078 // Initial pixel-boundary-aligned draw
79 draw_rect_tests(canvas);
80
81 // Repeat 4x with .2, .4, .6, .8 px offsets
82 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +000083 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +000084 draw_rect_tests(canvas);
85
86 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +000087 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +000088 draw_rect_tests(canvas);
89
90 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +000091 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +000092 draw_rect_tests(canvas);
93
94 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +000095 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +000096 draw_rect_tests(canvas);
Hal Canary607c44f2019-01-23 10:40:02 -050097}
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +000098
99/////////////////////////////////////////////////////////////////////////
100
101#ifdef SK_BUILD_FOR_MAC
102
Ben Wagnerd1701ba2019-04-30 13:44:26 -0400103#include "include/utils/mac/SkCGUtils.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -0400104
Mike Reed5df49342016-11-12 08:06:55 -0600105static std::unique_ptr<SkCanvas> make_canvas(const SkBitmap& bm) {
Mike Reed50a490a2020-02-11 08:00:37 -0500106 return SkCanvas::MakeRasterDirect(bm.info(), bm.getPixels(), bm.rowBytes());
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000107}
108
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000109static void test_image(SkCanvas* canvas, const SkImageInfo& info) {
110 SkBitmap bm;
111 bm.allocPixels(info);
112
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000113 if (info.isOpaque()) {
114 bm.eraseColor(SK_ColorGREEN);
115 } else {
116 bm.eraseColor(0);
117 }
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000118
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000119 SkPaint paint;
120 paint.setAntiAlias(true);
121 paint.setColor(SK_ColorBLUE);
Mike Reed5df49342016-11-12 08:06:55 -0600122 make_canvas(bm)->drawCircle(50, 50, 49, paint);
Mike Reed568f0ae2021-01-24 08:57:23 -0500123 canvas->drawImage(bm.asImage(), 10, 10);
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000124
halcanary96fcdcc2015-08-27 07:41:13 -0700125 CGImageRef image = SkCreateCGImageRefWithColorspace(bm, nullptr);
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000126
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000127 SkBitmap bm2;
128 SkCreateBitmapFromCGImage(&bm2, image);
Mike Reed568f0ae2021-01-24 08:57:23 -0500129 canvas->drawImage(bm2.asImage(), 10, 120);
Mike Reed463c8482016-12-21 12:01:12 -0500130 canvas->drawImage(SkMakeImageFromCGImage(image), 10, 120 + bm2.height() + 10);
131
132 CGImageRelease(image);
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000133}
134
Hal Canary607c44f2019-01-23 10:40:02 -0500135DEF_SIMPLE_GM(cgimage, canvas, 800, 250) {
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000136 const struct {
137 SkColorType fCT;
138 SkAlphaType fAT;
139 } rec[] = {
140 { kRGB_565_SkColorType, kOpaque_SkAlphaType },
141
142 { kRGBA_8888_SkColorType, kPremul_SkAlphaType },
143 { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType },
144 { kRGBA_8888_SkColorType, kOpaque_SkAlphaType },
145
146 { kBGRA_8888_SkColorType, kPremul_SkAlphaType },
147 { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType },
148 { kBGRA_8888_SkColorType, kOpaque_SkAlphaType },
149 };
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000150
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000151 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
152 SkImageInfo info = SkImageInfo::Make(100, 100, rec[i].fCT, rec[i].fAT);
153 test_image(canvas, info);
154 canvas->translate(info.width() + 10, 0);
155 }
Hal Canary607c44f2019-01-23 10:40:02 -0500156}
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000157
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000158#endif
reed8f76cb92015-04-22 17:38:23 -0700159
160///////////////////////////////////////////////////////////////////////////////////////////////////
161
halcanary6950de62015-11-07 05:29:00 -0800162// https://bug.skia.org/3716
reed8f76cb92015-04-22 17:38:23 -0700163class ClipCubicGM : public skiagm::GM {
164 const SkScalar W = 100;
165 const SkScalar H = 240;
166
167 SkPath fVPath, fHPath;
168public:
169 ClipCubicGM() {
Mike Reedad5494d2020-08-25 17:36:28 -0400170 fVPath = SkPathBuilder().moveTo(W, 0)
171 .cubicTo(W, H-10, 0, 10, 0, H)
172 .detach();
halcanary9d524f22016-03-29 09:03:52 -0700173
reed8f76cb92015-04-22 17:38:23 -0700174 SkMatrix pivot;
175 pivot.setRotate(90, W/2, H/2);
Mike Reedad5494d2020-08-25 17:36:28 -0400176 fHPath = fVPath.makeTransform(pivot);
reed8f76cb92015-04-22 17:38:23 -0700177 }
178
179protected:
180 SkString onShortName() override {
181 return SkString("clipcubic");
182 }
halcanary9d524f22016-03-29 09:03:52 -0700183
reed8f76cb92015-04-22 17:38:23 -0700184 SkISize onISize() override {
185 return SkISize::Make(400, 410);
186 }
187
188 void doDraw(SkCanvas* canvas, const SkPath& path) {
189 SkPaint paint;
190 paint.setAntiAlias(true);
halcanary9d524f22016-03-29 09:03:52 -0700191
Mike Kleind46dce32018-08-16 10:17:03 -0400192 paint.setColor(0xFFCCCCCC);
reed8f76cb92015-04-22 17:38:23 -0700193 canvas->drawPath(path, paint);
halcanary9d524f22016-03-29 09:03:52 -0700194
reed8f76cb92015-04-22 17:38:23 -0700195 paint.setColor(SK_ColorRED);
196 paint.setStyle(SkPaint::kStroke_Style);
197 canvas->drawPath(path, paint);
198 }
199
200 void drawAndClip(SkCanvas* canvas, const SkPath& path, SkScalar dx, SkScalar dy) {
201 SkAutoCanvasRestore acr(canvas, true);
202
203 SkRect r = SkRect::MakeXYWH(0, H/4, W, H/2);
204 SkPaint paint;
Mike Kleinea3f0142019-03-20 11:12:10 -0500205 paint.setColor(ToolUtils::color_to_565(0xFF8888FF));
reed8f76cb92015-04-22 17:38:23 -0700206
207 canvas->drawRect(r, paint);
208 this->doDraw(canvas, path);
halcanary9d524f22016-03-29 09:03:52 -0700209
reed8f76cb92015-04-22 17:38:23 -0700210 canvas->translate(dx, dy);
211
212 canvas->drawRect(r, paint);
213 canvas->clipRect(r);
214 this->doDraw(canvas, path);
215 }
216
217 void onDraw(SkCanvas* canvas) override {
218 canvas->translate(80, 10);
219 this->drawAndClip(canvas, fVPath, 200, 0);
220 canvas->translate(0, 200);
221 this->drawAndClip(canvas, fHPath, 200, 0);
222 }
halcanary9d524f22016-03-29 09:03:52 -0700223
reed8f76cb92015-04-22 17:38:23 -0700224private:
John Stiles7571f9e2020-09-02 22:42:33 -0400225 using INHERITED = skiagm::GM;
reed8f76cb92015-04-22 17:38:23 -0700226};
halcanary385fe4d2015-08-26 13:07:48 -0700227DEF_GM(return new ClipCubicGM;)