senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | |
| 10 | #include "SkPictureImageFilter.h" |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 11 | #include "SkPictureRecorder.h" |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 12 | |
| 13 | // This GM exercises the SkPictureImageFilter ImageFilter class. |
| 14 | |
| 15 | class PictureImageFilterGM : public skiagm::GM { |
| 16 | public: |
| 17 | PictureImageFilterGM() { |
| 18 | } |
| 19 | |
| 20 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 21 | SkString onShortName() override { |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 22 | return SkString("pictureimagefilter"); |
| 23 | } |
| 24 | |
| 25 | void makePicture() { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 26 | SkPictureRecorder recorder; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 27 | SkCanvas* canvas = recorder.beginRecording(100, 100, nullptr, 0); |
senorblanco | aad8b27 | 2015-04-08 12:36:08 -0700 | [diff] [blame] | 28 | canvas->clear(SK_ColorBLACK); |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 29 | SkPaint paint; |
| 30 | paint.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 31 | sk_tool_utils::set_portable_typeface(&paint); |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 32 | paint.setColor(0xFFFFFFFF); |
| 33 | paint.setTextSize(SkIntToScalar(96)); |
| 34 | const char* str = "e"; |
| 35 | canvas->drawText(str, strlen(str), SkIntToScalar(20), SkIntToScalar(70), paint); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 36 | fPicture.reset(recorder.endRecording()); |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 37 | } |
| 38 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 39 | SkISize onISize() override { return SkISize::Make(600, 300); } |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 40 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 41 | void onOnceBeforeDraw() override { |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 42 | this->makePicture(); |
| 43 | } |
| 44 | |
| 45 | static void fillRectFiltered(SkCanvas* canvas, const SkRect& clipRect, SkImageFilter* filter) { |
| 46 | SkPaint paint; |
| 47 | paint.setImageFilter(filter); |
| 48 | canvas->save(); |
| 49 | canvas->clipRect(clipRect); |
| 50 | canvas->drawPaint(paint); |
| 51 | canvas->restore(); |
| 52 | } |
| 53 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 54 | void onDraw(SkCanvas* canvas) override { |
senorblanco | aad8b27 | 2015-04-08 12:36:08 -0700 | [diff] [blame] | 55 | canvas->clear(SK_ColorBLACK); |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 56 | { |
| 57 | SkRect srcRect = SkRect::MakeXYWH(20, 20, 30, 30); |
| 58 | SkRect emptyRect = SkRect::MakeXYWH(20, 20, 0, 0); |
| 59 | SkRect bounds = SkRect::MakeXYWH(0, 0, 100, 100); |
reed | 80a6dca | 2015-10-06 11:12:23 -0700 | [diff] [blame^] | 60 | SkAutoTUnref<SkImageFilter> pictureSource( |
Justin Novosad | 5234075 | 2014-12-02 14:50:56 -0500 | [diff] [blame] | 61 | SkPictureImageFilter::Create(fPicture)); |
reed | 80a6dca | 2015-10-06 11:12:23 -0700 | [diff] [blame^] | 62 | SkAutoTUnref<SkImageFilter> pictureSourceSrcRect( |
Justin Novosad | 5234075 | 2014-12-02 14:50:56 -0500 | [diff] [blame] | 63 | SkPictureImageFilter::Create(fPicture, srcRect)); |
reed | 80a6dca | 2015-10-06 11:12:23 -0700 | [diff] [blame^] | 64 | SkAutoTUnref<SkImageFilter> pictureSourceEmptyRect( |
Justin Novosad | 5234075 | 2014-12-02 14:50:56 -0500 | [diff] [blame] | 65 | SkPictureImageFilter::Create(fPicture, emptyRect)); |
reed | 80a6dca | 2015-10-06 11:12:23 -0700 | [diff] [blame^] | 66 | SkAutoTUnref<SkImageFilter> pictureSourceResampled( |
junov | f3c78cc | 2014-12-09 13:07:22 -0800 | [diff] [blame] | 67 | SkPictureImageFilter::CreateForLocalSpace(fPicture, fPicture->cullRect(), |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 68 | kLow_SkFilterQuality)); |
reed | 80a6dca | 2015-10-06 11:12:23 -0700 | [diff] [blame^] | 69 | SkAutoTUnref<SkImageFilter> pictureSourcePixelated( |
junov | f3c78cc | 2014-12-09 13:07:22 -0800 | [diff] [blame] | 70 | SkPictureImageFilter::CreateForLocalSpace(fPicture, fPicture->cullRect(), |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 71 | kNone_SkFilterQuality)); |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 72 | |
Justin Novosad | 5234075 | 2014-12-02 14:50:56 -0500 | [diff] [blame] | 73 | canvas->save(); |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 74 | // Draw the picture unscaled. |
| 75 | fillRectFiltered(canvas, bounds, pictureSource); |
| 76 | canvas->translate(SkIntToScalar(100), 0); |
| 77 | |
| 78 | // Draw an unscaled subset of the source picture. |
| 79 | fillRectFiltered(canvas, bounds, pictureSourceSrcRect); |
| 80 | canvas->translate(SkIntToScalar(100), 0); |
| 81 | |
| 82 | // Draw the picture to an empty rect (should draw nothing). |
| 83 | fillRectFiltered(canvas, bounds, pictureSourceEmptyRect); |
| 84 | canvas->translate(SkIntToScalar(100), 0); |
Justin Novosad | 5234075 | 2014-12-02 14:50:56 -0500 | [diff] [blame] | 85 | |
| 86 | canvas->restore(); |
| 87 | |
| 88 | // Draw the picture scaled |
| 89 | canvas->translate(0, SkIntToScalar(100)); |
| 90 | canvas->scale(200 / srcRect.width(), 200 / srcRect.height()); |
| 91 | canvas->translate(-srcRect.fLeft, -srcRect.fTop); |
| 92 | fillRectFiltered(canvas, srcRect, pictureSource); |
| 93 | |
| 94 | // Draw the picture scaled, but rasterized at original resolution |
| 95 | canvas->translate(srcRect.width(), 0); |
| 96 | fillRectFiltered(canvas, srcRect, pictureSourceResampled); |
junov | f3c78cc | 2014-12-09 13:07:22 -0800 | [diff] [blame] | 97 | |
| 98 | // Draw the picture scaled, pixelated |
| 99 | canvas->translate(srcRect.width(), 0); |
| 100 | fillRectFiltered(canvas, srcRect, pictureSourcePixelated); |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 104 | private: |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 105 | SkAutoTUnref<SkPicture> fPicture; |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 106 | typedef GM INHERITED; |
| 107 | }; |
| 108 | |
| 109 | /////////////////////////////////////////////////////////////////////////////// |
| 110 | |
| 111 | DEF_GM( return new PictureImageFilterGM; ) |