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