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 | |
| 8 | #include "gm.h" |
| 9 | #include "SkPaint.h" |
Mike Reed | c090c64 | 2017-05-16 10:39:06 -0400 | [diff] [blame] | 10 | #include "SkPath.h" |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 11 | #include "SkPictureRecorder.h" |
| 12 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 13 | static sk_sp<SkPicture> make_picture() { |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 14 | SkPictureRecorder rec; |
| 15 | SkCanvas* canvas = rec.beginRecording(100, 100); |
| 16 | |
| 17 | SkPaint paint; |
| 18 | paint.setAntiAlias(true); |
| 19 | SkPath path; |
| 20 | |
| 21 | paint.setColor(0x800000FF); |
| 22 | canvas->drawRect(SkRect::MakeWH(100, 100), paint); |
| 23 | |
| 24 | paint.setColor(0x80FF0000); |
| 25 | path.moveTo(0, 0); path.lineTo(100, 0); path.lineTo(100, 100); |
| 26 | canvas->drawPath(path, paint); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 27 | |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 28 | paint.setColor(0x8000FF00); |
| 29 | path.reset(); path.moveTo(0, 0); path.lineTo(100, 0); path.lineTo(0, 100); |
| 30 | canvas->drawPath(path, paint); |
| 31 | |
| 32 | paint.setColor(0x80FFFFFF); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 33 | paint.setBlendMode(SkBlendMode::kPlus); |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 34 | canvas->drawRect(SkRect::MakeXYWH(25, 25, 50, 50), paint); |
| 35 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 36 | return rec.finishRecordingAsPicture(); |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | // Exercise the optional arguments to drawPicture |
| 40 | // |
| 41 | class PictureGM : public skiagm::GM { |
| 42 | public: |
caryclark | 63c684a | 2015-02-25 09:04:04 -0800 | [diff] [blame] | 43 | PictureGM() |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 44 | : fPicture(nullptr) |
caryclark | 63c684a | 2015-02-25 09:04:04 -0800 | [diff] [blame] | 45 | {} |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 46 | |
| 47 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 48 | void onOnceBeforeDraw() override { |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 49 | fPicture = make_picture(); |
caryclark | 63c684a | 2015-02-25 09:04:04 -0800 | [diff] [blame] | 50 | } |
| 51 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 52 | SkString onShortName() override { |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 53 | return SkString("pictures"); |
| 54 | } |
| 55 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 56 | SkISize onISize() override { |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 57 | return SkISize::Make(450, 120); |
| 58 | } |
| 59 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 60 | void onDraw(SkCanvas* canvas) override { |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 61 | canvas->translate(10, 10); |
| 62 | |
| 63 | SkMatrix matrix; |
| 64 | SkPaint paint; |
| 65 | |
| 66 | canvas->drawPicture(fPicture); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 67 | |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 68 | matrix.setTranslate(110, 0); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 69 | canvas->drawPicture(fPicture, &matrix, nullptr); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 70 | |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 71 | matrix.postTranslate(110, 0); |
| 72 | canvas->drawPicture(fPicture, &matrix, &paint); |
| 73 | |
| 74 | paint.setAlpha(0x80); |
| 75 | matrix.postTranslate(110, 0); |
| 76 | canvas->drawPicture(fPicture, &matrix, &paint); |
| 77 | } |
| 78 | |
| 79 | private: |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 80 | sk_sp<SkPicture> fPicture; |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 81 | |
| 82 | typedef skiagm::GM INHERITED; |
| 83 | }; |
| 84 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 85 | DEF_GM(return new PictureGM;) |