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"
11 #include "SkPictureRecorder.h"
12 
13 // This GM exercises the SkPictureImageFilter ImageFilter class.
14 
15 class PictureImageFilterGM : public skiagm::GM {
16 public:
PictureImageFilterGM()17     PictureImageFilterGM() {
18     }
19 
20 protected:
onShortName()21     SkString onShortName() override {
22         return SkString("pictureimagefilter");
23     }
24 
makePicture()25     void makePicture() {
26         SkPictureRecorder recorder;
27         SkCanvas* canvas = recorder.beginRecording(100, 100, NULL, 0);
28         canvas->clear(SK_ColorBLACK);
29         SkPaint paint;
30         paint.setAntiAlias(true);
31         sk_tool_utils::set_portable_typeface(&paint);
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);
36         fPicture.reset(recorder.endRecording());
37     }
38 
onISize()39     SkISize onISize() override { return SkISize::Make(600, 300); }
40 
onOnceBeforeDraw()41     void onOnceBeforeDraw() override {
42         this->makePicture();
43     }
44 
fillRectFiltered(SkCanvas * canvas,const SkRect & clipRect,SkImageFilter * filter)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 
onDraw(SkCanvas * canvas)54     void onDraw(SkCanvas* canvas) override {
55         canvas->clear(SK_ColorBLACK);
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);
60             SkAutoTUnref<SkPictureImageFilter> pictureSource(
61                 SkPictureImageFilter::Create(fPicture));
62             SkAutoTUnref<SkPictureImageFilter> pictureSourceSrcRect(
63                 SkPictureImageFilter::Create(fPicture, srcRect));
64             SkAutoTUnref<SkPictureImageFilter> pictureSourceEmptyRect(
65                 SkPictureImageFilter::Create(fPicture, emptyRect));
66             SkAutoTUnref<SkPictureImageFilter> pictureSourceResampled(
67                 SkPictureImageFilter::CreateForLocalSpace(fPicture, fPicture->cullRect(),
68                     kLow_SkFilterQuality));
69             SkAutoTUnref<SkPictureImageFilter> pictureSourcePixelated(
70                 SkPictureImageFilter::CreateForLocalSpace(fPicture, fPicture->cullRect(),
71                     kNone_SkFilterQuality));
72 
73             canvas->save();
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);
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);
97 
98             // Draw the picture scaled, pixelated
99             canvas->translate(srcRect.width(), 0);
100             fillRectFiltered(canvas, srcRect, pictureSourcePixelated);
101         }
102     }
103 
104 private:
105     SkAutoTUnref<SkPicture> fPicture;
106     typedef GM INHERITED;
107 };
108 
109 ///////////////////////////////////////////////////////////////////////////////
110 
111 DEF_GM( return new PictureImageFilterGM; )
112