tomhudson@google.com | ef279d3 | 2011-12-21 14:27:14 +0000 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Mike Reed | ac9f0c9 | 2020-12-23 10:11:33 -0500 | [diff] [blame] | 9 | #include "include/core/SkBitmap.h" |
Ben Wagner | d1701ba | 2019-04-30 13:44:26 -0400 | [diff] [blame] | 10 | #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 Reed | ad5494d | 2020-08-25 17:36:28 -0400 | [diff] [blame] | 15 | #include "include/core/SkPathBuilder.h" |
Ben Wagner | d1701ba | 2019-04-30 13:44:26 -0400 | [diff] [blame] | 16 | #include "include/core/SkRect.h" |
| 17 | #include "include/core/SkScalar.h" |
| 18 | #include "include/core/SkSize.h" |
| 19 | #include "include/core/SkString.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "src/core/SkCanvasPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "tools/ToolUtils.h" |
tomhudson@google.com | ef279d3 | 2011-12-21 14:27:14 +0000 | [diff] [blame] | 22 | |
tomhudson@google.com | ef279d3 | 2011-12-21 14:27:14 +0000 | [diff] [blame] | 23 | /** 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.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 27 | static void draw(SkCanvas* canvas, SkRect& target, int x, int y) { |
tomhudson@google.com | ef279d3 | 2011-12-21 14:27:14 +0000 | [diff] [blame] | 28 | 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.com | abfa8d1 | 2011-12-28 19:40:48 +0000 | [diff] [blame] | 39 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 40 | target.inset(SkIntToScalar(-2), SkIntToScalar(-2)); |
tomhudson@google.com | ef279d3 | 2011-12-21 14:27:14 +0000 | [diff] [blame] | 41 | canvas->drawRect(target, borderPaint); |
tomhudson@google.com | abfa8d1 | 2011-12-28 19:40:48 +0000 | [diff] [blame] | 42 | target.inset(SkIntToScalar(2), SkIntToScalar(2)); |
tomhudson@google.com | ef279d3 | 2011-12-21 14:27:14 +0000 | [diff] [blame] | 43 | canvas->drawRect(target, backgroundPaint); |
reed | 6699838 | 2016-09-21 11:15:07 -0700 | [diff] [blame] | 44 | canvas->clipRect(target, true); |
tomhudson@google.com | abfa8d1 | 2011-12-28 19:40:48 +0000 | [diff] [blame] | 45 | target.inset(SkIntToScalar(-4), SkIntToScalar(-4)); |
tomhudson@google.com | ef279d3 | 2011-12-21 14:27:14 +0000 | [diff] [blame] | 46 | canvas->drawRect(target, foregroundPaint); |
| 47 | canvas->restore(); |
| 48 | } |
| 49 | |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 50 | static void draw_square(SkCanvas* canvas, int x, int y) { |
tomhudson@google.com | ef279d3 | 2011-12-21 14:27:14 +0000 | [diff] [blame] | 51 | SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 10 * SK_Scalar1)); |
| 52 | draw(canvas, target, x, y); |
| 53 | } |
| 54 | |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 55 | static void draw_column(SkCanvas* canvas, int x, int y) { |
tomhudson@google.com | ef279d3 | 2011-12-21 14:27:14 +0000 | [diff] [blame] | 56 | SkRect target (SkRect::MakeWH(1 * SK_Scalar1, 10 * SK_Scalar1)); |
| 57 | draw(canvas, target, x, y); |
| 58 | } |
| 59 | |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 60 | static void draw_bar(SkCanvas* canvas, int x, int y) { |
tomhudson@google.com | ef279d3 | 2011-12-21 14:27:14 +0000 | [diff] [blame] | 61 | SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 1 * SK_Scalar1)); |
| 62 | draw(canvas, target, x, y); |
| 63 | } |
| 64 | |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 65 | static void draw_rect_tests(SkCanvas* canvas) { |
tomhudson@google.com | ef279d3 | 2011-12-21 14:27:14 +0000 | [diff] [blame] | 66 | 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 Canary | 607c44f | 2019-01-23 10:40:02 -0500 | [diff] [blame] | 77 | DEF_SIMPLE_GM(aaclip, canvas, 240, 120) { |
tomhudson@google.com | ef279d3 | 2011-12-21 14:27:14 +0000 | [diff] [blame] | 78 | // 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.com | abfa8d1 | 2011-12-28 19:40:48 +0000 | [diff] [blame] | 83 | canvas->translate(SkIntToScalar(50), 0); |
tomhudson@google.com | ef279d3 | 2011-12-21 14:27:14 +0000 | [diff] [blame] | 84 | draw_rect_tests(canvas); |
| 85 | |
| 86 | canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5); |
tomhudson@google.com | abfa8d1 | 2011-12-28 19:40:48 +0000 | [diff] [blame] | 87 | canvas->translate(SkIntToScalar(50), 0); |
tomhudson@google.com | ef279d3 | 2011-12-21 14:27:14 +0000 | [diff] [blame] | 88 | draw_rect_tests(canvas); |
| 89 | |
| 90 | canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5); |
tomhudson@google.com | abfa8d1 | 2011-12-28 19:40:48 +0000 | [diff] [blame] | 91 | canvas->translate(SkIntToScalar(50), 0); |
tomhudson@google.com | ef279d3 | 2011-12-21 14:27:14 +0000 | [diff] [blame] | 92 | draw_rect_tests(canvas); |
| 93 | |
| 94 | canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5); |
tomhudson@google.com | abfa8d1 | 2011-12-28 19:40:48 +0000 | [diff] [blame] | 95 | canvas->translate(SkIntToScalar(50), 0); |
tomhudson@google.com | ef279d3 | 2011-12-21 14:27:14 +0000 | [diff] [blame] | 96 | draw_rect_tests(canvas); |
Hal Canary | 607c44f | 2019-01-23 10:40:02 -0500 | [diff] [blame] | 97 | } |
commit-bot@chromium.org | 60b5dce | 2014-04-22 20:24:33 +0000 | [diff] [blame] | 98 | |
| 99 | ///////////////////////////////////////////////////////////////////////// |
| 100 | |
| 101 | #ifdef SK_BUILD_FOR_MAC |
| 102 | |
Ben Wagner | d1701ba | 2019-04-30 13:44:26 -0400 | [diff] [blame] | 103 | #include "include/utils/mac/SkCGUtils.h" |
Ben Wagner | d1701ba | 2019-04-30 13:44:26 -0400 | [diff] [blame] | 104 | |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 105 | static std::unique_ptr<SkCanvas> make_canvas(const SkBitmap& bm) { |
Mike Reed | 50a490a | 2020-02-11 08:00:37 -0500 | [diff] [blame] | 106 | return SkCanvas::MakeRasterDirect(bm.info(), bm.getPixels(), bm.rowBytes()); |
commit-bot@chromium.org | 60b5dce | 2014-04-22 20:24:33 +0000 | [diff] [blame] | 107 | } |
| 108 | |
commit-bot@chromium.org | 60b5dce | 2014-04-22 20:24:33 +0000 | [diff] [blame] | 109 | static void test_image(SkCanvas* canvas, const SkImageInfo& info) { |
| 110 | SkBitmap bm; |
| 111 | bm.allocPixels(info); |
| 112 | |
commit-bot@chromium.org | 60b5dce | 2014-04-22 20:24:33 +0000 | [diff] [blame] | 113 | if (info.isOpaque()) { |
| 114 | bm.eraseColor(SK_ColorGREEN); |
| 115 | } else { |
| 116 | bm.eraseColor(0); |
| 117 | } |
skia.committer@gmail.com | ede0c5c | 2014-04-23 03:04:11 +0000 | [diff] [blame] | 118 | |
commit-bot@chromium.org | 60b5dce | 2014-04-22 20:24:33 +0000 | [diff] [blame] | 119 | SkPaint paint; |
| 120 | paint.setAntiAlias(true); |
| 121 | paint.setColor(SK_ColorBLUE); |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 122 | make_canvas(bm)->drawCircle(50, 50, 49, paint); |
Mike Reed | 568f0ae | 2021-01-24 08:57:23 -0500 | [diff] [blame] | 123 | canvas->drawImage(bm.asImage(), 10, 10); |
skia.committer@gmail.com | ede0c5c | 2014-04-23 03:04:11 +0000 | [diff] [blame] | 124 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 125 | CGImageRef image = SkCreateCGImageRefWithColorspace(bm, nullptr); |
skia.committer@gmail.com | ede0c5c | 2014-04-23 03:04:11 +0000 | [diff] [blame] | 126 | |
commit-bot@chromium.org | 60b5dce | 2014-04-22 20:24:33 +0000 | [diff] [blame] | 127 | SkBitmap bm2; |
| 128 | SkCreateBitmapFromCGImage(&bm2, image); |
Mike Reed | 568f0ae | 2021-01-24 08:57:23 -0500 | [diff] [blame] | 129 | canvas->drawImage(bm2.asImage(), 10, 120); |
Mike Reed | 463c848 | 2016-12-21 12:01:12 -0500 | [diff] [blame] | 130 | canvas->drawImage(SkMakeImageFromCGImage(image), 10, 120 + bm2.height() + 10); |
| 131 | |
| 132 | CGImageRelease(image); |
commit-bot@chromium.org | 60b5dce | 2014-04-22 20:24:33 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Hal Canary | 607c44f | 2019-01-23 10:40:02 -0500 | [diff] [blame] | 135 | DEF_SIMPLE_GM(cgimage, canvas, 800, 250) { |
commit-bot@chromium.org | 60b5dce | 2014-04-22 20:24:33 +0000 | [diff] [blame] | 136 | 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.com | ede0c5c | 2014-04-23 03:04:11 +0000 | [diff] [blame] | 150 | |
commit-bot@chromium.org | 60b5dce | 2014-04-22 20:24:33 +0000 | [diff] [blame] | 151 | 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 Canary | 607c44f | 2019-01-23 10:40:02 -0500 | [diff] [blame] | 156 | } |
skia.committer@gmail.com | ede0c5c | 2014-04-23 03:04:11 +0000 | [diff] [blame] | 157 | |
commit-bot@chromium.org | 60b5dce | 2014-04-22 20:24:33 +0000 | [diff] [blame] | 158 | #endif |
reed | 8f76cb9 | 2015-04-22 17:38:23 -0700 | [diff] [blame] | 159 | |
| 160 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 161 | |
halcanary | 6950de6 | 2015-11-07 05:29:00 -0800 | [diff] [blame] | 162 | // https://bug.skia.org/3716 |
reed | 8f76cb9 | 2015-04-22 17:38:23 -0700 | [diff] [blame] | 163 | class ClipCubicGM : public skiagm::GM { |
| 164 | const SkScalar W = 100; |
| 165 | const SkScalar H = 240; |
| 166 | |
| 167 | SkPath fVPath, fHPath; |
| 168 | public: |
| 169 | ClipCubicGM() { |
Mike Reed | ad5494d | 2020-08-25 17:36:28 -0400 | [diff] [blame] | 170 | fVPath = SkPathBuilder().moveTo(W, 0) |
| 171 | .cubicTo(W, H-10, 0, 10, 0, H) |
| 172 | .detach(); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 173 | |
reed | 8f76cb9 | 2015-04-22 17:38:23 -0700 | [diff] [blame] | 174 | SkMatrix pivot; |
| 175 | pivot.setRotate(90, W/2, H/2); |
Mike Reed | ad5494d | 2020-08-25 17:36:28 -0400 | [diff] [blame] | 176 | fHPath = fVPath.makeTransform(pivot); |
reed | 8f76cb9 | 2015-04-22 17:38:23 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | protected: |
| 180 | SkString onShortName() override { |
| 181 | return SkString("clipcubic"); |
| 182 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 183 | |
reed | 8f76cb9 | 2015-04-22 17:38:23 -0700 | [diff] [blame] | 184 | 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); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 191 | |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 192 | paint.setColor(0xFFCCCCCC); |
reed | 8f76cb9 | 2015-04-22 17:38:23 -0700 | [diff] [blame] | 193 | canvas->drawPath(path, paint); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 194 | |
reed | 8f76cb9 | 2015-04-22 17:38:23 -0700 | [diff] [blame] | 195 | 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 Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 205 | paint.setColor(ToolUtils::color_to_565(0xFF8888FF)); |
reed | 8f76cb9 | 2015-04-22 17:38:23 -0700 | [diff] [blame] | 206 | |
| 207 | canvas->drawRect(r, paint); |
| 208 | this->doDraw(canvas, path); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 209 | |
reed | 8f76cb9 | 2015-04-22 17:38:23 -0700 | [diff] [blame] | 210 | 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 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 223 | |
reed | 8f76cb9 | 2015-04-22 17:38:23 -0700 | [diff] [blame] | 224 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 225 | using INHERITED = skiagm::GM; |
reed | 8f76cb9 | 2015-04-22 17:38:23 -0700 | [diff] [blame] | 226 | }; |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 227 | DEF_GM(return new ClipCubicGM;) |