reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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" |
Kevin Lubick | f437d01 | 2023-06-26 14:35:06 -0400 | [diff] [blame] | 9 | #include "include/core/SkBBHFactory.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 10 | #include "include/core/SkBlendMode.h" |
| 11 | #include "include/core/SkCanvas.h" |
| 12 | #include "include/core/SkMatrix.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkPaint.h" |
| 14 | #include "include/core/SkPath.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 15 | #include "include/core/SkPicture.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "include/core/SkPictureRecorder.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 17 | #include "include/core/SkRect.h" |
| 18 | #include "include/core/SkRefCnt.h" |
| 19 | #include "include/core/SkSize.h" |
| 20 | #include "include/core/SkString.h" |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 21 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 22 | static sk_sp<SkPicture> make_picture() { |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 23 | SkPictureRecorder rec; |
| 24 | SkCanvas* canvas = rec.beginRecording(100, 100); |
| 25 | |
| 26 | SkPaint paint; |
| 27 | paint.setAntiAlias(true); |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 28 | |
| 29 | paint.setColor(0x800000FF); |
| 30 | canvas->drawRect(SkRect::MakeWH(100, 100), paint); |
| 31 | |
| 32 | paint.setColor(0x80FF0000); |
Mike Reed | 06d7c9d | 2020-08-26 12:56:51 -0400 | [diff] [blame] | 33 | canvas->drawPath(SkPath::Polygon({{0, 0}, {100, 0}, {100, 100}}, false), paint); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 34 | |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 35 | paint.setColor(0x8000FF00); |
Mike Reed | 06d7c9d | 2020-08-26 12:56:51 -0400 | [diff] [blame] | 36 | canvas->drawPath(SkPath::Polygon({{0, 0}, {100, 0}, {0, 100}}, false), paint); |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 37 | |
| 38 | paint.setColor(0x80FFFFFF); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 39 | paint.setBlendMode(SkBlendMode::kPlus); |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 40 | canvas->drawRect(SkRect::MakeXYWH(25, 25, 50, 50), paint); |
| 41 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 42 | return rec.finishRecordingAsPicture(); |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | // Exercise the optional arguments to drawPicture |
| 46 | // |
| 47 | class PictureGM : public skiagm::GM { |
| 48 | public: |
caryclark | 63c684a | 2015-02-25 09:04:04 -0800 | [diff] [blame] | 49 | PictureGM() |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 50 | : fPicture(nullptr) |
caryclark | 63c684a | 2015-02-25 09:04:04 -0800 | [diff] [blame] | 51 | {} |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 52 | |
| 53 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 54 | void onOnceBeforeDraw() override { |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 55 | fPicture = make_picture(); |
caryclark | 63c684a | 2015-02-25 09:04:04 -0800 | [diff] [blame] | 56 | } |
| 57 | |
Leandro Lovisolo | 24fa211 | 2023-08-15 19:05:17 +0000 | [diff] [blame] | 58 | SkString getName() const override { return SkString("pictures"); } |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 59 | |
Leandro Lovisolo | 8f02388 | 2023-08-15 21:13:52 +0000 | [diff] [blame] | 60 | SkISize getISize() override { return SkISize::Make(450, 120); } |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 61 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 62 | void onDraw(SkCanvas* canvas) override { |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 63 | canvas->translate(10, 10); |
| 64 | |
| 65 | SkMatrix matrix; |
| 66 | SkPaint paint; |
| 67 | |
| 68 | canvas->drawPicture(fPicture); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 69 | |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 70 | matrix.setTranslate(110, 0); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 71 | canvas->drawPicture(fPicture, &matrix, nullptr); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 72 | |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 73 | matrix.postTranslate(110, 0); |
| 74 | canvas->drawPicture(fPicture, &matrix, &paint); |
| 75 | |
Mike Reed | 9407e24 | 2019-02-15 16:13:57 -0500 | [diff] [blame] | 76 | paint.setAlphaf(0.5f); |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 77 | matrix.postTranslate(110, 0); |
| 78 | canvas->drawPicture(fPicture, &matrix, &paint); |
| 79 | } |
| 80 | |
| 81 | private: |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 82 | sk_sp<SkPicture> fPicture; |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 83 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 84 | using INHERITED = skiagm::GM; |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
Yuqian Li | 74587d8 | 2019-08-14 12:45:17 -0700 | [diff] [blame] | 87 | // Exercise drawing a picture with a cull rect of non-zero top-left corner. |
| 88 | // |
| 89 | // See skbug.com/9334, which would fail |
| 90 | // ``` |
| 91 | // dm -m picture_cull_rect --config serialize-8888 |
| 92 | // ``` |
| 93 | // until that bug is fixed. |
| 94 | class PictureCullRectGM : public skiagm::GM { |
| 95 | public: |
| 96 | PictureCullRectGM() |
| 97 | : fPicture(nullptr) |
| 98 | {} |
| 99 | |
| 100 | protected: |
| 101 | void onOnceBeforeDraw() override { |
| 102 | SkPictureRecorder rec; |
| 103 | SkRTreeFactory rtreeFactory; |
| 104 | SkCanvas* canvas = rec.beginRecording(100, 100, &rtreeFactory); |
| 105 | |
| 106 | SkPaint paint; |
| 107 | paint.setAntiAlias(false); |
| 108 | |
| 109 | SkRect rect = SkRect::MakeLTRB(0, 80, 100, 100); |
| 110 | |
| 111 | // Make picture complex enough to trigger the cull rect and bbh (RTree) computations. |
| 112 | // (A single drawRect won't trigger it.) |
| 113 | paint.setColor(0x800000FF); |
| 114 | canvas->drawRect(rect, paint); |
| 115 | canvas->drawOval(rect, paint); |
| 116 | |
| 117 | fPicture = rec.finishRecordingAsPicture(); |
| 118 | |
| 119 | SkASSERT(fPicture->cullRect().top() == 80); |
| 120 | } |
| 121 | |
Leandro Lovisolo | 24fa211 | 2023-08-15 19:05:17 +0000 | [diff] [blame] | 122 | SkString getName() const override { return SkString("picture_cull_rect"); } |
Yuqian Li | 74587d8 | 2019-08-14 12:45:17 -0700 | [diff] [blame] | 123 | |
Leandro Lovisolo | 8f02388 | 2023-08-15 21:13:52 +0000 | [diff] [blame] | 124 | SkISize getISize() override { return SkISize::Make(120, 120); } |
Yuqian Li | 74587d8 | 2019-08-14 12:45:17 -0700 | [diff] [blame] | 125 | |
| 126 | void onDraw(SkCanvas* canvas) override { |
| 127 | canvas->clipRect(SkRect::MakeLTRB(0, 60, 120, 120)); |
| 128 | canvas->translate(10, 10); |
| 129 | canvas->drawPicture(fPicture); |
| 130 | } |
| 131 | |
| 132 | private: |
| 133 | sk_sp<SkPicture> fPicture; |
| 134 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 135 | using INHERITED = skiagm::GM; |
Yuqian Li | 74587d8 | 2019-08-14 12:45:17 -0700 | [diff] [blame] | 136 | }; |
| 137 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 138 | DEF_GM(return new PictureGM;) |
Yuqian Li | 74587d8 | 2019-08-14 12:45:17 -0700 | [diff] [blame] | 139 | DEF_GM(return new PictureCullRectGM;) |